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

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