[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 164, Mon May 6 10:07:19 2002 UTC revision 1693, Fri Mar 3 11:54:58 2006 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-2003 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 program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 21 
21   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   ***************************************************************************/   * $Id: encoder.c,v 1.128 2006-03-03 11:54:58 syskin Exp $
   
 /****************************************************************************  
  *  
  *  History  
  *  
  *  14.04.2002 added FrameCodeB()  
  *  
  *  $Id: encoder.c,v 1.35 2002-05-06 10:07:18 suxen_drol Exp $  
25   *   *
26   ***************************************************************************/   ****************************************************************************/
27    
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <stdio.h>  #include <stdio.h>
30  #include <math.h>  #include <math.h>
31    #include <string.h>
32    
33  #include "encoder.h"  #include "encoder.h"
34  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
35  #include "global.h"  #include "global.h"
36  #include "utils/timer.h"  #include "utils/timer.h"
37  #include "image/image.h"  #include "image/image.h"
38    #include "image/font.h"
39    #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "utils/ratecontrol.h"  
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  # include "motion/motion_smp.h"
 #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  
53    
54    
55  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
56  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
57  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits);   ****************************************************************************/
58    
59  static int DQtab[4] =  static int FrameCodeI(Encoder * pEnc,
60  {                                            Bitstream * bs);
         -1, -2, 1, 2  
 };  
61    
62  static int iDQtab[5] =  static int FrameCodeP(Encoder * pEnc,
63  {                                            Bitstream * bs);
         1, 0, NO_CHANGE, 2, 3  
 };  
64    
65    static void FrameCodeB(Encoder * pEnc,
66                                               FRAMEINFO * frame,
67                                               Bitstream * bs);
68    
 void static image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
69    
70    /*****************************************************************************
71     * Encoder creation
72     *
73     * This function creates an Encoder instance, it allocates all necessary
74     * image buffers (reference, current and bframes) and initialize the internal
75     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
76     *
77     * The code seems to be very long but is very basic, mainly memory allocation
78     * and cleaning code.
79     *
80     * Returned values :
81     *      - 0                             - no errors
82     *      - XVID_ERR_MEMORY - the libc could not allocate memory, the function
83     *                                              cleans the structure before exiting.
84     *                                              pParam->handle is also set to NULL.
85     *
86     ****************************************************************************/
87    
88  int encoder_create(XVID_ENC_PARAM * pParam)  /*
89     * Simplify the "fincr/fbase" fraction
90    */
91    static int
92    gcd(int a, int b)
93  {  {
94          Encoder *pEnc;          int r ;
         uint32_t i;  
   
         pParam->handle = NULL;  
   
         ENC_CHECK(pParam);  
95    
96          ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);          if (b > a) {
97          ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);                  r = a;
98          ENC_CHECK(!(pParam->width % 2));                  a = b;
99          ENC_CHECK(!(pParam->height % 2));                  b = r;
   
         if (pParam->fincr <= 0 || pParam->fbase <= 0)  
         {  
                 pParam->fincr = 1;  
                 pParam->fbase = 25;  
100          }          }
101    
102          // simplify the "fincr/fbase" fraction          while ((r = a % b)) {
103          // (neccessary, since windows supplies us with huge numbers)                  a = b;
104                    b = r;
         i = pParam->fincr;  
         while (i > 1)  
         {  
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
                         pParam->fincr /= i;  
                         pParam->fbase /= i;  
                         i = pParam->fincr;  
                         continue;  
105                  }                  }
106                  i--;          return b;
107          }          }
108    
109          if (pParam->fbase > 65535)  static void
110    simplify_time(int *inc, int *base)
111          {          {
112                  float div = (float)pParam->fbase / 65535;          /* common factor */
113                  pParam->fbase = (int)(pParam->fbase / div);          const int s = gcd(*inc, *base);
114                  pParam->fincr = (int)(pParam->fincr / div);    *inc  /= s;
115          }    *base /= s;
116    
117            if (*base > 65535 || *inc > 65535) {
118                    int *biggest;
119                    int *other;
120                    float div;
121    
122          if (pParam->rc_bitrate <= 0)                  if (*base > *inc) {
123                  pParam->rc_bitrate = 900000;                          biggest = base;
124                            other = inc;
125                    } else {
126                            biggest = inc;
127                            other = base;
128                    }
129    
130          if (pParam->rc_reaction_delay_factor <= 0)                  div = ((float)*biggest)/((float)65535);
131                  pParam->rc_reaction_delay_factor = 16;                  *biggest = (unsigned int)(((float)*biggest)/div);
132                    *other = (unsigned int)(((float)*other)/div);
133            }
134    }
135    
         if (pParam->rc_averaging_period <= 0)  
                 pParam->rc_averaging_period = 100;  
136    
137          if (pParam->rc_buffer <= 0)  int
138                  pParam->rc_buffer = 100;  enc_create(xvid_enc_create_t * create)
139    {
140            Encoder *pEnc;
141            int n;
142    
143          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
144                  pParam->min_quantizer = 1;                  return XVID_ERR_VERSION;
145    
146          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if (create->width%2 || create->height%2)
147                  pParam->max_quantizer = 31;                  return XVID_ERR_FAIL;
148    
149          if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */          if (create->width<=0 || create->height<=0)
150                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  return XVID_ERR_FAIL;
151    
152          if (pParam->max_quantizer < pParam->min_quantizer)          /* allocate encoder struct */
                 pParam->max_quantizer = pParam->min_quantizer;  
153    
154          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
155            if (pEnc == NULL)
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157            memset(pEnc, 0, sizeof(Encoder));
158    
159          /* Fill members of Encoder structure */          pEnc->mbParam.profile = create->profile;
   
         pEnc->mbParam.width = pParam->width;  
         pEnc->mbParam.height = pParam->height;  
160    
161            /* global flags */
162            pEnc->mbParam.global_flags = create->global;
163      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
164        pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
165    
166            /* width, height */
167            pEnc->mbParam.width = create->width;
168            pEnc->mbParam.height = create->height;
169          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
170          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
   
171          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
172          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
173    
174          pEnc->mbParam.fbase = pParam->fbase;          /* framerate */
175          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = MAX(create->fincr, 0);
176            pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
177            if (pEnc->mbParam.fincr>0)
178                    simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
179    
180          pEnc->sStat.fMvPrevSigma = -1;          /* zones */
181            if(create->num_zones > 0) {
182                    pEnc->num_zones = create->num_zones;
183                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
184                    if (pEnc->zones == NULL)
185                            goto xvid_err_memory0;
186                    memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
187            } else {
188                    pEnc->num_zones = 0;
189                    pEnc->zones = NULL;
190            }
191    
192          /* Fill rate control parameters */          /* plugins */
193            if(create->num_plugins > 0) {
194                    pEnc->num_plugins = create->num_plugins;
195                    pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
196                    if (pEnc->plugins == NULL)
197                            goto xvid_err_memory0;
198            } else {
199                    pEnc->num_plugins = 0;
200                    pEnc->plugins = NULL;
201            }
202    
203          pEnc->bitrate = pParam->rc_bitrate;          for (n=0; n<pEnc->num_plugins;n++) {
204                    xvid_plg_create_t pcreate;
205                    xvid_plg_info_t pinfo;
206    
207          pEnc->iFrameNum = 0;                  memset(&pinfo, 0, sizeof(xvid_plg_info_t));
208          pEnc->iMaxKeyInterval = pParam->max_key_interval;                  pinfo.version = XVID_VERSION;
209                    if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
210                            pEnc->mbParam.plugin_flags |= pinfo.flags;
211                    }
212    
213          /* try to allocate frame memory */                  memset(&pcreate, 0, sizeof(xvid_plg_create_t));
214                    pcreate.version = XVID_VERSION;
215                    pcreate.num_zones = pEnc->num_zones;
216                    pcreate.zones = pEnc->zones;
217                    pcreate.width = pEnc->mbParam.width;
218                    pcreate.height = pEnc->mbParam.height;
219                    pcreate.mb_width = pEnc->mbParam.mb_width;
220                    pcreate.mb_height = pEnc->mbParam.mb_height;
221                    pcreate.fincr = pEnc->mbParam.fincr;
222                    pcreate.fbase = pEnc->mbParam.fbase;
223                    pcreate.param = create->plugins[n].param;
224    
225          pEnc->current = NULL;                  pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
226          pEnc->reference = NULL;                  if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
227          if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||                          pEnc->plugins[n].func = create->plugins[n].func;
228                   (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)                  }
         {  
                 if (pEnc->current) xvid_free(pEnc->current);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
229          }          }
230    
231          /* try to allocate mb memory */          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
232                    (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
233                    pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
234            }
235    
236          pEnc->current->mbs = NULL;          /* temp dquants */
237          pEnc->reference->mbs = NULL;          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
238                    pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
239                                                    pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
240                    if (pEnc->temp_dquants==NULL)
241                            goto xvid_err_memory1a;
242            }
243    
244          if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||          /* temp lambdas */
245                  (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)          if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
246          {                  pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
247                  if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);                                                  pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
248                  xvid_free(pEnc->current);                  if (pEnc->temp_lambda == NULL)
249                  xvid_free(pEnc->reference);                          goto xvid_err_memory1a;
250                  xvid_free(pEnc);          }
251    
252            /* bframes */
253            pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
254            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
255            pEnc->mbParam.bquant_offset = create->bquant_offset;
256    
257            /* min/max quant */
258            for (n=0; n<3; n++) {
259                    pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
260                    pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
261          }          }
262    
263          /* try to allocate image memory */          /* frame drop ratio */
264            pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
265    
266            /* max keyframe interval */
267            pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
268    
269            /* allocate working frame-image memory */
270    
271            pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
272            pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
273    
274            if (pEnc->current == NULL || pEnc->reference == NULL)
275                    goto xvid_err_memory1;
276    
277            /* allocate macroblock memory */
278    
279            pEnc->current->mbs =
280                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
281                                            pEnc->mbParam.mb_height, CACHE_LINE);
282            pEnc->reference->mbs =
283                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
284                                            pEnc->mbParam.mb_height, CACHE_LINE);
285    
286            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
287                    goto xvid_err_memory2;
288    
289  #ifdef _DEBUG          /* allocate quant matrix memory */
290    
291            pEnc->mbParam.mpeg_quant_matrices =
292                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
293    
294            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
295                    goto xvid_err_memory2a;
296    
297            /* allocate interpolation image memory */
298    
299            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
300          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
301  #endif                  image_null(&pEnc->sOriginal2);
302  #ifdef BFRAMES          }
303    
304          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
305          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
306          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
307  #endif  
308          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
309          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
310          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
311          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
312          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
313    
314          if (          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
315  #ifdef _DEBUG                  if (image_create
316                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
317  #endif                           pEnc->mbParam.edged_height) < 0)
318  #ifdef BFRAMES                          goto xvid_err_memory3;
319                  image_create(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
320                  image_create(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  if (image_create
321                  image_create(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                          (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
322  #endif                           pEnc->mbParam.edged_height) < 0)
323                  image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                          goto xvid_err_memory3;
324                  image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          }
325                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
326                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create
327                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
328                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                   pEnc->mbParam.edged_height) < 0)
329                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                  goto xvid_err_memory3;
330          {          if (image_create
331  #ifdef _DEBUG                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
332                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   pEnc->mbParam.edged_height) < 0)
333  #endif                  goto xvid_err_memory3;
334  #ifdef BFRAMES          if (image_create
335                  image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
336                  image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   pEnc->mbParam.edged_height) < 0)
337                  image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  goto xvid_err_memory3;
338  #endif  
339                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create
340                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  (&pEnc->current->image, pEnc->mbParam.edged_width,
341                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   pEnc->mbParam.edged_height) < 0)
342                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  goto xvid_err_memory3;
343                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (image_create
344                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
345                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                   pEnc->mbParam.edged_height) < 0)
346                    goto xvid_err_memory3;
347            if (image_create
348                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
349                     pEnc->mbParam.edged_height) < 0)
350                    goto xvid_err_memory3;
351            if (image_create
352                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
353                     pEnc->mbParam.edged_height) < 0)
354                    goto xvid_err_memory3;
355            if (image_create
356                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
357                     pEnc->mbParam.edged_height) < 0)
358                    goto xvid_err_memory3;
359    
360    /* Create full bitplane for GMC, this might be wasteful */
361            if (image_create
362                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
363                     pEnc->mbParam.edged_height) < 0)
364                    goto xvid_err_memory3;
365    
366                  xvid_free(pEnc->current);          /* init bframe image buffers */
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
367    
368  // ==============================================================================          pEnc->bframenum_head = 0;
369  #ifdef BFRAMES          pEnc->bframenum_tail = 0;
370            pEnc->flush_bframes = 0;
371            pEnc->closed_bframenum = -1;
372    
373          // TODO: handle malloc() == NULL          /* B Frames specific init */
374          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->bframes = NULL;
         pEnc->bquant_ratio = pParam->bquant_ratio;  
         if (pEnc->mbParam.max_bframes > 0)  
         {  
                 int n;  
375    
376                  pEnc->bframes = malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *));          if (pEnc->mbParam.max_bframes > 0) {
377    
378                    pEnc->bframes =
379                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
380                                                    CACHE_LINE);
381    
382                    if (pEnc->bframes == NULL)
383                            goto xvid_err_memory3;
384    
385                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)
386                  {                          pEnc->bframes[n] = NULL;
                         pEnc->bframes[n] = malloc(sizeof(FRAMEINFO));  
                         pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);  
387    
                         if (image_create(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  
                         {  
                                 return XVID_ERR_MEMORY;  
                         }  
                 }  
         }  
         pEnc->bframenum_head = 0;  
         pEnc->bframenum_tail = 0;  
         pEnc->flush_bframes = 0;  
388    
389          pEnc->mbParam.m_seconds = 0;                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
390          pEnc->mbParam.m_ticks = 0;                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
 #endif  
391    
392                            if (pEnc->bframes[n] == NULL)
393                                    goto xvid_err_memory4;
394    
395  // ==============================================================================                          pEnc->bframes[n]->mbs =
396                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
397                                                            pEnc->mbParam.mb_height, CACHE_LINE);
398    
399                            if (pEnc->bframes[n]->mbs == NULL)
400                                    goto xvid_err_memory4;
401    
402          pParam->handle = (void *)pEnc;                          image_null(&pEnc->bframes[n]->image);
403    
404                            if (image_create
405                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
406                                     pEnc->mbParam.edged_height) < 0)
407                                    goto xvid_err_memory4;
408    
409          if (pParam->rc_bitrate)                  }
         {  
                 RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,  
                         pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,  
                         pParam->max_quantizer, pParam->min_quantizer);  
410          }          }
411    
412          init_timer();          /* init incoming frame queue */
413            pEnc->queue_head = 0;
414            pEnc->queue_tail = 0;
415            pEnc->queue_size = 0;
416    
417          return XVID_ERR_OK;          pEnc->queue =
418  }                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
419                                            CACHE_LINE);
420    
421            if (pEnc->queue == NULL)
422                    goto xvid_err_memory4;
423    
424  int encoder_destroy(Encoder * pEnc)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
425  {                  image_null(&pEnc->queue[n].image);
         ENC_CHECK(pEnc);  
426    
427  // =================================================================  
428  #ifdef BFRAMES          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
429          if (pEnc->mbParam.max_bframes > 0)                  if (image_create
430          {                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
431                  int n;                           pEnc->mbParam.edged_height) < 0)
432                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                          goto xvid_err_memory5;
                 {  
                         image_destroy(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                         free(pEnc->bframes[n]->mbs);  
                         free(pEnc->bframes[n]);  
                 }  
                 free(pEnc->bframes);  
433          }          }
 #endif  
 //====================================================================  
434    
435          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* timestamp stuff */
         image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #ifdef BFRAMES  
         image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
         xvid_free(pEnc->current->mbs);  
         xvid_free(pEnc->current);  
436    
437          xvid_free(pEnc->reference->mbs);          pEnc->mbParam.m_stamp = 0;
438          xvid_free(pEnc->reference);          pEnc->m_framenum = 0;
439            pEnc->current->stamp = 0;
440            pEnc->reference->stamp = 0;
441    
442          xvid_free(pEnc);          /* other stuff */
443          return XVID_ERR_OK;  
444            pEnc->iFrameNum = 0;
445            pEnc->fMvPrevSigma = -1;
446    
447            /* multithreaded stuff */
448            if (create->num_threads > 0) {
449                    int t = create->num_threads;
450                    int rows_per_thread = (pEnc->mbParam.mb_height+t-1)/t;
451                    pEnc->num_threads = t;
452                    pEnc->motionData = xvid_malloc(t*sizeof(SMPmotionData), CACHE_LINE);
453                    if (!pEnc->motionData)
454                            goto xvid_err_nosmp;
455    
456                    for (n = 0; n < t; n++) {
457                            pEnc->motionData[n].complete_count_self =
458                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
459    
460                            if (!pEnc->motionData[n].complete_count_self)
461                                    goto xvid_err_nosmp;
462    
463                            if (n != 0)
464                                    pEnc->motionData[n].complete_count_above =
465                                            pEnc->motionData[n-1].complete_count_self;
466                    }
467                    pEnc->motionData[0].complete_count_above =
468                            pEnc->motionData[t-1].complete_count_self - 1;
469    
470            } else {
471      xvid_err_nosmp:
472                    /* no SMP */
473                    create->num_threads = 0;
474                    pEnc->motionData = NULL;
475  }  }
476    
477            create->handle = (void *) pEnc;
478    
479            init_timer();
480            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
481    
482  // ==================================================================          return 0;   /* ok */
 #ifdef BFRAMES  
 int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
483    
484          ENC_CHECK(pEnc);          /*
485          ENC_CHECK(pFrame);           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
486             */
487    
488          start_global_timer();    xvid_err_memory5:
489    
490          BitstreamInit(&bs, pFrame->bitstream, 0);          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
491                            image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
492                                                      pEnc->mbParam.edged_height);
493                    }
494    
495  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          xvid_free(pEnc->queue);
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
496    
497          if ((pFrame->image == NULL || pEnc->flush_bframes) &&    xvid_err_memory4:
                 pEnc->bframenum_head < pEnc->bframenum_tail)  
         {  
498    
499                  if (pEnc->flush_bframes == 0)          if (pEnc->mbParam.max_bframes > 0) {
500                  {                  int i;
                         // we have reached end of stream without getting a future reference  
                         // .. so encode last final frame a pframe  
                         dprintf("--- PFRAME (final frame correction) --- ");  
                         pEnc->bframenum_tail--;  
                         SWAP(pEnc->current, pEnc->reference);  
501    
502                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
503    
504                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          if (pEnc->bframes[i] == NULL)
505                                    continue;
506    
507                          BitstreamPad(&bs);                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
508                          pFrame->length = BitstreamLength(&bs);                                                    pEnc->mbParam.edged_height);
509                          pFrame->input_consumed = 0;                          xvid_free(pEnc->bframes[i]->mbs);
510                          pFrame->intra = 0;                          xvid_free(pEnc->bframes[i]);
511                          return XVID_ERR_OK;                  }
512                  }  
513                    xvid_free(pEnc->bframes);
514            }
515    
516      xvid_err_memory3:
517    
518            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
519                    image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
520                                              pEnc->mbParam.edged_height);
521                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
522                                              pEnc->mbParam.edged_height);
523            }
524    
525            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
526                                      pEnc->mbParam.edged_height);
527            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
528                                      pEnc->mbParam.edged_height);
529            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
530                                      pEnc->mbParam.edged_height);
531    
532            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
533                                      pEnc->mbParam.edged_height);
534            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
535                                      pEnc->mbParam.edged_height);
536            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
537                                      pEnc->mbParam.edged_height);
538            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
539                                      pEnc->mbParam.edged_height);
540            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
541                                      pEnc->mbParam.edged_height);
542    
543    /* destroy GMC image */
544            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
545                                      pEnc->mbParam.edged_height);
546    
547                  dprintf("--- BFRAME (flush) --- ");    xvid_err_memory2a:
548                  FrameCodeB(pEnc,          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
                         pEnc->bframes[pEnc->bframenum_head],  
                          &bs, &bits);  
                 pEnc->bframenum_head++;  
549    
550      xvid_err_memory2:
551            xvid_free(pEnc->current->mbs);
552            xvid_free(pEnc->reference->mbs);
553    
554                  BitstreamPad(&bs);    xvid_err_memory1:
555                  pFrame->length = BitstreamLength(&bs);          xvid_free(pEnc->current);
556                  pFrame->input_consumed = 0;          xvid_free(pEnc->reference);
                 pFrame->intra = 0;  
                 return XVID_ERR_OK;  
         }  
557    
558          if (pFrame->image == NULL)    xvid_err_memory1a:
559          {          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
560                  pFrame->length = 0;                  xvid_free(pEnc->temp_dquants);
                 pFrame->input_consumed = 1;  
                 pFrame->intra = 0;  
                 return XVID_ERR_OK;  
561          }          }
562    
563          if (pEnc->bframenum_head > 0)          if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
564          {                  xvid_free(pEnc->temp_lambda);
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
565          }          }
566    
567          pEnc->flush_bframes = 0;    xvid_err_memory0:
568            for (n=0; n<pEnc->num_plugins;n++) {
569  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  if (pEnc->plugins[n].func) {
570                            pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
571                    }
572            }
573            xvid_free(pEnc->plugins);
574    
575          SWAP(pEnc->current, pEnc->reference);          xvid_free(pEnc->zones);
576    
577          pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(0) : pFrame->quant;          xvid_free(pEnc);
578    
579          if(pEnc->current->quant < 1)          create->handle = NULL;
                 pEnc->current->quant = 1;  
580    
581          if(pEnc->current->quant > 31)          return XVID_ERR_MEMORY;
582                  pEnc->current->quant = 31;  }
583    
584          pEnc->current->global_flags = pFrame->general;  /*****************************************************************************
585          pEnc->current->motion_flags = pFrame->motion;   * Encoder destruction
586          pEnc->current->seconds = pEnc->mbParam.m_seconds;   *
587          pEnc->current->ticks = pEnc->mbParam.m_ticks;   * This function destroy the entire encoder structure created by a previous
588          //@@@ TODO: dyanmic fcode (in both directions)   * successful enc_create call.
589          pEnc->current->fcode = pEnc->mbParam.m_fcode;   *
590          pEnc->current->bcode = pEnc->mbParam.m_fcode;   * Returned values (for now only one returned value) :
591     *      - 0      - no errors
592     *
593     ****************************************************************************/
594    
595          start_timer();  int
596          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  enc_destroy(Encoder * pEnc)
                         pFrame->image, pFrame->colorspace))  
597          {          {
598                  return XVID_ERR_FORMAT;          int i;
599    
600            /* B Frames specific */
601            for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
602                    image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
603                                              pEnc->mbParam.edged_height);
604          }          }
         stop_conv_timer();  
605    
606  #ifdef _DEBUG          xvid_free(pEnc->queue);
         image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);  
 #endif  
607    
608            if (pEnc->mbParam.max_bframes > 0) {
609    
610  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
 // lumi masking  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
611    
612          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                          if (pEnc->bframes[i] == NULL)
613          {                                  continue;
                 int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
614    
615                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
616                                                              pEnc->mbParam.edged_width,  // stride                                            pEnc->mbParam.edged_height);
617                                                              temp_dquants,                          xvid_free(pEnc->bframes[i]->mbs);
618                                                              pEnc->current->quant,                          xvid_free(pEnc->bframes[i]);
619                                                              pEnc->current->quant,       // min_quant                  }
620                                                              2*pEnc->current->quant,     // max_quant  
621                                                              pEnc->mbParam.mb_width,                  xvid_free(pEnc->bframes);
                                                             pEnc->mbParam.mb_height);  
622    
                 for (y = 0; y < pEnc->mbParam.mb_height; y++)  
                         for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                         {  
                                 MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                                 pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];  
623                          }                          }
                 xvid_free(temp_dquants);  
         }  
   
624    
625  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* All images, reference, current etc ... */
626  // ivop/pvop/bvop selection  
627  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
628                                      pEnc->mbParam.edged_height);
629            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
630                                      pEnc->mbParam.edged_height);
631            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
632                                      pEnc->mbParam.edged_height);
633            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
634                                      pEnc->mbParam.edged_height);
635            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
636                                      pEnc->mbParam.edged_height);
637            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
638                                      pEnc->mbParam.edged_height);
639            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
640                                      pEnc->mbParam.edged_height);
641            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
642                                      pEnc->mbParam.edged_height);
643            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
644                                      pEnc->mbParam.edged_height);
645    
646            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
647                    image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
648                                              pEnc->mbParam.edged_height);
649                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
650                                              pEnc->mbParam.edged_height);
651            }
652    
653          if (pEnc->iFrameNum == 0 ||          /* Encoder structure */
                 pFrame->intra == 1 ||  
                 (pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) ||  
                 image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30)  
         {  
                 dprintf("--- IFRAME ---");  
654    
655                  FrameCodeI(pEnc, &bs, &bits);          xvid_free(pEnc->current->mbs);
656            xvid_free(pEnc->current);
657    
658                  pFrame->intra = 1;          xvid_free(pEnc->reference->mbs);
659                  pEnc->flush_bframes = 1;          xvid_free(pEnc->reference);
660    
661                  /* note: sequences like "IIBB" decode fine with msfdam but,          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
662                     go screwy with divx5.00 */                  xvid_free(pEnc->temp_dquants);
663          }          }
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 dprintf("--- PFRAME ---");  
664    
665                  FrameCodeP(pEnc, &bs, &bits, 1, 0);          if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
666                  pFrame->intra = 0;                  xvid_free(pEnc->temp_lambda);
                 pEnc->flush_bframes = 1;  
667          }          }
         else  
         {  
                 dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
668    
669                  if (pFrame->bquant < 1)          if (pEnc->num_plugins>0) {
670                  {                  xvid_plg_destroy_t pdestroy;
671                          pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;                  memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
672    
673                    pdestroy.version = XVID_VERSION;
674                    pdestroy.num_frames = pEnc->m_framenum;
675    
676                    for (i=0; i<pEnc->num_plugins;i++) {
677                            if (pEnc->plugins[i].func) {
678                                    pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
679                  }                  }
680                  else                  }
681                  {                  xvid_free(pEnc->plugins);
                         pEnc->current->quant = pFrame->bquant;  
682                  }                  }
683    
684                  // store frame into bframe buffer & swap ref back to current          xvid_free(pEnc->mbParam.mpeg_quant_matrices);
                 SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
                 SWAP(pEnc->current, pEnc->reference);  
685    
686                  pEnc->bframenum_tail++;          if (pEnc->num_zones > 0)
687                    xvid_free(pEnc->zones);
688    
689                  pFrame->intra = 0;          if (pEnc->num_threads > 0) {
690                  pFrame->length = 0;                  for (i = 0; i < pEnc->num_threads; i++)
691                  pFrame->input_consumed = 1;                          xvid_free(pEnc->motionData[i].complete_count_self);
692    
693                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  xvid_free(pEnc->motionData);
                 if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
                 return XVID_ERR_OK;  
694          }          }
695    
696          BitstreamPad(&bs);          xvid_free(pEnc);
         pFrame->length = BitstreamLength(&bs);  
697    
698          if (pResult)          return 0;  /* ok */
         {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->sStat.kblks;  
                 pResult->mblks = pEnc->sStat.mblks;  
                 pResult->ublks = pEnc->sStat.ublks;  
699          }          }
700    
 #ifdef _DEBUG  
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
701    
702          sprintf(temp, "PSNR: %f\n", psnr);  /*
703          DEBUG(temp);    call the plugins
704  #endif    */
705    
706          if (pFrame->quant == 0)  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
707                                                     int opt, int * type, int * quant, xvid_enc_stats_t * stats)
708          {          {
709                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);          unsigned int i, j, k;
710            xvid_plg_data_t data;
711    
712            /* set data struct */
713    
714            memset(&data, 0, sizeof(xvid_plg_data_t));
715            data.version = XVID_VERSION;
716    
717            /* find zone */
718            for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
719            data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
720    
721            data.width = pEnc->mbParam.width;
722            data.height = pEnc->mbParam.height;
723            data.mb_width = pEnc->mbParam.mb_width;
724            data.mb_height = pEnc->mbParam.mb_height;
725            data.fincr = frame->fincr;
726            data.fbase = pEnc->mbParam.fbase;
727            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
728            data.bquant_offset = pEnc->mbParam.bquant_offset;
729    
730            for (i=0; i<3; i++) {
731                    data.min_quant[i] = pEnc->mbParam.min_quant[i];
732                    data.max_quant[i] = pEnc->mbParam.max_quant[i];
733          }          }
734    
735          pEnc->iFrameNum++;          data.reference.csp = XVID_CSP_PLANAR;
736          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;          data.reference.plane[0] = pEnc->reference->image.y;
737          if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)          data.reference.plane[1] = pEnc->reference->image.u;
738          {          data.reference.plane[2] = pEnc->reference->image.v;
739                  pEnc->mbParam.m_seconds++;          data.reference.stride[0] = pEnc->mbParam.edged_width;
740                  pEnc->mbParam.m_ticks = 0;          data.reference.stride[1] = pEnc->mbParam.edged_width/2;
741            data.reference.stride[2] = pEnc->mbParam.edged_width/2;
742    
743            data.current.csp = XVID_CSP_PLANAR;
744            data.current.plane[0] = frame->image.y;
745            data.current.plane[1] = frame->image.u;
746            data.current.plane[2] = frame->image.v;
747            data.current.stride[0] = pEnc->mbParam.edged_width;
748            data.current.stride[1] = pEnc->mbParam.edged_width/2;
749            data.current.stride[2] = pEnc->mbParam.edged_width/2;
750    
751            data.frame_num = frame->frame_num;
752    
753            if (opt == XVID_PLG_BEFORE) {
754                    data.type = *type;
755                    data.quant = *quant;
756    
757                    data.vol_flags = frame->vol_flags;
758                    data.vop_flags = frame->vop_flags;
759                    data.motion_flags = frame->motion_flags;
760    
761            } else if (opt == XVID_PLG_FRAME) {
762                    data.type = coding2type(frame->coding_type);
763                    data.quant = frame->quant;
764    
765                    if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
766                            data.dquant = pEnc->temp_dquants;
767                            data.dquant_stride = pEnc->mbParam.mb_width;
768                            memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
769          }          }
         pFrame->input_consumed = 1;  
770    
771          stop_global_timer();                  if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
772          write_timer();                          int block = 0;
773                            data.lambda = pEnc->temp_lambda;
774                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
775                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
776                                            for (k = 0; k < 6; k++)
777                                                    data.lambda[block++] = 1.0f;
778                    }
779    
780          return XVID_ERR_OK;          } else { /* XVID_PLG_AFTER */
781                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
782                            data.original.csp = XVID_CSP_PLANAR;
783                            data.original.plane[0] = original->y;
784                            data.original.plane[1] = original->u;
785                            data.original.plane[2] = original->v;
786                            data.original.stride[0] = pEnc->mbParam.edged_width;
787                            data.original.stride[1] = pEnc->mbParam.edged_width/2;
788                            data.original.stride[2] = pEnc->mbParam.edged_width/2;
789  }  }
 // ==================================================================  
 #else  
 int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
         uint16_t write_vol_header = 0;  
 #ifdef _DEBUG  
         float psnr;  
         uint8_t temp[100];  
 #endif  
790    
791          start_global_timer();                  if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
792                            (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
793    
794          ENC_CHECK(pEnc);                          data.sse_y =
795          ENC_CHECK(pFrame);                                  plane_sse( original->y, frame->image.y,
796          ENC_CHECK(pFrame->bitstream);                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
797          ENC_CHECK(pFrame->image);                                                     pEnc->mbParam.height);
   
         SWAP(pEnc->current, pEnc->reference);  
   
         pEnc->current->global_flags = pFrame->general;  
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->mbParam.hint = &pFrame->hint;  
798    
799          start_timer();                          data.sse_u =
800          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,                                  plane_sse( original->u, frame->image.u,
801                          pFrame->image, pFrame->colorspace))                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
802          {                                                     pEnc->mbParam.height/2);
                 return XVID_ERR_FORMAT;  
         }  
         stop_conv_timer();  
803    
804  #ifdef _DEBUG                          data.sse_v =
805          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                                  plane_sse( original->v, frame->image.v,
806  #endif                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
807                                                       pEnc->mbParam.height/2);
808                    }
809    
810          EMMS();                  data.type = coding2type(frame->coding_type);
811                    data.quant = frame->quant;
812    
813          BitstreamInit(&bs, pFrame->bitstream, 0);                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
814                            data.dquant = pEnc->temp_dquants;
815                            data.dquant_stride = pEnc->mbParam.mb_width;
816    
817          if (pFrame->quant == 0)                          for (j=0; j<pEnc->mbParam.mb_height; j++)
818          {                          for (i=0; i<pEnc->mbParam.mb_width; i++) {
819                  pEnc->current->quant = RateControlGetQ(0);                                  data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
820          }          }
         else  
         {  
                 pEnc->current->quant = pFrame->quant;  
821          }          }
822    
823          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  data.vol_flags = frame->vol_flags;
824          {                  data.vop_flags = frame->vop_flags;
825                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                  data.motion_flags = frame->motion_flags;
826    
827                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  data.length = frame->length;
828                                                              pEnc->mbParam.edged_width,  // stride                  data.kblks = frame->sStat.kblks;
829                                                              temp_dquants,                  data.mblks = frame->sStat.mblks;
830                                                              pEnc->current->quant,                  data.ublks = frame->sStat.ublks;
                                                             pEnc->current->quant,       // min_quant  
                                                             2*pEnc->current->quant,     // max_quant  
                                                             pEnc->mbParam.mb_width,  
                                                             pEnc->mbParam.mb_height);  
831    
832                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  /* New code */
833                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                  data.stats.type      = coding2type(frame->coding_type);
834                          {                  data.stats.quant     = frame->quant;
835                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                  data.stats.vol_flags = frame->vol_flags;
836                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                  data.stats.vop_flags = frame->vop_flags;
837                          }                  data.stats.length    = frame->length;
838                  xvid_free(temp_dquants);                  data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
839                    data.stats.kblks     = frame->sStat.kblks;
840                    data.stats.mblks     = frame->sStat.mblks;
841                    data.stats.ublks     = frame->sStat.ublks;
842                    data.stats.sse_y     = data.sse_y;
843                    data.stats.sse_u     = data.sse_u;
844                    data.stats.sse_v     = data.sse_v;
845    
846                    if (stats)
847                            *stats = data.stats;
848          }          }
849    
850          if (pEnc->current->global_flags & XVID_H263QUANT) {          /* call plugins */
851                  if(pEnc->mbParam.m_quant_type != H263_QUANT)          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
852                          write_vol_header = 1;                  emms();
853                  pEnc->mbParam.m_quant_type = H263_QUANT;                  if (pEnc->plugins[i].func) {
854                            if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
855                                    continue;
856                            }
857          }          }
858          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {          }
859                  int ret1, ret2;          emms();
860    
861                  ret1 = ret2 = 0;          /* copy modified values back into frame*/
862            if (opt == XVID_PLG_BEFORE) {
863                    *type = data.type;
864                    *quant = data.quant > 0 ? data.quant : 2;   /* default */
865    
866                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  frame->vol_flags = data.vol_flags;
867                          write_vol_header = 1;                  frame->vop_flags = data.vop_flags;
868                    frame->motion_flags = data.motion_flags;
869    
870                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;          } else if (opt == XVID_PLG_FRAME) {
871    
872                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
873                          if(pFrame->quant_intra_matrix != NULL)                          for (j=0; j<pEnc->mbParam.mb_height; j++)
874                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                          for (i=0; i<pEnc->mbParam.mb_width; i++) {
875                          if(pFrame->quant_inter_matrix != NULL)                                  frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
                                 ret2 = set_inter_matrix(pFrame->quant_inter_matrix);  
876                  }                  }
877                  else {                  } else {
878                          ret1 = set_intra_matrix(get_default_intra_matrix());                          for (j=0; j<pEnc->mbParam.mb_height; j++)
879                          ret2 = set_inter_matrix(get_default_inter_matrix());                          for (i=0; i<pEnc->mbParam.mb_width; i++) {
880                                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
881                  }                  }
                 if(write_vol_header == 0)  
                         write_vol_header = ret1 | ret2;  
882          }          }
883    
884          if (pFrame->intra < 0)                  if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
885          {                          for (j = 0; j < pEnc->mbParam.mb_height; j++)
886                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                                  for (i = 0; i < pEnc->mbParam.mb_width; i++)
887                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                          for (k = 0; k < 6; k++) {
888                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
889                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
890                            }
891                    } else {
892                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
893                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
894                                            for (k = 0; k < 6; k++) {
895                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
896                            }
897                    }
898    
899                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
900                  else                  frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);  
901          }          }
902          else  
903          {  
                 if (pFrame->intra == 1)  
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 else  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);  
904          }          }
905    
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPad(&bs);  
         pFrame->length = BitstreamLength(&bs);  
906    
907          if (pResult)  static __inline void inc_frame_num(Encoder * pEnc)
908          {          {
909                  pResult->quant = pEnc->current->quant;          pEnc->current->frame_num = pEnc->m_framenum;
910                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
911                  pResult->kblks = pEnc->sStat.kblks;  
912                  pResult->mblks = pEnc->sStat.mblks;          pEnc->mbParam.m_stamp += pEnc->current->fincr;
913                  pResult->ublks = pEnc->sStat.ublks;          pEnc->m_framenum++;     /* debug ticker */
914          }          }
915    
916          EMMS();  static __inline void dec_frame_num(Encoder * pEnc)
917    {
918            pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
919            pEnc->m_framenum--;     /* debug ticker */
920    }
921    
922          if (pFrame->quant == 0)  static __inline void
923    MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
924          {          {
925                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);          if (pMB->cbp == 0) {
926                    /* we want to code dquant but the quantizer value will not be used yet
927                            let's find out if we can postpone dquant to next MB
928                    */
929                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
930                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
931                            return;
932                    } else {
933                            MACROBLOCK * next = pMB + 1;
934                            const MACROBLOCK * prev = pMB - 1;
935                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
936                                    /* mode allows dquant change in the future */
937                                    if (abs(next->quant - prev->quant) <= 2) {
938                                            /* quant change is not out of range */
939                                            pMB->quant = prev->quant;
940                                            pMB->dquant = 0;
941                                            next->dquant = next->quant - prev->quant;
942                                            return;
943                                    }
944                    }
945            }
946            /* couldn't skip this dquant */
947            pMB->mode = MODE_INTER_Q;
948          }          }
949    
 #ifdef _DEBUG  
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
950    
         sprintf(temp, "PSNR: %f\n", psnr);  
         DEBUG(temp);  
 #endif  
951    
952          pEnc->iFrameNum++;  static __inline void
953    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
954    {
955    
956          stop_global_timer();          pCur->ticks = (int32_t)pCur->stamp % time_base;
957          write_timer();          pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
958    
959          return XVID_ERR_OK;  #if 0   /* HEAVY DEBUG OUTPUT */
960  }          fprintf(stderr,"WriteVop:   %d - %d \n",
961                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
962            fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
963                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
964            fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
965                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
966  #endif  #endif
967    }
968    
969    static void
970    simplify_par(int *par_width, int *par_height)
971    {
972    
973  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {          int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
974            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
975            int divisor = gcd(_par_width, _par_height);
976    
977          pMB->mode = MODE_INTRA;          _par_width  /= divisor;
978            _par_height /= divisor;
979    
980          /* zero mv statistics */          /* 2^8 precision maximum */
981          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;          if (_par_width>255 || _par_height>255) {
982          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;                  float div;
983          pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;                  emms();
984          pMB->sad16 = 0;                  if (_par_width>_par_height)
985                            div = (float)_par_width/255;
986          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  else
987                  if(pMB->dquant != NO_CHANGE)                          div = (float)_par_height/255;
                 {  
                         pMB->mode = MODE_INTRA_Q;  
                         pEnc->current->quant += DQtab[pMB->dquant];  
988    
989                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                  _par_width  = (int)((float)_par_width/div);
990                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                  _par_height = (int)((float)_par_height/div);
                 }  
991          }          }
992    
993          pMB->quant = pEnc->current->quant;          *par_width = _par_width;
994  }          *par_height = _par_height;
995    
996            return;
997    }
998    
 #define FCODEBITS       3  
 #define MODEBITS        5  
999    
1000  void HintedMESet(Encoder * pEnc, int * intra)  /*****************************************************************************
1001     * IPB frame encoder entry point
1002     *
1003     * Returned values :
1004     *      - >0                       - output bytes
1005     *      - 0                             - no output
1006     *      - XVID_ERR_VERSION - wrong version passed to core
1007     *      - XVID_ERR_END   - End of stream reached before end of coding
1008     *      - XVID_ERR_FORMAT  - the image subsystem reported the image had a wrong
1009     *                                               format
1010     ****************************************************************************/
1011    
1012    
1013    int
1014    enc_encode(Encoder * pEnc,
1015                               xvid_enc_frame_t * xFrame,
1016                               xvid_enc_stats_t * stats)
1017  {  {
1018          HINTINFO * hint;          xvid_enc_frame_t * frame;
1019            int type;
1020          Bitstream bs;          Bitstream bs;
         int length, high;  
         uint32_t x, y;  
1021    
1022          hint = pEnc->mbParam.hint;          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
1023                    return XVID_ERR_VERSION;
1024    
1025          if (hint->rawhints)          xFrame->out_flags = 0;
1026          {  
1027                  *intra = hint->mvhint.intra;          start_global_timer();
1028          }          BitstreamInit(&bs, xFrame->bitstream, 0);
1029          else  
1030    
1031            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1032             * enqueue image to the encoding-queue
1033             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1034    
1035            if (xFrame->input.csp != XVID_CSP_NULL)
1036          {          {
1037                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
                 *intra = BitstreamGetBit(&bs);  
         }  
1038    
1039          if (*intra)                  start_timer();
1040                    if (image_input
1041                            (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
1042                            pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
1043                            xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
1044          {          {
1045                  return;                          emms();
1046                            return XVID_ERR_FORMAT;
1047          }          }
1048                    stop_conv_timer();
1049    
1050          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1051                            image_chroma_optimize(&q->image,
1052                                    pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1053                    }
1054    
1055          length  = pEnc->current->fcode + 5;                  q->frame = *xFrame;
         high    = 1 << (length - 1);  
1056    
1057          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)                  if (xFrame->quant_intra_matrix)
1058          {          {
1059                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
1060                            q->frame.quant_intra_matrix = q->quant_intra_matrix;
1061                    }
1062    
1063                    if (xFrame->quant_inter_matrix)
1064                  {                  {
1065                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
1066                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
1067                          VECTOR pred[4];                  }
                         VECTOR tmp;  
                         int32_t dummy[4];  
                         int vec;  
1068    
1069                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                  pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
1070                    pEnc->queue_size++;
1071            }
1072    
                         pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;  
                         pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;  
1073    
1074                          if (pMB->mode == MODE_INTER)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1075                          {           * bframe flush code
1076                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                                 tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);  
                                 tmp.x -= (tmp.x >= high) ? high*2 : 0;  
                                 tmp.y -= (tmp.y >= high) ? high*2 : 0;  
1077    
1078                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);  repeat:
1079    
1080                                  for (vec=0 ; vec<4 ; ++vec)          if (pEnc->flush_bframes)
1081                                  {                                  {
1082                                          pMB->mvs[vec].x  = tmp.x;                  if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1083                                          pMB->mvs[vec].y  = tmp.y;  
1084                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                          DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1085                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pEnc->bframenum_head, pEnc->bframenum_tail,
1086                                            pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1087    
1088                            if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1089                                    image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
1090                                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1091                                  }                                  }
1092    
1093                            FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1094                            call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1095                            pEnc->bframenum_head++;
1096    
1097                            goto done;
1098                          }                          }
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
                                 {  
                                         tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);  
                                         tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);  
                                         tmp.x -= (tmp.x >= high) ? high*2 : 0;  
                                         tmp.y -= (tmp.y >= high) ? high*2 : 0;  
1099    
1100                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                  /* write an empty marker to the bitstream.
1101    
1102                       for divx5 decoder compatibility, this marker must consist
1103                       of a not-coded p-vop, with a time_base of zero, and time_increment
1104                       indentical to the future-referece frame.
1105                    */
1106    
1107                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
1108                            int tmp;
1109                            int bits;
1110    
1111                            DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1112                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1113                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1114    
1115                            bits = BitstreamPos(&bs);
1116    
1117                            tmp = pEnc->current->seconds;
1118                            pEnc->current->seconds = 0; /* force time_base = 0 */
1119    
1120                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1121                            BitstreamPad(&bs);
1122                            pEnc->current->seconds = tmp;
1123    
1124                            /* add the not-coded length to the reference frame size */
1125                            pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1126                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1127    
1128                            /* flush complete: reset counters */
1129                            pEnc->flush_bframes = 0;
1130                            pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1131                            goto done;
1132    
                                         pMB->mvs[vec].x  = tmp.x;  
                                         pMB->mvs[vec].y  = tmp.y;  
                                         pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;  
                                         pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;  
1133                                  }                                  }
1134    
1135                    /* flush complete: reset counters */
1136                    pEnc->flush_bframes = 0;
1137                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1138                          }                          }
1139                          else    // intra / stuffing / not_coded  
1140            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1141             * dequeue frame from the encoding queue
1142             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1143    
1144            if (pEnc->queue_size == 0)              /* empty */
1145                          {                          {
1146                                  for (vec=0 ; vec<4 ; ++vec)                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1147                                  {                                  {
1148                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;  
1149                            DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1150                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1151                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1152    
1153                            if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1154                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1155                                  }                                  }
1156    
1157                            /* if the very last frame is to be b-vop, we must change it to a p-vop */
1158                            if (pEnc->bframenum_tail > 0) {
1159    
1160                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1161                                    pEnc->bframenum_tail--;
1162                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1163    
1164                                    /* convert B-VOP to P-VOP */
1165                                    pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1166                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1167                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1168    
1169                                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1170                                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1171                                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1172                          }                          }
1173    
1174                          if (pMB->mode == MODE_INTER4V &&                                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1175                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1176                          {                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1177                                  pMB->mode = MODE_INTRA;                                  pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1178    
1179                                  for (vec=0 ; vec<4 ; ++vec)                                  FrameCodeP(pEnc, &bs);
1180                                  {  
1181                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;  
1182                                    if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1183                                            call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1184                                    }else{
1185                                            pEnc->flush_bframes = 1;
1186                                            goto done;
1187                                  }                                  }
1188                          }                          }
1189                            DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1190    
1191                            emms();
1192                            return XVID_ERR_END;    /* end of stream reached */
1193                  }                  }
1194                    goto done;      /* nothing to encode yet; encoder lag */
1195          }          }
1196    
1197            /* the current FRAME becomes the reference */
1198            SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1199    
1200            /* remove frame from encoding-queue (head), and move it into the current */
1201            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1202            frame = &pEnc->queue[pEnc->queue_head].frame;
1203            pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
1204            pEnc->queue_size--;
1205    
1206    
1207            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1208             * init pEnc->current fields
1209             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1210    
1211            pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1212            inc_frame_num(pEnc);
1213            pEnc->current->vol_flags = frame->vol_flags;
1214            pEnc->current->vop_flags = frame->vop_flags;
1215            pEnc->current->motion_flags = frame->motion;
1216            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1217            pEnc->current->bcode = pEnc->mbParam.m_fcode;
1218    
1219    
1220            if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1221                    image_chroma_optimize(&pEnc->current->image,
1222                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1223  }  }
1224    
1225            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1226             * frame type & quant selection
1227             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1228    
1229  void HintedMEGet(Encoder * pEnc, int intra)          type = frame->type;
1230  {          pEnc->current->quant = frame->quant;
         HINTINFO * hint;  
         Bitstream bs;  
         uint32_t x, y;  
         int length, high;  
1231    
1232          hint = pEnc->mbParam.hint;          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
1233    
1234          if (hint->rawhints)          if (type > 0){  /* XVID_TYPE_?VOP */
1235          {                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1236                  hint->mvhint.intra = intra;          } else{         /* XVID_TYPE_AUTO */
1237                    if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
1238                            pEnc->iFrameNum = 0;
1239                            type = I_VOP;
1240                    }else{
1241                            type = MEanalysis(&pEnc->reference->image, pEnc->current,
1242                                                              &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1243                                                              pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1244                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1245          }          }
         else  
         {  
                 BitstreamInit(&bs, hint->hintstream, 0);  
                 BitstreamPutBit(&bs, intra);  
1246          }          }
1247    
1248          if (intra)          if (type != I_VOP)
1249          {                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
1250                  if (!hint->rawhints)  
1251                  {          /* bframes buffer overflow check */
1252                          BitstreamPad(&bs);          if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1253                          hint->hintlength = BitstreamLength(&bs);                  type = P_VOP;
1254                  }                  }
1255                  return;  
1256            pEnc->iFrameNum++;
1257    
1258            if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1259                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1260                            "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1261            }
1262    
1263            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1264             * encode this frame as a b-vop
1265             * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1266             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1267            if (type == B_VOP) {
1268                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1269                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1270                    }
1271    
1272                    if (frame->quant < 1) {
1273                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1274                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1275    
1276                    } else {
1277                            pEnc->current->quant = frame->quant;
1278          }          }
1279    
1280          length  = pEnc->current->fcode + 5;                  if (pEnc->current->quant < 1)
1281          high    = 1 << (length - 1);                          pEnc->current->quant = 1;
1282                    else if (pEnc->current->quant > 31)
1283                            pEnc->current->quant = 31;
1284    
1285                    DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1286                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1287                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1288    
1289                    /* store frame into bframe buffer & swap ref back to current */
1290                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1291                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1292    
1293                    pEnc->bframenum_tail++;
1294    
1295                    goto repeat;
1296            }
1297    
1298          if (hint->rawhints)  
1299                    DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1300                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1301                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1302    
1303            /* for unpacked bframes, output the stats for the last encoded frame */
1304            if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1305          {          {
1306                  hint->mvhint.fcode = pEnc->current->fcode;                  if (pEnc->current->stamp > 0) {
1307                            call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1308          }          }
1309          else          else
1310          {                          stats->type = XVID_TYPE_NOTHING;
                 BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);  
1311          }          }
1312    
1313          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314          {           * closed-gop
1315                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)           * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1316                  {           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];  
                         VECTOR tmp;  
1317    
1318                          if (hint->rawhints)          if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
1319                          {  
1320                                  bhint->mode = pMB->mode;                  /* place this frame back on the encoding-queue (head) */
1321                    /* we will deal with it next time */
1322                    dec_frame_num(pEnc);
1323                    pEnc->iFrameNum--;
1324    
1325                    pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1326                    pEnc->queue_size++;
1327                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1328    
1329                    /* grab the last frame from the bframe-queue */
1330    
1331                    pEnc->bframenum_tail--;
1332                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1333    
1334                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1335                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1336                          }                          }
1337                          else  
1338                          {                  /* convert B-VOP quant to P-VOP */
1339                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1340                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1341                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1342                    type = P_VOP;
1343                          }                          }
1344    
                         if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)  
                         {  
                                 tmp.x  = pMB->mvs[0].x;  
                                 tmp.y  = pMB->mvs[0].y;  
                                 tmp.x += (tmp.x < 0) ? high*2 : 0;  
                                 tmp.y += (tmp.y < 0) ? high*2 : 0;  
1345    
1346                                  if (hint->rawhints)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1347                                  {           * encode this frame as an i-vop
1348                                          bhint->mvs[0].x = tmp.x;           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1349                                          bhint->mvs[0].y = tmp.y;  
1350            if (type == I_VOP) {
1351    
1352                    DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1353                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1354                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1355    
1356                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1357                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1358                                  }                                  }
1359                                  else  
1360                                  {                  pEnc->iFrameNum = 1;
1361                                          BitstreamPutBits(&bs, tmp.x, length);  
1362                                          BitstreamPutBits(&bs, tmp.y, length);                  /* ---- update vol flags at IVOP ----------- */
1363                    pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
1364    
1365                    /* Aspect ratio */
1366                    switch(frame->par) {
1367                    case XVID_PAR_11_VGA:
1368                    case XVID_PAR_43_PAL:
1369                    case XVID_PAR_43_NTSC:
1370                    case XVID_PAR_169_PAL:
1371                    case XVID_PAR_169_NTSC:
1372                    case XVID_PAR_EXT:
1373                            pEnc->mbParam.par = frame->par;
1374                            break;
1375                    default:
1376                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1377                            break;
1378                                  }                                  }
1379    
1380                    /* For extended PAR only, we try to sanityse/simplify par values */
1381                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1382                            pEnc->mbParam.par_width  = frame->par_width;
1383                            pEnc->mbParam.par_height = frame->par_height;
1384                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1385                          }                          }
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
                                 int vec;  
1386    
1387                                  for (vec=0 ; vec<4 ; ++vec)                  if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1388                                  {                          if (frame->quant_intra_matrix != NULL)
1389                                          tmp.x  = pMB->mvs[vec].x;                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1390                                          tmp.y  = pMB->mvs[vec].y;                          if (frame->quant_inter_matrix != NULL)
1391                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                  set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1392                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                  }
1393    
1394                                          if (hint->rawhints)                  /* prevent vol/vop misuse */
1395                                          {  
1396                                                  bhint->mvs[vec].x = tmp.x;                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1397                                                  bhint->mvs[vec].y = tmp.y;                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
1398    
1399                    /* ^^^------------------------ */
1400    
1401                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1402                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1403                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1404                                          }                                          }
1405                                          else  
1406                                          {                  FrameCodeI(pEnc, &bs);
1407                                                  BitstreamPutBits(&bs, tmp.x, length);                  xFrame->out_flags |= XVID_KEYFRAME;
1408                                                  BitstreamPutBits(&bs, tmp.y, length);  
1409            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410             * encode this frame as an p-vop
1411             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1412    
1413            } else { /* (type == P_VOP || type == S_VOP) */
1414    
1415                    DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1416                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1417                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1418    
1419                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1420                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1421                    }
1422    
1423                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1424                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1425                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1426                                          }                                          }
1427    
1428                    if ( FrameCodeP(pEnc, &bs) == 0 ) {
1429                            /* N-VOP, we mustn't code b-frames yet */
1430                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1431                                     pEnc->mbParam.max_bframes == 0)
1432                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1433                            goto done;
1434                                  }                                  }
1435                          }                          }
1436    
1437    
1438            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1439             * on next enc_encode call we must flush bframes
1440             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1441    
1442    /*done_flush:*/
1443    
1444            pEnc->flush_bframes = 1;
1445    
1446            /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1447            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1448                    goto repeat;
1449                  }                  }
1450    
1451            /* packed or no-bframes or no-bframes-queued: output stats */
1452            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1453                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1454          }          }
1455    
1456          if (!hint->rawhints)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457             * done; return number of bytes consumed
1458             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1459    
1460    done:
1461    
1462            stop_global_timer();
1463            write_timer();
1464    
1465            emms();
1466            return BitstreamLength(&bs);
1467    }
1468    
1469    
1470    static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1471          {          {
1472                  BitstreamPad(&bs);          unsigned int i;
1473                  hint->hintlength = BitstreamLength(&bs);          MACROBLOCK * pMB = frame->mbs;
1474            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1475            if (quant > 31)
1476                    frame->quant = quant = 31;
1477            else if (quant < 1)
1478                    frame->quant = quant = 1;
1479    
1480            for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
1481                    quant += pMB->dquant;
1482                    if (quant > 31)
1483                            quant = 31;
1484                    else if (quant < 1)
1485                            quant = 1;
1486                    pMB->quant = quant;
1487                    pMB++;
1488          }          }
1489  }  }
1490    
1491    
1492  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static __inline void
1493    CodeIntraMB(Encoder * pEnc,
1494                            MACROBLOCK * pMB)
1495  {  {
1496    
1497            pMB->mode = MODE_INTRA;
1498    
1499            /* zero mv statistics */
1500            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1501            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1502            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1503            pMB->sad16 = 0;
1504    
1505            if (pMB->dquant != 0) {
1506                    pMB->mode = MODE_INTRA_Q;
1507            }
1508    }
1509    
1510    
1511    
1512    static int
1513    FrameCodeI(Encoder * pEnc,
1514                       Bitstream * bs)
1515    {
1516            int bits = BitstreamPos(bs);
1517            int mb_width = pEnc->mbParam.mb_width;
1518            int mb_height = pEnc->mbParam.mb_height;
1519    
1520          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1521          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1522    
1523          uint16_t x, y;          uint16_t x, y;
1524    
         pEnc->iFrameNum = 0;  
1525          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1526          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1527          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1528    
1529            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1530    
1531            SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1532    
1533          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1534    
1535          *pBits = BitstreamPos(bs);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1536    
1537          pEnc->sStat.iTextBits = 0;          BitstreamPad(bs);
         pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;  
         pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1538    
1539          for (y = 0; y < pEnc->mbParam.mb_height; y++)          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1540                  for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1541                  {          pEnc->current->sStat.iTextBits = 0;
1542                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];          pEnc->current->sStat.iMVBits = 0;
1543            pEnc->current->sStat.kblks = mb_width * mb_height;
1544            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1545    
1546            for (y = 0; y < mb_height; y++)
1547                    for (x = 0; x < mb_width; x++) {
1548                            MACROBLOCK *pMB =
1549                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1550    
1551                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1552    
1553                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1554                                                              dct_codes, qcoeff);
1555    
1556                          start_timer();                          start_timer();
1557                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1558                          stop_prediction_timer();                          stop_prediction_timer();
1559    
1560                          start_timer();                          start_timer();
1561                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1562                          stop_coding_timer();                          stop_coding_timer();
1563                  }                  }
1564    
1565          emms();          emms();
1566    
1567          *pBits = BitstreamPos(bs) - *pBits;          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1568          pEnc->sStat.fMvPrevSigma = -1;  
1569          pEnc->sStat.iMvSum = 0;          pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1570          pEnc->sStat.iMvCount = 0;  
1571            pEnc->fMvPrevSigma = -1;
1572          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1573    
1574          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          pEnc->current->is_edged = 0; /* not edged */
1575          {          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
                 HintedMEGet(pEnc, 1);  
         }  
1576    
1577          return 1;                                        // intra          return 1;                                       /* intra */
1578  }  }
1579    
1580    static __inline void
1581    updateFcode(Statistics * sStat, Encoder * pEnc)
1582    {
1583            float fSigma;
1584            int iSearchRange;
1585    
1586            if (sStat->iMvCount == 0)
1587                    sStat->iMvCount = 1;
1588    
1589            fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1590    
1591  #define INTRA_THRESHOLD 0.5          iSearchRange = 16 << pEnc->mbParam.m_fcode;
1592    
1593  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)          if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1594                    pEnc->mbParam.m_fcode++;
1595    
1596            else if ((5.0 * fSigma < iSearchRange)
1597                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1598                               && (pEnc->mbParam.m_fcode >= 2) )
1599                    pEnc->mbParam.m_fcode--;
1600    
1601            pEnc->fMvPrevSigma = fSigma;
1602    }
1603    
1604    #define BFRAME_SKIP_THRESHHOLD 30
1605    
1606    /* FrameCodeP also handles S(GMC)-VOPs */
1607    static int
1608    FrameCodeP(Encoder * pEnc,
1609                       Bitstream * bs)
1610  {  {
1611          float fSigma;          int bits = BitstreamPos(bs);
1612    
1613          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1614          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1615    
1616          int iLimit;          int x, y, k;
1617          uint32_t x, y;          FRAMEINFO *const current = pEnc->current;
1618          int iSearchRange;          FRAMEINFO *const reference = pEnc->reference;
1619          int bIntra;          MBParam * const pParam = &pEnc->mbParam;
1620            int mb_width = pParam->mb_width;
1621            int mb_height = pParam->mb_height;
1622            int coded = 1;
1623    
1624          /* IMAGE *pCurrent = &pEnc->current->image; */          IMAGE *pRef = &reference->image;
         IMAGE *pRef = &pEnc->reference->image;  
1625    
1626            if (!reference->is_edged) {
1627          start_timer();          start_timer();
1628          image_setedges(pRef,                  image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1629                         pEnc->mbParam.edged_width,                                             pParam->width, pParam->height, 0);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->current->global_flags & XVID_INTERLACING);  
1630          stop_edges_timer();          stop_edges_timer();
1631                    reference->is_edged = 1;
1632            }
1633    
1634          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1635          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          current->rounding_type = pParam->m_rounding_type;
1636          pEnc->current->fcode = pEnc->mbParam.m_fcode;          current->fcode = pParam->m_fcode;
   
         if (!force_inter)  
                 iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);  
         else  
                 iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;  
1637    
1638          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1639                    if (reference->is_interpolated != current->rounding_type) {
1640                  start_timer();                  start_timer();
1641                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                          image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1642                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                            pEnc->vInterHV.y, pParam->edged_width,
1643                                    pEnc->current->rounding_type);                                                            pParam->edged_height,
1644                                                              (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1645                                                              current->rounding_type);
1646                  stop_inter_timer();                  stop_inter_timer();
1647                            reference->is_interpolated = current->rounding_type;
1648                    }
1649          }          }
1650    
1651            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1652                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1653                    current->sStat.iMVBits = 0;
1654    
1655            current->coding_type = P_VOP;
1656    
1657            call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1658    
1659            SetMacroblockQuants(&pEnc->mbParam, current);
1660    
1661          start_timer();          start_timer();
1662          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
1663          {          {       int gmcval;
1664                  HintedMESet(pEnc, &bIntra);                  current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1665          }                                                                   &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1666          else  
1667          {                  if (current->motion_flags & XVID_ME_GME_REFINE) {
1668                  bIntra = MotionEstimation(                          gmcval = GlobalMotionEstRefine(&current->warp,
1669                          &pEnc->mbParam,                                                                                     current->mbs, pParam,
1670                          pEnc->current,                                                                                     current, reference,
1671                          pEnc->reference,                                                                                     &current->image,
1672                                                                                       &reference->image,
1673                          &pEnc->vInterH,                          &pEnc->vInterH,
1674                          &pEnc->vInterV,                          &pEnc->vInterV,
1675                          &pEnc->vInterHV,                                                                                     &pEnc->vInterHV);
1676                          iLimit);                  } else {
1677                            gmcval = globalSAD(&current->warp, pParam, current->mbs,
1678                                                               current,
1679                                                               &reference->image,
1680                                                               &current->image,
1681                                                               pEnc->vGMC.y);
1682          }          }
         stop_motion_timer();  
1683    
1684          if (bIntra == 1)                  gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1685    
1686                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1687                    generate_GMCparameters( 3, 3, &current->warp,
1688                                    pParam->width, pParam->height,
1689                                    &current->new_gmc_data);
1690    
1691                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
1692                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
1693          {          {
1694                  return FrameCodeI(pEnc, bs, pBits);                          current->coding_type = S_VOP;
         }  
1695    
1696          pEnc->current->coding_type = P_VOP;                          generate_GMCimage(&current->new_gmc_data, &reference->image,
1697                                    pParam->mb_width, pParam->mb_height,
1698                                    pParam->edged_width, pParam->edged_width/2,
1699                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1700                                    current->rounding_type, current->mbs, &pEnc->vGMC);
1701    
1702          if(vol_header)                  } else {
                 BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);  
1703    
1704          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);                          generate_GMCimage(&current->new_gmc_data, &reference->image,
1705                                    pParam->mb_width, pParam->mb_height,
1706                                    pParam->edged_width, pParam->edged_width/2,
1707                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
1708                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
1709                    }
1710            }
1711    
         *pBits = BitstreamPos(bs);  
1712    
1713          pEnc->sStat.iTextBits = 0;          if (pEnc->num_threads > 0) {
1714          pEnc->sStat.iMvSum = 0;                  /* multithreaded motion estimation - dispatch threads */
         pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1715    
1716          for(y = 0; y < pEnc->mbParam.mb_height; y++)                  void * status;
1717          {                  int rows_per_thread = (pParam->mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
                 for(x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1718    
1719                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                  for (k = 0; k < pEnc->num_threads; k++) {
1720                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
1721                            pEnc->motionData[k].pParam = &pEnc->mbParam;
1722                            pEnc->motionData[k].current = current;
1723                            pEnc->motionData[k].reference = reference;
1724                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
1725                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
1726                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
1727                            pEnc->motionData[k].pGMC = &pEnc->vGMC;
1728                            pEnc->motionData[k].y_step = pEnc->num_threads;
1729                            pEnc->motionData[k].start_y = k;
1730                            /* todo: sort out temp space once and for all */
1731                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pParam->edged_width;
1732                    }
1733    
1734                          if (!bIntra)                  for (k = 1; k < pEnc->num_threads; k++) {
1735                          {                          pthread_create(&pEnc->motionData[k].handle, NULL,
1736                                  start_timer();                                  (void*)MotionEstimateSMP, (void*)&pEnc->motionData[k]);
1737                                  MBMotionCompensation(pMB,                  }
                                                      x, y,  
                                                      &pEnc->reference->image,  
                                                      &pEnc->vInterH,  
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
                                                      pEnc->mbParam.height,  
                                                      pEnc->mbParam.edged_width,  
                                                      pEnc->current->rounding_type);  
                                 stop_comp_timer();  
1738    
1739                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  MotionEstimateSMP(&pEnc->motionData[0]);
1740                                          if(pMB->dquant != NO_CHANGE) {  
1741                                                  pMB->mode = MODE_INTER_Q;                  for (k = 1; k < pEnc->num_threads; k++) {
1742                                                  pEnc->current->quant += DQtab[pMB->dquant];                          pthread_join(pEnc->motionData[k].handle, &status);
                                                 if (pEnc->current->quant > 31) pEnc->current->quant = 31;  
                                                 else if(pEnc->current->quant < 1) pEnc->current->quant = 1;  
1743                                          }                                          }
1744    
1745                    current->fcode = 0;
1746                    for (k = 0; k < pEnc->num_threads; k++) {
1747                            current->sStat.iMvSum += pEnc->motionData[k].mvSum;
1748                            current->sStat.iMvCount += pEnc->motionData[k].mvCount;
1749                            if (pEnc->motionData[k].minfcode > current->fcode)
1750                                    current->fcode = pEnc->motionData[k].minfcode;
1751                                  }                                  }
                                 pMB->quant = pEnc->current->quant;  
1752    
1753                                  pMB->field_pred = 0;          } else {
1754                    /* regular ME */
1755    
1756                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                  MotionEstimation(&pEnc->mbParam, current, reference,
1757                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1758                                                     &pEnc->vGMC, 256*4096);
1759                          }                          }
1760                          else  
1761                          {          stop_motion_timer();
1762    
1763            set_timecodes(current,reference,pParam->fbase);
1764    
1765            BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
1766    
1767            for (y = 0; y < mb_height; y++) {
1768                    for (x = 0; x < mb_width; x++) {
1769                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1770                            int skip_possible;
1771    
1772                            if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1773                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1774                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1775                          }                                                                    dct_codes, qcoeff);
1776    
1777                          start_timer();                          start_timer();
1778                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff);
1779                          stop_prediction_timer();                          stop_prediction_timer();
1780    
1781                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                                  current->sStat.kblks++;
1782                          {  
1783                                  pEnc->sStat.kblks++;                                  MBCoding(current, pMB, qcoeff, bs, &current->sStat);
1784                                    stop_coding_timer();
1785                                    continue;
1786                          }                          }
1787                          else if (pMB->cbp ||  
1788                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1789                                   pMB->mvs[1].x || pMB->mvs[1].y ||                          MBMotionCompensation(pMB, x, y, &reference->image,
1790                                   pMB->mvs[2].x || pMB->mvs[2].y ||                                                                   &pEnc->vInterH, &pEnc->vInterV,
1791                                   pMB->mvs[3].x || pMB->mvs[3].y)                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1792                          {                                                                   &current->image,
1793                                  pEnc->sStat.mblks++;                                                                   dct_codes, pParam->width,
1794                                                                     pParam->height,
1795                                                                     pParam->edged_width,
1796                                                                     (current->vol_flags & XVID_VOL_QUARTERPEL),
1797                                                                     current->rounding_type);
1798    
1799                            stop_comp_timer();
1800    
1801                            pMB->field_pred = 0;
1802    
1803                            if (pMB->cbp != 0) {
1804                                    pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y,
1805                                                                              dct_codes, qcoeff);
1806                          }                          }
1807                          else  
1808                          {                          if (pMB->dquant != 0)
1809                                  pEnc->sStat.ublks++;                                  MBSetDquant(pMB, x, y, &pEnc->mbParam);
1810    
1811    
1812                            if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1813                                       pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1814                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1815                                    current->sStat.mblks++;
1816                            }  else {
1817                                    current->sStat.ublks++;
1818                          }                          }
1819    
1820                          start_timer();                          start_timer();
1821                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);  
1822                            /* Finished processing the MB, now check if to CODE or SKIP */
1823    
1824                            skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
1825    
1826                            if (current->coding_type == S_VOP)
1827                                    skip_possible &= (pMB->mcsel == 1);
1828                            else { /* PVOP */
1829                                    const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1830                                                                                    pMB->qmvs : pMB->mvs;
1831                                    skip_possible &= ((mv->x|mv->y) == 0);
1832                            }
1833    
1834                            if ((pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1835                                    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1836                                    int bSkip = 1;
1837    
1838                                    if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1839    
1840                                            for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1841                                                    int iSAD;
1842                                                    iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1843                                                                                    pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1844                                                                                    pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1845                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) {
1846                                                            bSkip = 0; /* could not SKIP */
1847                                                            if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1848                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1849                                                                    pMB->pmvs[0].x = - predMV.x;
1850                                                                    pMB->pmvs[0].y = - predMV.y;
1851                                                            } else {
1852                                                                    VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0);
1853                                                                    pMB->pmvs[0].x = - predMV.x;
1854                                                                    pMB->pmvs[0].y = - predMV.y;
1855                                                            }
1856                                                            pMB->mode = MODE_INTER;
1857                                                            pMB->cbp = 0;
1858                                                            break;
1859                                                    }
1860                                            }
1861                                    }
1862    
1863                                    if (bSkip) {
1864                                            /* do SKIP */
1865                                            pMB->mode = MODE_NOT_CODED;
1866                                            MBSkip(bs);
1867                                            stop_coding_timer();
1868                                            continue;       /* next MB */
1869                                    }
1870                            }
1871    
1872                            /* ordinary case: normal coded INTER/INTER4V block */
1873                            MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat);
1874                          stop_coding_timer();                          stop_coding_timer();
1875                  }                  }
1876          }          }
1877    
1878          emms();          emms();
1879            updateFcode(&current->sStat, pEnc);
1880    
1881          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          /* frame drop code */
1882    #if 0
1883            DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
1884    #endif
1885            if (current->sStat.kblks + current->sStat.mblks <=
1886                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
1887                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) )
1888          {          {
1889                  HintedMEGet(pEnc, 0);                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
1890          }                  current->sStat.ublks = mb_width * mb_height;
1891    
1892          if (pEnc->sStat.iMvCount == 0)                  BitstreamReset(bs);
                 pEnc->sStat.iMvCount = 1;  
1893    
1894          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);                  set_timecodes(current,reference,pParam->fbase);
1895                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
1896    
1897          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);                  /* copy reference frame details into the current frame */
1898                    current->quant = reference->quant;
1899                    current->motion_flags = reference->motion_flags;
1900                    current->rounding_type = reference->rounding_type;
1901                    current->fcode = reference->fcode;
1902                    current->bcode = reference->bcode;
1903                    current->stamp = reference->stamp;
1904                    image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
1905                    memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1906                    coded = 0;
1907    
1908          if ((fSigma > iSearchRange / 3)          } else {
1909              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128  
1910          {                  pEnc->current->is_edged = 0; /* not edged */
1911                  pEnc->mbParam.m_fcode++;                  pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
1912                  iSearchRange *= 2;  
1913                    /* what was this frame's interpolated reference will become
1914                            forward (past) reference in b-frame coding */
1915    
1916                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
1917                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
1918                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
1919          }          }
1920          else if ((fSigma < iSearchRange / 6)  
1921                   && (pEnc->sStat.fMvPrevSigma >= 0)          /* XXX: debug
                  && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)  
                  && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16  
1922          {          {
1923                  pEnc->mbParam.m_fcode--;                  char s[100];
1924                  iSearchRange /= 2;                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1925                    image_dump_yuvpgm(&current->image,
1926                            pParam->edged_width,
1927                            pParam->width, pParam->height, s);
1928    
1929                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1930                    image_dump_yuvpgm(&reference->image,
1931                            pParam->edged_width,
1932                            pParam->width, pParam->height, s);
1933          }          }
1934            */
1935    
1936          pEnc->sStat.fMvPrevSigma = fSigma;          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1937    
1938          *pBits = BitstreamPos(bs) - *pBits;          current->length = (BitstreamPos(bs) - bits) / 8;
1939    
1940          return 0;                                        // inter          return coded;
1941  }  }
1942    
1943    
1944  #ifdef BFRAMES  static void
1945  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  FrameCodeB(Encoder * pEnc,
1946                       FRAMEINFO * frame,
1947                       Bitstream * bs)
1948  {  {
1949      int16_t dct_codes[6*64];          int bits = BitstreamPos(bs);
1950      int16_t qcoeff[6*64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1951            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1952      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1953    
1954      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1955          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1956    
1957          // forward          #ifdef BFRAMES_DEC_DEBUG
1958          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          FILE *fp;
1959          start_timer();          static char first=0;
1960          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  #define BFRAME_DEBUG    if (!first && fp){ \
1961                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                  fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1962          stop_inter_timer();          }
1963    
1964          // backward          if (!first){
1965          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);                  fp=fopen("C:\\XVIDDBGE.TXT","w");
1966      start_timer();          }
1967          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  #endif
                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);  
         stop_inter_timer();  
1968    
1969          start_timer();          /* forward  */
1970          MotionEstimationBVOP(&pEnc->mbParam, frame,          if (!pEnc->reference->is_edged) {
1971                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_setedges(f_ref, pEnc->mbParam.edged_width,
1972                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                             pEnc->mbParam.edged_height, pEnc->mbParam.width,
1973                                               pEnc->mbParam.height, 0);
1974                    pEnc->current->is_edged = 1;
1975            }
1976    
1977            if (pEnc->reference->is_interpolated != 0) {
1978                    start_timer();
1979                    image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
1980                                                      pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1981                                                      (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1982                    stop_inter_timer();
1983                    pEnc->reference->is_interpolated = 0;
1984            }
1985    
1986          stop_motion_timer();          /* backward */
1987            if (!pEnc->current->is_edged) {
1988                    image_setedges(b_ref, pEnc->mbParam.edged_width,
1989                                               pEnc->mbParam.edged_height, pEnc->mbParam.width,
1990                                               pEnc->mbParam.height, 0);
1991                    pEnc->current->is_edged = 1;
1992            }
1993    
1994          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          if (pEnc->current->is_interpolated != 0) {
1995          {                  start_timer();
1996                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
1997          }*/                                                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1998                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
1999                    stop_inter_timer();
2000                    pEnc->current->is_interpolated = 0;
2001            }
2002    
2003      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
2004      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
   
     *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;  
2005    
2006            frame->fcode = frame->bcode = pEnc->current->fcode;
2007    
2008      for (y = 0; y < pEnc->mbParam.mb_height; y++)          start_timer();
2009          {          if (pEnc->num_threads > 0) {
2010                  // reset prediction                  void * status;
2011                    int k;
2012                  forward.x = 0;                  /* multithreaded motion estimation - dispatch threads */
2013                  forward.y = 0;                  int rows_per_thread = (pEnc->mbParam.mb_height + pEnc->num_threads - 1)/pEnc->num_threads;
2014                  backward.x = 0;  
2015                  backward.y = 0;                  for (k = 0; k < pEnc->num_threads; k++) {
2016                            memset(pEnc->motionData[k].complete_count_self, 0, rows_per_thread * sizeof(int));
2017                            pEnc->motionData[k].pParam = &pEnc->mbParam;
2018                            pEnc->motionData[k].current = frame;
2019                            pEnc->motionData[k].reference = pEnc->current;
2020                            pEnc->motionData[k].fRef = f_ref;
2021                            pEnc->motionData[k].fRefH = &pEnc->f_refh;
2022                            pEnc->motionData[k].fRefV = &pEnc->f_refv;
2023                            pEnc->motionData[k].fRefHV = &pEnc->f_refhv;
2024                            pEnc->motionData[k].pRef = b_ref;
2025                            pEnc->motionData[k].pRefH = &pEnc->vInterH;
2026                            pEnc->motionData[k].pRefV = &pEnc->vInterV;
2027                            pEnc->motionData[k].pRefHV = &pEnc->vInterHV;
2028                            pEnc->motionData[k].time_bp = (int32_t)(pEnc->current->stamp - frame->stamp);
2029                            pEnc->motionData[k].time_pp = (int32_t)(pEnc->current->stamp - pEnc->reference->stamp);
2030                            pEnc->motionData[k].y_step = pEnc->num_threads;
2031                            pEnc->motionData[k].start_y = k;
2032                            /* todo: sort out temp space once and for all */
2033                            pEnc->motionData[k].RefQ = pEnc->vInterH.u + 16*k*pEnc->mbParam.edged_width;
2034                    }
2035    
2036                    for (k = 1; k < pEnc->num_threads; k++) {
2037                            pthread_create(&pEnc->motionData[k].handle, NULL,
2038                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->motionData[k]);
2039                    }
2040    
2041                    SMPMotionEstimationBVOP(&pEnc->motionData[0]);
2042    
2043                    for (k = 1; k < pEnc->num_threads; k++) {
2044                            pthread_join(pEnc->motionData[k].handle, &status);
2045                    }
2046    
2047                    frame->fcode = frame->bcode = 0;
2048                    for (k = 0; k < pEnc->num_threads; k++) {
2049                            if (pEnc->motionData[k].minfcode > frame->fcode)
2050                                    frame->fcode = pEnc->motionData[k].minfcode;
2051                            if (pEnc->motionData[k].minbcode > frame->bcode)
2052                                    frame->bcode = pEnc->motionData[k].minbcode;
2053                    }
2054            } else {
2055                    MotionEstimationBVOP(&pEnc->mbParam, frame,
2056                                                             ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2057                                                             ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2058                                                             pEnc->reference->mbs, f_ref,
2059                                                             &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2060                                                             pEnc->current, b_ref, &pEnc->vInterH,
2061                                                             &pEnc->vInterV, &pEnc->vInterHV);
2062            }
2063            stop_motion_timer();
2064    
2065                  for (x = 0; x < pEnc->mbParam.mb_width; x++)          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2066                  {          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
                         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];  
2067    
2068                          // decoder ignores mb when refence block is INTER(0,0), CBP=0          frame->sStat.iTextBits = 0;
2069                          if (mb->mode == MODE_NOT_CODED)          frame->sStat.iMVBits = 0;
2070                          {          frame->sStat.iMvSum = 0;
2071                                  mb->mvs[0].x = 0;          frame->sStat.iMvCount = 0;
2072                                  mb->mvs[0].y = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2073            frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2074            frame->sStat.kblks = frame->sStat.ublks = 0;
2075    
2076            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2077                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2078                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2079    
2080                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2081                            if (mb->mode == MODE_NOT_CODED) {
2082                                    if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2083                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2084                                                                                            NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0);
2085                                    }
2086                                  continue;                                  continue;
2087                          }                          }
2088    
                         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);  
   
2089                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
2090    
2091                            if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) {
2092                                    /* we have to motion-compensate, transfer etc,
2093                                            because there might be blocks to code */
2094    
2095                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2096                                  mb->cbp == 0 &&                                                                                   f_ref, &pEnc->f_refh, &pEnc->f_refv,
2097                                  mb->mvs[0].x == 0 &&                                                                                   &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2098                                  mb->mvs[0].y == 0)                                                                                   &pEnc->vInterV, &pEnc->vInterHV,
2099                          {                                                                                   dct_codes);
                                 mb->mode = 5;  // skipped  
                         }  
2100    
2101                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                                  mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y,  dct_codes, qcoeff);
                         {  
                                 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;  
2102                          }                          }
2103    
2104                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_DIRECT_NO4V)
2105                          {                                  mb->mode = MODE_DIRECT;
                                 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;  
                         }  
2106    
2107  //                      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);                          if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2108                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2109                            else
2110                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2111                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2112                                            mb->cbp &= 0x3C;
2113    
2114                          start_timer();                          start_timer();
2115                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs,
2116                                                     &frame->sStat);
2117                          stop_coding_timer();                          stop_coding_timer();
2118                  }                  }
2119          }          }
   
2120          emms();          emms();
2121    
2122          // TODO: dynamic fcode/bcode ???          BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2123            frame->length = (BitstreamPos(bs) - bits) / 8;
2124    
2125          *pBits = BitstreamPos(bs) - *pBits;  #ifdef BFRAMES_DEC_DEBUG
2126            if (!first){
2127                    first=1;
2128                    if (fp)
2129                            fclose(fp);
2130  }  }
2131  #endif  #endif
2132    }

Legend:
Removed from v.164  
changed lines
  Added in v.1693

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