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

Legend:
Removed from v.1.74  
changed lines
  Added in v.1.117

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