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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.58  
changed lines
  Added in v.1.118

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