[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.44, Sat Feb 15 05:57:14 2003 UTC revision 1.141, Wed Apr 6 14:30:14 2011 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-2010 Michael Militzer <isibaar@xvid.org>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
8   *  to use this software module in hardware or software products are   *               2002      Daniel Smith <danielsmith@astroboymail.com>
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
9   *   *
10   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
11   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 21 
21   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
  ****************************************************************************/  
   
 /*****************************************************************************  
  *  
  *  History  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              MinChen <chenm001@163.com>  
  *  20.06.2002 bframe patch  
  *  08.05.2002 fix some problem in DEBUG mode;  
  *             MinChen <chenm001@163.com>  
  *  14.04.2002 added FrameCodeB()  
  *  
24   *  $Id$   *  $Id$
25   *   *
26   ****************************************************************************/   ****************************************************************************/
# Line 56  Line 38 
38  #include "image/font.h"  #include "image/font.h"
39  #include "motion/sad.h"  #include "motion/sad.h"
40  #include "motion/motion.h"  #include "motion/motion.h"
41    #include "motion/gmc.h"
42    
43  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
44  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
45  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
46  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "utils/ratecontrol.h"  
47  #include "utils/emms.h"  #include "utils/emms.h"
48  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
 #include "quant/adapt_quant.h"  
49  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
50  #include "utils/mem_align.h"  #include "utils/mem_align.h"
51    
52  /*****************************************************************************  # include "motion/motion_smp.h"
  * Local macros  
  ****************************************************************************/  
53    
 #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  
 #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }  
54    
55  /*****************************************************************************  /*****************************************************************************
56   * Local function prototypes   * Local function prototypes
57   ****************************************************************************/   ****************************************************************************/
58    
59  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
60                                            Bitstream * bs,                                            Bitstream * bs);
                                           uint32_t * pBits);  
61    
62  static int FrameCodeP(Encoder * pEnc,  static int FrameCodeP(Encoder * pEnc,
63                                            Bitstream * bs,                                            Bitstream * bs);
                                           uint32_t * pBits,  
                                           bool force_inter,  
                                           bool vol_header);  
64    
65  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
66                                             FRAMEINFO * frame,                                             FRAMEINFO * frame,
67                                             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  
 };  
68    
69    
70  /*****************************************************************************  /*****************************************************************************
# Line 117  Line 78 
78   * and cleaning code.   * and cleaning code.
79   *   *
80   * Returned values :   * Returned values :
81   *    - XVID_ERR_OK     - no errors   *      - 0                             - no errors
82   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function   *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
83   *                        cleans the structure before exiting.   *                        cleans the structure before exiting.
84   *                        pParam->handle is also set to NULL.   *                        pParam->handle is also set to NULL.
85   *   *
86   ****************************************************************************/   ****************************************************************************/
87    
 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;  
         }  
   
88          /*          /*
89           * Simplify the "fincr/fbase" fraction           * Simplify the "fincr/fbase" fraction
          * (neccessary, since windows supplies us with huge numbers)  
90           */           */
91    static int
92    gcd(int a, int b)
93    {
94            int r ;
95    
96          i = pParam->fincr;          if (b > a) {
97          while (i > 1) {                  r = a;
98                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {                  a = b;
99                          pParam->fincr /= i;                  b = r;
                         pParam->fbase /= i;  
                         i = pParam->fincr;  
                         continue;  
                 }  
                 i--;  
100          }          }
101    
102          if (pParam->fbase > 65535) {          while ((r = a % b)) {
103                  float div = (float) pParam->fbase / 65535;                  a = b;
104                    b = r;
105                  pParam->fbase = (int) (pParam->fbase / div);          }
106                  pParam->fincr = (int) (pParam->fincr / div);          return b;
107          }          }
108    
109          /* Bitrate allocator defaults */  static void
110    simplify_time(int *inc, int *base)
111          if (pParam->rc_bitrate <= 0)  {
112                  pParam->rc_bitrate = 900000;          /* common factor */
113            const int s = gcd(*inc, *base);
114      *inc  /= s;
115      *base /= s;
116    
117          if (pParam->rc_reaction_delay_factor <= 0)          if (*base > 65535 || *inc > 65535) {
118                  pParam->rc_reaction_delay_factor = 16;                  int *biggest;
119                    int *other;
120                    float div;
121    
122          if (pParam->rc_averaging_period <= 0)                  if (*base > *inc) {
123                  pParam->rc_averaging_period = 100;                          biggest = base;
124                            other = inc;
125                    } else {
126                            biggest = inc;
127                            other = base;
128                    }
129    
130          if (pParam->rc_buffer <= 0)                  div = ((float)*biggest)/((float)65535);
131                  pParam->rc_buffer = 100;                  *biggest = (unsigned int)(((float)*biggest)/div);
132                    *other = (unsigned int)(((float)*other)/div);
133            }
134    }
135    
         /* Max and min quantizers */  
136    
137          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))  int
138                  pParam->min_quantizer = 1;  enc_create(xvid_enc_create_t * create)
139    {
140            Encoder *pEnc;
141            int n;
142    
143          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */
144                  pParam->max_quantizer = 31;                  return XVID_ERR_VERSION;
145    
146          if (pParam->max_quantizer < pParam->min_quantizer)          if (create->width%2 || create->height%2)
147                  pParam->max_quantizer = pParam->min_quantizer;                  return XVID_ERR_FAIL;
148    
149          /* 1 keyframe each 10 seconds */          if (create->width<=0 || create->height<=0)
150                    return XVID_ERR_FAIL;
151    
152          if (pParam->max_key_interval <= 0)          /* allocate encoder struct */
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
153    
154          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
155          if (pEnc == NULL)          if (pEnc == NULL)
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
   
         /* Zero the Encoder Structure */  
   
157          memset(pEnc, 0, sizeof(Encoder));          memset(pEnc, 0, sizeof(Encoder));
158    
159          /* Fill members of Encoder structure */          pEnc->mbParam.profile = create->profile;
   
         pEnc->mbParam.width = pParam->width;  
         pEnc->mbParam.height = pParam->height;  
160    
161            /* global flags */
162            pEnc->mbParam.global_flags = create->global;
163      if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED))
164        pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA;
165    
166            /* width, height */
167            pEnc->mbParam.width = create->width;
168            pEnc->mbParam.height = create->height;
169          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
170          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
   
171          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
172          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
173    
174          pEnc->mbParam.fbase = pParam->fbase;          /* framerate */
175          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = MAX(create->fincr, 0);
176            pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
177            if (pEnc->mbParam.fincr>0)
178                    simplify_time((int*)&pEnc->mbParam.fincr, (int*)&pEnc->mbParam.fbase);
179    
180          pEnc->mbParam.m_quant_type = H263_QUANT;          /* zones */
181            if(create->num_zones > 0) {
182                    pEnc->num_zones = create->num_zones;
183                    pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE);
184                    if (pEnc->zones == NULL)
185                            goto xvid_err_memory0;
186                    memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones);
187            } else {
188                    pEnc->num_zones = 0;
189                    pEnc->zones = NULL;
190            }
191    
192          pEnc->fMvPrevSigma = -1;          /* plugins */
193            if(create->num_plugins > 0) {
194                    pEnc->num_plugins = create->num_plugins;
195                    pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE);
196                    if (pEnc->plugins == NULL)
197                            goto xvid_err_memory0;
198            } else {
199                    pEnc->num_plugins = 0;
200                    pEnc->plugins = NULL;
201            }
202    
203            for (n=0; n<pEnc->num_plugins;n++) {
204                    xvid_plg_create_t pcreate;
205                    xvid_plg_info_t pinfo;
206    
207                    memset(&pinfo, 0, sizeof(xvid_plg_info_t));
208                    pinfo.version = XVID_VERSION;
209                    if (create->plugins[n].func(NULL, XVID_PLG_INFO, &pinfo, NULL) >= 0) {
210                            pEnc->mbParam.plugin_flags |= pinfo.flags;
211                    }
212    
213                    memset(&pcreate, 0, sizeof(xvid_plg_create_t));
214                    pcreate.version = XVID_VERSION;
215                    pcreate.num_zones = pEnc->num_zones;
216                    pcreate.zones = pEnc->zones;
217                    pcreate.width = pEnc->mbParam.width;
218                    pcreate.height = pEnc->mbParam.height;
219                    pcreate.mb_width = pEnc->mbParam.mb_width;
220                    pcreate.mb_height = pEnc->mbParam.mb_height;
221                    pcreate.fincr = pEnc->mbParam.fincr;
222                    pcreate.fbase = pEnc->mbParam.fbase;
223                    pcreate.param = create->plugins[n].param;
224    
225                    pEnc->plugins[n].func = NULL;   /* disable plugins that fail */
226                    if (create->plugins[n].func(NULL, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) {
227                            pEnc->plugins[n].func = create->plugins[n].func;
228                    }
229            }
230    
231            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) ||
232                    (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
233                    pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */
234            }
235    
236            /* temp dquants */
237            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
238                    pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width *
239                                                    pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
240                    if (pEnc->temp_dquants==NULL)
241                            goto xvid_err_memory1a;
242            }
243    
244            /* temp lambdas */
245            if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
246                    pEnc->temp_lambda = (float *) xvid_malloc(pEnc->mbParam.mb_width *
247                                                    pEnc->mbParam.mb_height * 6 * sizeof(float), CACHE_LINE);
248                    if (pEnc->temp_lambda == NULL)
249                            goto xvid_err_memory1a;
250            }
251    
252            /* bframes */
253            pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
254            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
255            pEnc->mbParam.bquant_offset = create->bquant_offset;
256    
257          /* Fill rate control parameters */          /* min/max quant */
258            for (n=0; n<3; n++) {
259                    pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2;
260                    pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31;
261            }
262    
263          pEnc->bitrate = pParam->rc_bitrate;          /* frame drop ratio */
264            pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
265    
266          pEnc->iFrameNum = -1;          /* max keyframe interval */
267          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval;
268    
269          /* try to allocate frame memory */          /* allocate working frame-image memory */
270    
271          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
272          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
# Line 240  Line 274 
274          if (pEnc->current == NULL || pEnc->reference == NULL)          if (pEnc->current == NULL || pEnc->reference == NULL)
275                  goto xvid_err_memory1;                  goto xvid_err_memory1;
276    
277          /* try to allocate mb memory */          /* allocate macroblock memory */
278    
279          pEnc->current->mbs =          pEnc->current->mbs =
280                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
# Line 252  Line 286 
286          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
287                  goto xvid_err_memory2;                  goto xvid_err_memory2;
288    
289          /* try to allocate image memory */          /* allocate quant matrix memory */
290    
291            pEnc->mbParam.mpeg_quant_matrices =
292                    xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
293    
294            if (pEnc->mbParam.mpeg_quant_matrices == NULL)
295                    goto xvid_err_memory2a;
296    
297            /* allocate interpolation image memory */
298    
299          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
300                  image_null(&pEnc->sOriginal);                  image_null(&pEnc->sOriginal);
301                    image_null(&pEnc->sOriginal2);
302            }
303    
304          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
305          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
# Line 265  Line 309 
309          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
310          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
311          image_null(&pEnc->vInterV);          image_null(&pEnc->vInterV);
         image_null(&pEnc->vInterVf);  
312          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
         image_null(&pEnc->vInterHVf);  
313    
314          if (pParam->global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
315          {       if (image_create                  if (image_create
316                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
317                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
318                          goto xvid_err_memory3;                          goto xvid_err_memory3;
319    
320                    if (image_create
321                            (&pEnc->sOriginal2, pEnc->mbParam.edged_width,
322                             pEnc->mbParam.edged_height) < 0)
323                            goto xvid_err_memory3;
324          }          }
325    
326          if (image_create          if (image_create
# Line 306  Line 353 
353                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
354                  goto xvid_err_memory3;                  goto xvid_err_memory3;
355          if (image_create          if (image_create
                 (&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
         if (image_create  
356                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
357                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
358                  goto xvid_err_memory3;                  goto xvid_err_memory3;
         if (image_create  
                 (&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                  pEnc->mbParam.edged_height) < 0)  
                 goto xvid_err_memory3;  
359    
360  /* Create full bitplane for GMC, this might be wasteful */  /* Create full bitplane for GMC, this might be wasteful */
361          if (image_create          if (image_create
# Line 324  Line 363 
363                   pEnc->mbParam.edged_height) < 0)                   pEnc->mbParam.edged_height) < 0)
364                  goto xvid_err_memory3;                  goto xvid_err_memory3;
365    
366            /* init bframe image buffers */
367    
368            pEnc->bframenum_head = 0;
369          pEnc->mbParam.global = pParam->global;          pEnc->bframenum_tail = 0;
370            pEnc->flush_bframes = 0;
371            pEnc->closed_bframenum = -1;
372    
373          /* B Frames specific init */          /* B Frames specific init */
         pEnc->mbParam.max_bframes = pParam->max_bframes;  
         pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;  
         pEnc->mbParam.bquant_offset = pParam->bquant_offset;  
         pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;  
374          pEnc->bframes = NULL;          pEnc->bframes = NULL;
375    
376          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
                 int n;  
377    
378                  pEnc->bframes =                  pEnc->bframes =
379                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
# Line 372  Line 409 
409                  }                  }
410          }          }
411    
412          pEnc->bframenum_head = 0;          /* init incoming frame queue */
413          pEnc->bframenum_tail = 0;          pEnc->queue_head = 0;
414          pEnc->flush_bframes = 0;          pEnc->queue_tail = 0;
415          pEnc->bframenum_dx50bvop = -1;          pEnc->queue_size = 0;
   
         pEnc->queue = NULL;  
   
   
         if (pEnc->mbParam.max_bframes > 0) {  
                 int n;  
416    
417                  pEnc->queue =                  pEnc->queue =
418                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),                  xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
419                                                  CACHE_LINE);                                                  CACHE_LINE);
420    
421                  if (pEnc->queue == NULL)                  if (pEnc->queue == NULL)
422                          goto xvid_err_memory4;                          goto xvid_err_memory4;
423    
424                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)          for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
425                          image_null(&pEnc->queue[n]);                  image_null(&pEnc->queue[n].image);
426    
427                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {  
428            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
429                          if (image_create                          if (image_create
430                                  (&pEnc->queue[n], pEnc->mbParam.edged_width,                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
431                                   pEnc->mbParam.edged_height) < 0)                                   pEnc->mbParam.edged_height) < 0)
432                                  goto xvid_err_memory5;                                  goto xvid_err_memory5;
   
                 }  
433          }          }
434    
435          pEnc->queue_head = 0;          /* timestamp stuff */
         pEnc->queue_tail = 0;  
         pEnc->queue_size = 0;  
436    
437          pEnc->mbParam.m_stamp = 0;          pEnc->mbParam.m_stamp = 0;
438            pEnc->m_framenum = create->start_frame_num;
         pEnc->m_framenum = 0;  
439          pEnc->current->stamp = 0;          pEnc->current->stamp = 0;
440          pEnc->reference->stamp = 0;          pEnc->reference->stamp = 0;
441    
442          pParam->handle = (void *) pEnc;          /* other stuff */
443    
444            pEnc->iFrameNum = 0;
445            pEnc->fMvPrevSigma = -1;
446    
447            /* slices */
448            pEnc->num_slices = MIN(MAX(1, create->num_slices), (int) pEnc->mbParam.mb_height);
449    
450            /* multithreaded stuff */
451            if (create->num_threads > 0) {
452    #ifndef HAVE_PTHREAD
453                    int t = MAX(1, create->num_threads);
454    #else
455                    int t = MIN(create->num_threads, (int) (pEnc->mbParam.mb_height>>1)); /* at least two rows per thread */
456    #endif
457                    int threads_per_slice = MAX(1, (t / pEnc->num_slices));
458                    int rows_per_thread = (pEnc->mbParam.mb_height + threads_per_slice - 1) / threads_per_slice;
459    
460                    pEnc->num_threads = t;
461                    pEnc->smpData = xvid_malloc(t*sizeof(SMPData), CACHE_LINE);
462                    if (!pEnc->smpData)
463                            goto xvid_err_nosmp;
464    
465                    /* tmp bitstream buffer for slice coding */
466                    pEnc->smpData[0].tmp_buffer = xvid_malloc(16*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height*sizeof(uint8_t), CACHE_LINE);
467                    if (! pEnc->smpData[0].tmp_buffer) goto xvid_err_nosmp;
468    
469                    for (n = 0; n < t; n++) {
470                            int s = MIN(pEnc->num_threads, pEnc->num_slices);
471    
472                            pEnc->smpData[n].complete_count_self =
473                                    xvid_malloc(rows_per_thread * sizeof(int), CACHE_LINE);
474    
475                            if (!pEnc->smpData[n].complete_count_self)
476                                    goto xvid_err_nosmp;
477    
478                            if (n > 0 && n < s) {
479                                    pEnc->smpData[n].bs = (Bitstream *) xvid_malloc(sizeof(Bitstream), CACHE_LINE);
480                                    if (!pEnc->smpData[n].bs)
481                                            goto xvid_err_nosmp;
482    
483                                    pEnc->smpData[n].sStat = (Statistics *) xvid_malloc(sizeof(Statistics), CACHE_LINE);
484                                    if (!pEnc->smpData[n].sStat)
485                                            goto xvid_err_nosmp;
486    
487                                    pEnc->smpData[n].tmp_buffer = pEnc->smpData[0].tmp_buffer + 16*(((n-1)*pEnc->mbParam.edged_width*pEnc->mbParam.mb_height)/s);
488                                    BitstreamInit(pEnc->smpData[n].bs, pEnc->smpData[n].tmp_buffer, 0);
489                            }
490    
491                            if (n != 0)
492                                    pEnc->smpData[n].complete_count_above =
493                                            pEnc->smpData[n-1].complete_count_self;
494                    }
495                    pEnc->smpData[0].complete_count_above =
496                            pEnc->smpData[t-1].complete_count_self - 1;
497    
498            } else {
499      xvid_err_nosmp:
500                    /* no SMP */
501                    if (pEnc->smpData) {
502                            if (pEnc->smpData[0].tmp_buffer)
503                                    xvid_free(pEnc->smpData[0].tmp_buffer);
504                    }
505                    else {
506                            pEnc->smpData = xvid_malloc(1*sizeof(SMPData), CACHE_LINE);
507                            if (pEnc->smpData == NULL)
508                                    goto xvid_err_memory5;
509                    }
510    
511          if (pParam->rc_bitrate) {                  create->num_threads = 0;
                 RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,  
                                                 pParam->rc_reaction_delay_factor,  
                                                 pParam->rc_averaging_period, pParam->rc_buffer,  
                                                 pParam->fbase * 1000 / pParam->fincr,  
                                                 pParam->max_quantizer, pParam->min_quantizer);  
512          }          }
513    
514            create->handle = (void *) pEnc;
515    
516          init_timer();          init_timer();
517            init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices);
518    
519          return XVID_ERR_OK;          return 0;   /* ok */
520    
521          /*          /*
522           * 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 432  Line 524 
524    
525    xvid_err_memory5:    xvid_err_memory5:
526    
527            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) {
528          if (pEnc->mbParam.max_bframes > 0) {                          image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width,
   
                 for (i = 0; i < pEnc->mbParam.max_bframes; i++) {  
                         image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,  
529                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
530                  }                  }
531    
532                  xvid_free(pEnc->queue);                  xvid_free(pEnc->queue);
         }  
533    
534    xvid_err_memory4:    xvid_err_memory4:
535    
536          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
537                    int i;
538    
539                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
540    
# Line 453  Line 543 
543    
544                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
545                                                    pEnc->mbParam.edged_height);                                                    pEnc->mbParam.edged_height);
   
546                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
547                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
   
548                  }                  }
549    
550                  xvid_free(pEnc->bframes);                  xvid_free(pEnc->bframes);
# Line 465  Line 552 
552    
553    xvid_err_memory3:    xvid_err_memory3:
554    
555          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
556          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
557                                              pEnc->mbParam.edged_height);
558                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
559                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
560          }          }
561    
# Line 485  Line 574 
574                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
575          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
576                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
577          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
578                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
579    
580  /* destroy GMC image */  /* destroy GMC image */
581          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
582                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
583    
584      xvid_err_memory2a:
585            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
586    
587    xvid_err_memory2:    xvid_err_memory2:
588          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
# Line 504  Line 591 
591    xvid_err_memory1:    xvid_err_memory1:
592          xvid_free(pEnc->current);          xvid_free(pEnc->current);
593          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
594    
595      xvid_err_memory1a:
596            if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
597                    xvid_free(pEnc->temp_dquants);
598            }
599    
600            if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
601                    xvid_free(pEnc->temp_lambda);
602            }
603    
604      xvid_err_memory0:
605            for (n=0; n<pEnc->num_plugins;n++) {
606                    if (pEnc->plugins[n].func) {
607                            pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, NULL, NULL);
608                    }
609            }
610            xvid_free(pEnc->plugins);
611    
612            xvid_free(pEnc->zones);
613    
614          xvid_free(pEnc);          xvid_free(pEnc);
615    
616          pParam->handle = NULL;          create->handle = NULL;
617    
618          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
619  }  }
# Line 515  Line 622 
622   * Encoder destruction   * Encoder destruction
623   *   *
624   * This function destroy the entire encoder structure created by a previous   * This function destroy the entire encoder structure created by a previous
625   * successful encoder_create call.   * successful enc_create call.
626   *   *
627   * Returned values (for now only one returned value) :   * Returned values (for now only one returned value) :
628   *    - XVID_ERR_OK     - no errors   *      - 0      - no errors
629   *   *
630   ****************************************************************************/   ****************************************************************************/
631    
632  int  int
633  encoder_destroy(Encoder * pEnc)  enc_destroy(Encoder * pEnc)
634  {  {
635          int i;          int i;
636    
         ENC_CHECK(pEnc);  
   
637          /* B Frames specific */          /* B Frames specific */
638          if (pEnc->mbParam.max_bframes > 0) {          for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
639                    image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
                 for (i = 0; i < pEnc->mbParam.max_bframes; i++) {  
   
                         image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,  
640                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
641                  }                  }
                 xvid_free(pEnc->queue);  
         }  
642    
643            xvid_free(pEnc->queue);
644    
645          if (pEnc->mbParam.max_bframes > 0) {          if (pEnc->mbParam.max_bframes > 0) {
646    
# Line 550  Line 651 
651    
652                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
653                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
   
654                          xvid_free(pEnc->bframes[i]->mbs);                          xvid_free(pEnc->bframes[i]->mbs);
   
655                          xvid_free(pEnc->bframes[i]);                          xvid_free(pEnc->bframes[i]);
656                  }                  }
657    
# Line 570  Line 669 
669                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
670          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
671                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
672          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
673                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,  
                                   pEnc->mbParam.edged_height);  
   
674          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
675                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
676          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
677                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
678          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
679                                    pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
680            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
681                                      pEnc->mbParam.edged_height);
682    
683          if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
684          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
685                                              pEnc->mbParam.edged_height);
686                    image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width,
687                                            pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
688          }          }
689    
# Line 597  Line 695 
695          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
696          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
697    
698          xvid_free(pEnc);          if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
699                    xvid_free(pEnc->temp_dquants);
         return XVID_ERR_OK;  
700  }  }
701    
702            if ((pEnc->mbParam.plugin_flags & XVID_REQLAMBDA)) {
703  static __inline void inc_frame_num(Encoder * pEnc)                  xvid_free(pEnc->temp_lambda);
 {  
         pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero  
         pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;  
704  }  }
705    
706            if (pEnc->num_plugins>0) {
707                    xvid_plg_destroy_t pdestroy;
708                    memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t));
709    
710  static __inline void                  pdestroy.version = XVID_VERSION;
711  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)                  pdestroy.num_frames = pEnc->m_framenum;
 {  
         if (pEnc->queue_size >= pEnc->mbParam.max_bframes)  
         {  
                 DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");  
                 return;  
         }  
712    
713          DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  for (i=0; i<pEnc->num_plugins;i++) {
714                                  pEnc->bframenum_head, pEnc->bframenum_tail,                          if (pEnc->plugins[i].func) {
715                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, NULL);
716                            }
717                    }
718                    xvid_free(pEnc->plugins);
719            }
720    
721            xvid_free(pEnc->mbParam.mpeg_quant_matrices);
722    
723          start_timer();          if (pEnc->num_zones > 0)
724          if (image_input                  xvid_free(pEnc->zones);
                 (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,  
                  pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))  
                 return;  
         stop_conv_timer();  
725    
726          if ((pFrame->general & XVID_CHROMAOPT)) {          if (pEnc->num_threads > 0) {
727                  image_chroma_optimize(&pEnc->queue[pEnc->queue_tail],                  for (i = 1; i < MAX(1, MIN(pEnc->num_threads, pEnc->num_slices)); i++) {
728                          pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                          xvid_free(pEnc->smpData[i].bs);
729                            xvid_free(pEnc->smpData[i].sStat);
730          }          }
731                    if (pEnc->smpData[0].tmp_buffer) xvid_free(pEnc->smpData[0].tmp_buffer);
732    
733          pEnc->queue_size++;                  for (i = 0; i < pEnc->num_threads; i++)
734          pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;                          xvid_free(pEnc->smpData[i].complete_count_self);
735  }  }
736            xvid_free(pEnc->smpData);
737    
738  static __inline void          xvid_free(pEnc);
 set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)  
 {  
   
                 pCur->ticks = (int32_t)pCur->stamp % time_base;  
                 pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;  
   
                 //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable  
   
 /*              fprintf(stderr,"WriteVop:   %d - %d \n",  
                         ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));  
                 fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",  
                         pCur->coding_type, pCur->stamp, pRef->stamp, time_base);  
                 fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",  
                         pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);  
739    
740  */          return 0;  /* ok */
741  }  }
742    
743    
744    /*
745      call the plugins
746      */
747    
748  /* convert pFrame->intra to coding_type */  static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original,
749  static int intra2coding_type(int intra)                                                   int opt, int * type, int * quant, xvid_enc_stats_t * stats)
750  {  {
751          if (intra < 0)  return -1;          unsigned int i, j, k;
752          if (intra == 1) return I_VOP;          xvid_plg_data_t data;
         if (intra == 2) return B_VOP;  
753    
754          return P_VOP;          /* set data struct */
 }  
755    
756            memset(&data, 0, sizeof(xvid_plg_data_t));
757            data.version = XVID_VERSION;
758    
759            /* find zone */
760            for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ;
761            data.zone = i>0 ? &pEnc->zones[i-1] : NULL;
762    
763            data.width = pEnc->mbParam.width;
764            data.height = pEnc->mbParam.height;
765            data.mb_width = pEnc->mbParam.mb_width;
766            data.mb_height = pEnc->mbParam.mb_height;
767            data.fincr = frame->fincr;
768            data.fbase = pEnc->mbParam.fbase;
769            data.bquant_ratio = pEnc->mbParam.bquant_ratio;
770            data.bquant_offset = pEnc->mbParam.bquant_offset;
771    
772            for (i=0; i<3; i++) {
773                    data.min_quant[i] = pEnc->mbParam.min_quant[i];
774                    data.max_quant[i] = pEnc->mbParam.max_quant[i];
775            }
776    
777            data.reference.csp = XVID_CSP_PLANAR;
778            data.reference.plane[0] = pEnc->reference->image.y;
779            data.reference.plane[1] = pEnc->reference->image.u;
780            data.reference.plane[2] = pEnc->reference->image.v;
781            data.reference.stride[0] = pEnc->mbParam.edged_width;
782            data.reference.stride[1] = pEnc->mbParam.edged_width/2;
783            data.reference.stride[2] = pEnc->mbParam.edged_width/2;
784    
785            data.current.csp = XVID_CSP_PLANAR;
786            data.current.plane[0] = frame->image.y;
787            data.current.plane[1] = frame->image.u;
788            data.current.plane[2] = frame->image.v;
789            data.current.stride[0] = pEnc->mbParam.edged_width;
790            data.current.stride[1] = pEnc->mbParam.edged_width/2;
791            data.current.stride[2] = pEnc->mbParam.edged_width/2;
792    
793            data.frame_num = frame->frame_num;
794    
795            if (opt == XVID_PLG_BEFORE) {
796                    data.type = *type;
797                    data.quant = *quant;
798    
799                    data.vol_flags = frame->vol_flags;
800                    data.vop_flags = frame->vop_flags;
801                    data.motion_flags = frame->motion_flags;
802    
803            } else if (opt == XVID_PLG_FRAME) {
804                    data.type = coding2type(frame->coding_type);
805                    data.quant = frame->quant;
806    
807                    if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
808                            data.dquant = pEnc->temp_dquants;
809                            data.dquant_stride = pEnc->mbParam.mb_width;
810                            memset(data.dquant, 0, data.mb_width*data.mb_height*sizeof(int));
811                    }
812    
813                    if(pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
814                            int block = 0;
815                            emms();
816                            data.lambda = pEnc->temp_lambda;
817                            for(i = 0;i < pEnc->mbParam.mb_height; i++)
818                                    for(j = 0;j < pEnc->mbParam.mb_width; j++)
819                                            for (k = 0; k < 6; k++)
820                                                    data.lambda[block++] = 1.0f;
821                    }
822    
823            } else { /* XVID_PLG_AFTER */
824                    if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
825                            data.original.csp = XVID_CSP_PLANAR;
826                            data.original.plane[0] = original->y;
827                            data.original.plane[1] = original->u;
828                            data.original.plane[2] = original->v;
829                            data.original.stride[0] = pEnc->mbParam.edged_width;
830                            data.original.stride[1] = pEnc->mbParam.edged_width/2;
831                            data.original.stride[2] = pEnc->mbParam.edged_width/2;
832                    }
833    
834  /*****************************************************************************                  if ((frame->vol_flags & XVID_VOL_EXTRASTATS) ||
835   * IPB frame encoder entry point                          (pEnc->mbParam.plugin_flags & XVID_REQPSNR)) {
  *  
  * Returned values :  
  *    - XVID_ERR_OK     - no errors  
  *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong  
  *                        format  
  ****************************************************************************/  
836    
837  int                          data.sse_y =
838  encoder_encode_bframes(Encoder * pEnc,                                  plane_sse( original->y, frame->image.y,
839                             XVID_ENC_FRAME * pFrame,                                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
840                             XVID_ENC_STATS * pResult)                                                     pEnc->mbParam.height);
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
         int mode;  
841    
842          int input_valid = 1;                          data.sse_u =
843          int bframes_count = 0;                                  plane_sse( original->u, frame->image.u,
844                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
845                                                       pEnc->mbParam.height/2);
846    
847          ENC_CHECK(pEnc);                          data.sse_v =
848          ENC_CHECK(pFrame);                                  plane_sse( original->v, frame->image.v,
849          ENC_CHECK(pFrame->image);                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
850                                                       pEnc->mbParam.height/2);
851                    }
852    
853          start_global_timer();                  data.type = coding2type(frame->coding_type);
854                    data.quant = frame->quant;
855    
856          BitstreamInit(&bs, pFrame->bitstream, 0);                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
857                            data.dquant = pEnc->temp_dquants;
858                            data.dquant_stride = pEnc->mbParam.mb_width;
859    
860                            for (j=0; j<pEnc->mbParam.mb_height; j++)
861                            for (i=0; i<pEnc->mbParam.mb_width; i++) {
862                                    data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;
863                            }
864                    }
865    
866  ipvop_loop:                  data.vol_flags = frame->vol_flags;
867                    data.vop_flags = frame->vop_flags;
868                    data.motion_flags = frame->motion_flags;
869    
870                    data.length = frame->length;
871                    data.kblks = frame->sStat.kblks;
872                    data.mblks = frame->sStat.mblks;
873                    data.ublks = frame->sStat.ublks;
874    
875                    /* New code */
876                    data.stats.type      = coding2type(frame->coding_type);
877                    data.stats.quant     = frame->quant;
878                    data.stats.vol_flags = frame->vol_flags;
879                    data.stats.vop_flags = frame->vop_flags;
880                    data.stats.length    = frame->length;
881                    data.stats.hlength   = frame->length - (frame->sStat.iTextBits / 8);
882                    data.stats.kblks     = frame->sStat.kblks;
883                    data.stats.mblks     = frame->sStat.mblks;
884                    data.stats.ublks     = frame->sStat.ublks;
885                    data.stats.sse_y     = data.sse_y;
886                    data.stats.sse_u     = data.sse_u;
887                    data.stats.sse_v     = data.sse_v;
888    
889          /*                  if (stats)
890           * bframe "flush" code                          *stats = data.stats;
891           */          }
892    
893          if ((pFrame->image == NULL || pEnc->flush_bframes)          /* call plugins */
894                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {          for (i=0; i<(unsigned int)pEnc->num_plugins;i++) {
895                    emms();
896                    if (pEnc->plugins[i].func) {
897                            if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, NULL) < 0) {
898                                    continue;
899                            }
900                    }
901            }
902            emms();
903    
904                  if (pEnc->flush_bframes == 0) {          /* copy modified values back into frame*/
905                          /*          if (opt == XVID_PLG_BEFORE) {
906                           * we have reached the end of stream without getting                  *type = data.type;
907                           * a future reference frame... so encode last final                  *quant = data.quant > 0 ? data.quant : 2;   /* default */
                          * frame as a pframe  
                          */  
908    
909                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                  frame->vol_flags = data.vol_flags;
910                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  frame->vop_flags = data.vop_flags;
911                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                  frame->motion_flags = data.motion_flags;
912    
913                          pEnc->bframenum_tail--;          } else if (opt == XVID_PLG_FRAME) {
                         SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);  
914    
915                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) {
916                            for (j=0; j<pEnc->mbParam.mb_height; j++)
917                            for (i=0; i<pEnc->mbParam.mb_width; i++) {
918                                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i];
919                            }
920                    } else {
921                            for (j=0; j<pEnc->mbParam.mb_height; j++)
922                            for (i=0; i<pEnc->mbParam.mb_width; i++) {
923                                    frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0;
924                            }
925                    }
926    
927                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                  if (pEnc->mbParam.plugin_flags & XVID_REQLAMBDA) {
928                          bframes_count = 0;                          for (j = 0; j < pEnc->mbParam.mb_height; j++)
929                                    for (i = 0; i < pEnc->mbParam.mb_width; i++)
930                                            for (k = 0; k < 6; k++) {
931                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] =
932                                                            (int) ((float)(1<<LAMBDA_EXP) * data.lambda[6 * (j * data.mb_width + i) + k]);
933                                            }
934                    } else {
935                            for (j = 0; j<pEnc->mbParam.mb_height; j++)
936                                    for (i = 0; i<pEnc->mbParam.mb_width; i++)
937                                            for (k = 0; k < 6; k++) {
938                                                    frame->mbs[j*pEnc->mbParam.mb_width + i].lambda[k] = 1<<LAMBDA_EXP;
939                                            }
940                    }
941    
                         BitstreamPadAlways(&bs);  
                         pFrame->length = BitstreamLength(&bs);  
                         pFrame->intra = 0;  
942    
943                    frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */
944            }
945    
                         emms();  
946    
                         if (pResult) {  
                                 pResult->quant = pEnc->current->quant;  
                                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                                 pResult->kblks = pEnc->current->sStat.kblks;  
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
                         }  
   
                         return XVID_ERR_OK;  
947                  }                  }
948    
949    
950                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",  static __inline void inc_frame_num(Encoder * pEnc)
951                                  pEnc->bframenum_head, pEnc->bframenum_tail,  {
952                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);          pEnc->current->frame_num = pEnc->m_framenum;
953            pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
                 FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);  
                 pEnc->bframenum_head++;  
954    
955                  BitstreamPadAlways(&bs);          pEnc->mbParam.m_stamp += pEnc->current->fincr;
956                  pFrame->length = BitstreamLength(&bs);          pEnc->m_framenum++;     /* debug ticker */
                 pFrame->intra = 2;  
   
                 if (pResult) {  
                         pResult->quant = pEnc->current->quant;  
                         pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                         pResult->kblks = pEnc->current->sStat.kblks;  
                         pResult->mblks = pEnc->current->sStat.mblks;  
                         pResult->ublks = pEnc->current->sStat.ublks;  
957                  }                  }
958    
959                  if (input_valid)  static __inline void dec_frame_num(Encoder * pEnc)
960                          queue_image(pEnc, pFrame);  {
961            pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr;
962                  emms();          pEnc->m_framenum--;     /* debug ticker */
   
                 return XVID_ERR_OK;  
963          }          }
964    
965          if (pEnc->bframenum_head > 0) {  static __inline void
966                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam)
967    {
968                  /* write an empty marker to the bitstream.          if (pMB->cbp == 0) {
969                    /* we want to code dquant but the quantizer value will not be used yet
970                     for divx5 decoder compatibility, this marker must consist                          let's find out if we can postpone dquant to next MB
                    of a not-coded p-vop, with a time_base of zero, and time_increment  
                    indentical to the future-referece frame.  
971                  */                  */
972                    if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) {
973                            pMB->dquant = 0; /* it's the last MB of all, the easiest case */
974                            return;
975                    } else {
976                            MACROBLOCK * next = pMB + 1;
977                            const MACROBLOCK * prev = pMB - 1;
978                            if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED)
979                                    /* mode allows dquant change in the future */
980                                    if (abs(next->quant - prev->quant) <= 2) {
981                                            /* quant change is not out of range */
982                                            pMB->quant = prev->quant;
983                                            pMB->dquant = 0;
984                                            next->dquant = next->quant - prev->quant;
985                                            return;
986                                    }
987                    }
988            }
989            /* couldn't skip this dquant */
990            pMB->mode = MODE_INTER_Q;
991    }
992    
                 if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {  
                         int tmp;  
   
                         DPRINTF(DPRINTF_DEBUG,"*** EMPTY 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);  
993    
994    
995                          tmp = pEnc->current->seconds;  static __inline void
996                          pEnc->current->seconds = 0; /* force time_base = 0 */  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
997    {
998    
999                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);          pCur->ticks = (int32_t)pCur->stamp % time_base;
1000                          pEnc->current->seconds = tmp;          pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
1001    
1002                          BitstreamPadAlways(&bs);  #if 0   /* HEAVY DEBUG OUTPUT */
1003                          pFrame->length = BitstreamLength(&bs);          fprintf(stderr,"WriteVop:   %d - %d \n",
1004                          pFrame->intra = 4;                          ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
1005            fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
1006                          if (pResult) {                          pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
1007                                  pResult->quant = pEnc->current->quant;          fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
1008                                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);                          pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
1009                                  pResult->kblks = pEnc->current->sStat.kblks;  #endif
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
1010                          }                          }
1011    
1012                          if (input_valid)  static void
1013                                  queue_image(pEnc, pFrame);  simplify_par(int *par_width, int *par_height)
1014    {
1015    
1016            int _par_width  = (!*par_width)  ? 1 : (*par_width<0)  ? -*par_width:  *par_width;
1017            int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height;
1018            int divisor = gcd(_par_width, _par_height);
1019    
1020            _par_width  /= divisor;
1021            _par_height /= divisor;
1022    
1023            /* 2^8 precision maximum */
1024            if (_par_width>255 || _par_height>255) {
1025                    float div;
1026                          emms();                          emms();
1027                    if (_par_width>_par_height)
1028                            div = (float)_par_width/255;
1029                    else
1030                            div = (float)_par_height/255;
1031    
1032                          return XVID_ERR_OK;                  _par_width  = (int)((float)_par_width/div);
1033                    _par_height = (int)((float)_par_height/div);
1034                  }                  }
1035    
1036            *par_width = _par_width;
1037            *par_height = _par_height;
1038    
1039            return;
1040          }          }
1041    
1042    /*****************************************************************************
1043     * IPB frame encoder entry point
1044     *
1045     * Returned values :
1046     *      - >0                       - output bytes
1047     *      - 0                             - no output
1048     *      - XVID_ERR_VERSION - wrong version passed to core
1049     *      - XVID_ERR_END   - End of stream reached before end of coding
1050     *      - XVID_ERR_FORMAT  - the image subsystem reported the image had a wrong
1051     *                                               format
1052     ****************************************************************************/
1053    
 bvop_loop:  
1054    
1055          if (pEnc->bframenum_dx50bvop != -1)  int
1056    enc_encode(Encoder * pEnc,
1057                               xvid_enc_frame_t * xFrame,
1058                               xvid_enc_stats_t * stats)
1059          {          {
1060            xvid_enc_frame_t * frame;
1061            int type;
1062            Bitstream bs;
1063    
1064                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);          if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))     /* v1.x.x */
1065                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);                  return XVID_ERR_VERSION;
1066    
1067                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {          xFrame->out_flags = 0;
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");  
                 }  
1068    
1069                  if (input_valid)          start_global_timer();
1070                  {          BitstreamInit(&bs, xFrame->bitstream, 0);
                         queue_image(pEnc, pFrame);  
                         input_valid = 0;  
                 }  
1071    
         } else if (input_valid) {  
1072    
1073                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1074             * enqueue image to the encoding-queue
1075             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1076    
1077            if (xFrame->input.csp != XVID_CSP_NULL)
1078            {
1079                    QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
1080    
1081                  start_timer();                  start_timer();
1082                  if (image_input                  if (image_input
1083                          (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
1084                          pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))                          pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
1085                            xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING))
1086                  {                  {
1087                          emms();                          emms();
1088                          return XVID_ERR_FORMAT;                          return XVID_ERR_FORMAT;
1089                  }                  }
1090                  stop_conv_timer();                  stop_conv_timer();
1091    
1092                  if ((pFrame->general & XVID_CHROMAOPT)) {                  if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1093                          image_chroma_optimize(&pEnc->current->image,                          image_chroma_optimize(&q->image,
1094                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1095                  }                  }
1096    
1097                  // queue input frame, and dequue next image                  q->frame = *xFrame;
1098                  if (pEnc->queue_size > 0)  
1099                  {                  if (xFrame->quant_intra_matrix)
                         image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);  
                         if (pEnc->queue_head != pEnc->queue_tail)  
1100                          {                          {
1101                                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char));
1102                          }                          q->frame.quant_intra_matrix = q->quant_intra_matrix;
                         pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;  
                         pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;  
1103                  }                  }
1104    
1105          } else if (pEnc->queue_size > 0) {                  if (xFrame->quant_inter_matrix)
1106                    {
1107                            memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char));
1108                            q->frame.quant_inter_matrix = q->quant_inter_matrix;
1109                    }
1110    
1111                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);                  pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
1112                    pEnc->queue_size++;
1113            }
1114    
                 image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);  
                 pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;  
                 pEnc->queue_size--;  
1115    
1116          } else {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1117             * bframe flush code
1118             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1119    
1120                  /* if nothing was encoded, write an 'ignore this frame' flag  repeat:
                    to the bitstream */  
1121    
1122                  if (BitstreamPos(&bs) == 0) {          if (pEnc->flush_bframes)
1123            {
1124                    if (pEnc->bframenum_head < pEnc->bframenum_tail) {
1125    
1126                          DPRINTF(DPRINTF_DEBUG,"*** SKIP 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",
1127                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1128                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1129    
1130                  //      BitstreamPutBits(&bs, 0x7f, 8);                          if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1131                          pFrame->intra = 5;                                  image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image,
1132                                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
                         if (pResult) {  
                                 /*  
                                  * We must decide what to put there because i know some apps  
                                  * are storing statistics about quantizers and just do  
                                  * stats[quant]++ or stats[quant-1]++  
                                  * transcode is one of these app with its 2pass module  
                                  */  
   
                                 /*  
                                  * For now i prefer 31 than 0 that could lead to a segfault  
                                  * in transcode  
                                  */  
                                 pResult->quant = 31;  
   
                                 pResult->hlength = 0;  
                                 pResult->kblks = 0;  
                                 pResult->mblks = 0;  
                                 pResult->ublks = 0;  
1133                          }                          }
1134    
1135                  } else {                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
1136                            call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, NULL, NULL, stats);
1137                          if (pResult) {                          pEnc->bframenum_head++;
                                 pResult->quant = pEnc->current->quant;  
                                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                                 pResult->kblks = pEnc->current->sStat.kblks;  
                                 pResult->mblks = pEnc->current->sStat.mblks;  
                                 pResult->ublks = pEnc->current->sStat.ublks;  
                         }  
1138    
1139                            goto done;
1140                  }                  }
1141    
1142                  pFrame->length = BitstreamLength(&bs);                  /* write an empty marker to the bitstream.
   
                 emms();  
1143    
1144                  return XVID_ERR_OK;                     for divx5 decoder compatibility, this marker must consist
1145          }                     of a not-coded p-vop, with a time_base of zero, and time_increment
1146                       indentical to the future-referece frame.
1147                    */
1148    
1149          pEnc->flush_bframes = 0;                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) {
1150                            int tmp;
1151                            int bits;
1152    
1153          emms();                          DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1154                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1155                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1156    
1157          // only inc frame num, adapt quant, etc. if we havent seen it before                          bits = BitstreamPos(&bs);
         if (pEnc->bframenum_dx50bvop < 0 )  
         {  
                 mode = intra2coding_type(pFrame->intra);  
                 if (pFrame->quant == 0)  
                         pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);  
                 else  
                         pEnc->current->quant = pFrame->quant;  
1158    
1159  /*              if (pEnc->current->quant < 1)                          tmp = pEnc->current->seconds;
1160                          pEnc->current->quant = 1;                          pEnc->current->seconds = 0; /* force time_base = 0 */
1161    
1162                  if (pEnc->current->quant > 31)                          BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant);
1163                          pEnc->current->quant = 31;                          BitstreamPad(&bs);
1164  */                          pEnc->current->seconds = tmp;
                 pEnc->current->global_flags = pFrame->general;  
                 pEnc->current->motion_flags = pFrame->motion;  
1165    
1166                  /* ToDo : dynamic fcode (in both directions) */                          /* add the not-coded length to the reference frame size */
1167                  pEnc->current->fcode = pEnc->mbParam.m_fcode;                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
1168                  pEnc->current->bcode = pEnc->mbParam.m_fcode;                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1169    
1170                  inc_frame_num(pEnc);                          /* flush complete: reset counters */
1171                            pEnc->flush_bframes = 0;
1172                            pEnc->bframenum_head = pEnc->bframenum_tail = 0;
1173                            goto done;
1174    
                 if (pFrame->general & XVID_EXTRASTATS)  
                 {       image_copy(&pEnc->sOriginal, &pEnc->current->image,  
                                    pEnc->mbParam.edged_width, pEnc->mbParam.height);  
1175                  }                  }
1176    
1177                  emms();                  /* flush complete: reset counters */
1178                    pEnc->flush_bframes = 0;
1179                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
                         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);  
1180                  }                  }
1181    
1182          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1183           * Luminance masking           * dequeue frame from the encoding queue
1184           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1185    
1186                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if (pEnc->queue_size == 0)              /* empty */
1187                          int *temp_dquants =          {
1188                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
1189                                                                  pEnc->mbParam.mb_height * sizeof(int),                  {
                                                                 CACHE_LINE);  
1190    
1191                          pEnc->current->quant =                          DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1192                                  adaptive_quantization(pEnc->current->image.y,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1193                                                                    pEnc->mbParam.edged_width, temp_dquants,                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                                                                   pEnc->current->quant, pEnc->current->quant,  
                                                                   2 * pEnc->current->quant,  
                                                                   pEnc->mbParam.mb_width,  
                                                                   pEnc->mbParam.mb_height);  
1194    
1195                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {                          if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) {
1196                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1197                            }
1198    
1199  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          /* if the very last frame is to be b-vop, we must change it to a p-vop */
1200                            if (pEnc->bframenum_tail > 0) {
1201    
1202                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1203                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];                                  pEnc->bframenum_tail--;
1204                                    SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1205    
1206                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                                  /* convert B-VOP to P-VOP */
1207                                  }                                  pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1208                                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1209                                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1210    
1211  #undef OFFSET                                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1212                                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1213                                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1214                          }                          }
1215    
1216                          xvid_free(temp_dquants);                                  DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1217                  }                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1218                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1219                                    pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */
1220    
1221          }                                  FrameCodeP(pEnc, &bs);
1222    
         /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
          * ivop/pvop/bvop selection  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
         pEnc->iFrameNum++;  
1223    
1224          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||                                  if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) {
1225                  (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&                                          call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
                         pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))  
         {  
                 mode = I_VOP;  
1226          }else{          }else{
1227                  mode = MEanalysis(&pEnc->reference->image, pEnc->current,                                          pEnc->flush_bframes = 1;
1228                                          &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,                                          goto done;
                                         (mode < 0) ? pEnc->iFrameNum : 0,  
                                         bframes_count++);  
1229          }          }
   
         if (mode == I_VOP) {  
                 /*  
                  * 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;  
   
                 if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
   
                 if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {  
                         if (pFrame->quant_intra_matrix != NULL)  
                                 set_intra_matrix(pFrame->quant_intra_matrix);  
                         if (pFrame->quant_inter_matrix != NULL)  
                                 set_inter_matrix(pFrame->quant_inter_matrix);  
1230                  }                  }
1231                            DPRINTF(XVID_DEBUG_DEBUG, "*** END\n");
1232    
1233                            emms();
1234                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",                          return XVID_ERR_END;    /* end of stream reached */
1235                                  pEnc->bframenum_head, pEnc->bframenum_tail,                  }
1236                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);                  goto done;      /* nothing to encode yet; encoder lag */
   
                 if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {  
                         image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");  
1237                  }                  }
1238    
1239                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe          /* the current FRAME becomes the reference */
1240            SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1241    
1242                  if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {          /* remove frame from encoding-queue (head), and move it into the current */
1243            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1244            frame = &pEnc->queue[pEnc->queue_head].frame;
1245            pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1);
1246            pEnc->queue_size--;
1247    
                         pEnc->bframenum_tail--;  
                         pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;  
1248    
1249                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250                          if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {           * init pEnc->current fields
1251                                  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;  
                         pFrame->intra = 0;  
1252    
1253                  } else {          pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr;
1254            inc_frame_num(pEnc);
1255            pEnc->current->vol_flags = frame->vol_flags;
1256            pEnc->current->vop_flags = frame->vop_flags;
1257            pEnc->current->motion_flags = frame->motion;
1258            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1259            pEnc->current->bcode = pEnc->mbParam.m_fcode;
1260    
                         FrameCodeI(pEnc, &bs, &bits);  
                         bframes_count = 0;  
                         pFrame->intra = 1;  
1261    
1262                          pEnc->bframenum_dx50bvop = -1;          if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) {
1263                    image_chroma_optimize(&pEnc->current->image,
1264                            pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
1265                  }                  }
1266    
1267                  pEnc->flush_bframes = 1;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268             * frame type & quant selection
1269             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1270    
1271                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {          type = frame->type;
1272                          BitstreamPadAlways(&bs);          pEnc->current->quant = frame->quant;
                         input_valid = 0;  
                         goto ipvop_loop;  
                 }  
1273    
1274                  /*          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, (int*)&pEnc->current->quant, stats);
                  * NB : sequences like "IIBB" decode fine with msfdam but,  
                  *      go screwy with divx 5.00  
                  */  
         } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {  
                 /*  
                  * 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);  
1275    
1276                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {          if (type > 0){  /* XVID_TYPE_?VOP */
1277                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
1278            } else{         /* XVID_TYPE_AUTO */
1279                    if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){
1280                            pEnc->iFrameNum = 0;
1281                            type = I_VOP;
1282                    }else{
1283                            type = MEanalysis(&pEnc->reference->image, pEnc->current,
1284                                                              &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1285                                                              pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold,
1286                                                              (pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL);
1287                    }
1288                  }                  }
1289    
1290                  FrameCodeP(pEnc, &bs, &bits, 1, 0);          if (type != I_VOP)
1291                  bframes_count = 0;                  pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
1292    
1293                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {          /* bframes buffer overflow check */
1294                          BitstreamPadAlways(&bs);          if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1295                          input_valid = 0;                  type = P_VOP;
                         goto ipvop_loop;  
1296                  }                  }
1297    
1298          } else {        /* mode == B_VOP */          pEnc->iFrameNum++;
1299                  /*  
1300                   * This will be coded as a Bidirectional Frame          if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1301                   */                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
1302                            "%d  st:%lld  if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum);
1303            }
1304    
1305                  if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1306             * encode this frame as a b-vop
1307             * (we dont encode here, rather we store the frame in the bframes queue, to be encoded later)
1308             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1309            if (type == B_VOP) {
1310                    if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1311                          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");
1312                  }                  }
1313    
1314                  if (pFrame->bquant < 1) {                  if (frame->quant < 1) {
1315                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1316                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1317    
1318                  } else {                  } else {
1319                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = frame->quant;
1320                  }                  }
1321    
1322                  if (pEnc->current->quant < 1)                  if (pEnc->current->quant < 1)
# Line 1135  Line 1324 
1324                  else if (pEnc->current->quant > 31)                  else if (pEnc->current->quant > 31)
1325              pEnc->current->quant = 31;              pEnc->current->quant = 31;
1326    
1327                  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",
1328                                  pEnc->bframenum_head, pEnc->bframenum_tail,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1329                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1330    
# Line 1145  Line 1334 
1334    
1335                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1336    
1337  // bframe report by koepi                  goto repeat;
1338                  pFrame->intra = 2;          }
                 pFrame->length = 0;  
   
                 input_valid = 0;  
                 goto bvop_loop;  
         }  
   
         BitstreamPadAlways(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult) {  
                 pResult->quant = pEnc->current->quant;  
                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->current->sStat.kblks;  
                 pResult->mblks = pEnc->current->sStat.mblks;  
                 pResult->ublks = pEnc->current->sStat.ublks;  
   
                 if (pFrame->general & XVID_EXTRASTATS)  
                 {       pResult->sse_y =  
                                 plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,  
                                                    pEnc->mbParam.edged_width, pEnc->mbParam.width,  
                                                    pEnc->mbParam.height);  
1339    
                         pResult->sse_u =  
                                 plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,  
                                                    pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,  
                                                    pEnc->mbParam.height/2);  
1340    
1341                          pResult->sse_v =                  DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1342                                  plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1343                                                     pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1344                                                     pEnc->mbParam.height/2);  
1345            /* for unpacked bframes, output the stats for the last encoded frame */
1346            if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0)
1347            {
1348                    if (pEnc->current->stamp > 0) {
1349                            call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1350                  }                  }
1351            else if (stats) {
1352                stats->type = XVID_TYPE_NOTHING;
1353          }          }
   
         emms();  
   
         if (pFrame->quant == 0) {  
                 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,  
                                                   pFrame->length, pFrame->intra);  
1354          }          }
1355    
1356          stop_global_timer();          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1357          write_timer();           * closed-gop
1358             * if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe
1359             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1360    
1361          emms();          if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
         return XVID_ERR_OK;  
 }  
1362    
1363                    /* place this frame back on the encoding-queue (head) */
1364                    /* we will deal with it next time */
1365                    dec_frame_num(pEnc);
1366                    pEnc->iFrameNum--;
1367    
1368                    pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
1369                    pEnc->queue_size++;
1370                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
1371    
1372  /*****************************************************************************                  /* grab the last frame from the bframe-queue */
  * "original" IP frame encoder entry point  
  *  
  * Returned values :  
  *    - XVID_ERR_OK     - no errors  
  *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong  
  *                        format  
  ****************************************************************************/  
1373    
1374  int                  pEnc->bframenum_tail--;
1375  encoder_encode(Encoder * pEnc,                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                            XVID_ENC_FRAME * pFrame,  
                            XVID_ENC_STATS * pResult)  
 {  
         uint16_t x, y;  
         Bitstream bs;  
         uint32_t bits;  
         uint16_t write_vol_header = 0;  
1376    
1377          float psnr;                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1378          uint8_t temp[128];                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP");
1379                    }
1380    
1381          start_global_timer();                  /* convert B-VOP quant to P-VOP */
1382                    pEnc->current->quant  = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset;
1383                    pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */
1384                    pEnc->current->quant /= pEnc->mbParam.bquant_ratio;
1385                    type = P_VOP;
1386            }
1387    
         ENC_CHECK(pEnc);  
         ENC_CHECK(pFrame);  
         ENC_CHECK(pFrame->bitstream);  
         ENC_CHECK(pFrame->image);  
1388    
1389          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390             * encode this frame as an i-vop
1391             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1392    
1393          pEnc->current->global_flags = pFrame->general;          if (type == I_VOP) {
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->mbParam.hint = &pFrame->hint;  
1394    
1395          inc_frame_num(pEnc);                  DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i\n",
1396                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1397                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1398    
1399          /* disable alternate scan flag if interlacing is not enabled */                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1400          if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
                 !(pEnc->current->global_flags & XVID_INTERLACING))  
         {  
                 pEnc->current->global_flags -= XVID_ALTERNATESCAN;  
1401          }          }
1402    
1403          start_timer();                  pEnc->iFrameNum = 1;
         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();  
1404    
1405          if ((pFrame->general & XVID_CHROMAOPT)) {                  /* ---- update vol flags at IVOP ----------- */
1406                  image_chroma_optimize(&pEnc->current->image,                  pEnc->mbParam.vol_flags = pEnc->current->vol_flags;
                         pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);  
         }  
1407    
1408          if (pFrame->general & XVID_EXTRASTATS)                  /* Aspect ratio */
1409          {       image_copy(&pEnc->sOriginal, &pEnc->current->image,                  switch(frame->par) {
1410                                     pEnc->mbParam.edged_width, pEnc->mbParam.height);                  case XVID_PAR_11_VGA:
1411                    case XVID_PAR_43_PAL:
1412                    case XVID_PAR_43_NTSC:
1413                    case XVID_PAR_169_PAL:
1414                    case XVID_PAR_169_NTSC:
1415                    case XVID_PAR_EXT:
1416                            pEnc->mbParam.par = frame->par;
1417                            break;
1418                    default:
1419                            pEnc->mbParam.par = XVID_PAR_11_VGA;
1420                            break;
1421          }          }
1422    
1423          emms();                  /* For extended PAR only, we try to sanityse/simplify par values */
1424                    if (pEnc->mbParam.par == XVID_PAR_EXT) {
1425          BitstreamInit(&bs, pFrame->bitstream, 0);                          pEnc->mbParam.par_width  = frame->par_width;
1426                            pEnc->mbParam.par_height = frame->par_height;
1427                            simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height);
1428                    }
1429    
1430          if (pFrame->quant == 0) {                  if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) {
1431                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                          if (frame->quant_intra_matrix != NULL)
1432          } else {                                  set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix);
1433                  pEnc->current->quant = pFrame->quant;                          if (frame->quant_inter_matrix != NULL)
1434                                    set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix);
1435          }          }
1436    
1437          if ((pEnc->current->global_flags & XVID_QUARTERPEL))                  /* prevent vol/vop misuse */
                 pEnc->mbParam.m_quarterpel = 1;  
         else  
                 pEnc->mbParam.m_quarterpel = 0;  
1438    
1439          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING))
1440                  int *temp_dquants =                          pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN);
                         (int *) xvid_malloc(pEnc->mbParam.mb_width *  
                                                                 pEnc->mbParam.mb_height * sizeof(int),  
                                                                 CACHE_LINE);  
1441    
1442                  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);  
1443    
1444                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1445                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1446                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1447                    }
1448    
1449  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                  FrameCodeI(pEnc, &bs);
1450                    xFrame->out_flags |= XVID_KEYFRAME;
1451    
1452                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453             * encode this frame as an p-vop
1454             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1455    
1456            } else { /* (type == P_VOP || type == S_VOP) */
1457    
1458                                  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",
1459                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1460                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1461    
1462                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];                  if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) {
1463                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1464                          }                          }
1465    
1466  #undef OFFSET                  if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) {
1467                            image_copy(&pEnc->sOriginal, &pEnc->current->image,
1468                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1469                  }                  }
1470    
1471                  xvid_free(temp_dquants);                  if ( FrameCodeP(pEnc, &bs) == 0 ) {
1472                            /* N-VOP, we mustn't code b-frames yet */
1473                            if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) ||
1474                                     pEnc->mbParam.max_bframes == 0)
1475                                    call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
1476                            goto done;
1477                    }
1478          }          }
1479    
         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;  
1480    
1481                  matrix1_changed = matrix2_changed = 0;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1482             * on next enc_encode call we must flush bframes
1483             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1484    
1485                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)  /*done_flush:*/
                         write_vol_header = 1;  
1486    
1487                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;          pEnc->flush_bframes = 1;
1488    
1489                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {          /* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */
1490                          if (pFrame->quant_intra_matrix != NULL)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1491                                  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;  
1492          }          }
1493    
1494          if (pFrame->intra < 0) {          /* packed or no-bframes or no-bframes-queued: output stats */
1495                  if ((pEnc->iFrameNum == -1)          if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0 ) {
1496                          || ((pEnc->mbParam.iMaxKeyInterval > 0)                  call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, NULL, NULL, stats);
                                 && (pEnc->iFrameNum >= pEnc->mbParam.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);  
1497                  }                  }
1498    
1499          }          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500             * done; return number of bytes consumed
1501             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1502    
1503  //      BitstreamPutBits(&bs, 0xFFFF, 16);  done:
 //      BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPadAlways(&bs);  
         pFrame->length = BitstreamLength(&bs);  
1504    
1505          if (pResult) {          stop_global_timer();
1506                  pResult->quant = pEnc->current->quant;          write_timer();
                 pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->current->sStat.kblks;  
                 pResult->mblks = pEnc->current->sStat.mblks;  
                 pResult->ublks = pEnc->current->sStat.ublks;  
         }  
1507    
1508          emms();          emms();
1509            return BitstreamLength(&bs);
         if (pFrame->quant == 0) {  
                 RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,  
                                                   pFrame->length, pFrame->intra);  
         }  
         if (pFrame->general & XVID_EXTRASTATS)  
         {  
                 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);  
1510          }          }
1511    
         pEnc->iFrameNum++;  
1512    
1513          stop_global_timer();  static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame)
1514          write_timer();  {
1515            unsigned int i;
1516          return XVID_ERR_OK;          MACROBLOCK * pMB = frame->mbs;
1517            int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */
1518            if (quant > 31)
1519                    frame->quant = quant = 31;
1520            else if (quant < 1)
1521                    frame->quant = quant = 1;
1522    
1523            for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) {
1524                    quant += pMB->dquant;
1525                    if (quant > 31)
1526                            quant = 31;
1527                    else if (quant < 1)
1528                            quant = 1;
1529                    pMB->quant = quant;
1530                    pMB++;
1531            }
1532  }  }
1533    
1534    
1535  static __inline void  static __inline void
1536  CodeIntraMB(Encoder * pEnc,  CodeIntraMB(MACROBLOCK * pMB)
                         MACROBLOCK * pMB)  
1537  {  {
   
1538          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1539    
1540          /* zero mv statistics */          /* zero mv statistics */
# Line 1400  Line 1543 
1543          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;
1544          pMB->sad16 = 0;          pMB->sad16 = 0;
1545    
1546          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if (pMB->dquant != 0) {
                 if (pMB->dquant != NO_CHANGE) {  
1547                          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;  
                 }  
1548          }          }
   
         pMB->quant = pEnc->current->quant;  
1549  }  }
1550    
1551    static void
1552  #define FCODEBITS       3  SliceCodeI(SMPData *data)
 #define MODEBITS        5  
   
 void  
 HintedMESet(Encoder * pEnc,  
                         int *intra)  
1553  {  {
1554          HINTINFO *hint;          Encoder *pEnc = (Encoder *) data->pEnc;
1555          Bitstream bs;          Bitstream *bs = (Bitstream *) data->bs;
         int length, high;  
         uint32_t x, y;  
1556    
1557          hint = pEnc->mbParam.hint;          uint16_t x, y;
1558            int mb_width = pEnc->mbParam.mb_width;
1559            int mb_height = pEnc->mbParam.mb_height;
1560    
1561          if (hint->rawhints) {          int bound = 0, num_slices = pEnc->num_slices;
1562                  *intra = hint->mvhint.intra;          FRAMEINFO *const current = pEnc->current;
         } else {  
                 BitstreamInit(&bs, hint->hintstream, hint->hintlength);  
                 *intra = BitstreamGetBit(&bs);  
         }  
1563    
1564          if (*intra) {          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1565                  return;          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1566    
1567            if (data->start_y > 0) { /* write resync marker */
1568                    bound = data->start_y*mb_width;
1569                    write_video_packet_header(bs, &pEnc->mbParam, current, bound);
1570          }          }
1571    
1572          pEnc->current->fcode =          for (y = data->start_y; y < data->stop_y; y++) {
1573                  (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,                  int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
                                                                                                                                  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;  
1574    
1575                                  for (vec = 0; vec < 4; ++vec) {                  if (new_bound > bound) {
1576                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                          bound = new_bound;
1577                                  }                          BitstreamPadAlways(bs);
1578                          }                          write_video_packet_header(bs, &pEnc->mbParam, current, bound);
                 }  
         }  
1579  }  }
1580    
1581                    for (x = 0; x < mb_width; x++) {
1582                            MACROBLOCK *pMB = &current->mbs[x + y * mb_width];
1583    
1584  void                          CodeIntraMB(pMB);
1585  HintedMEGet(Encoder * pEnc,  
1586                          int intra)                          MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y,
1587  {                                                            dct_codes, qcoeff);
         HINTINFO *hint;  
         Bitstream bs;  
         uint32_t x, y;  
         int length, high;  
1588    
1589          hint = pEnc->mbParam.hint;                          start_timer();
1590                            MBPrediction(current, x, y, mb_width, qcoeff, bound);
1591                            stop_prediction_timer();
1592    
1593          if (hint->rawhints) {                          start_timer();
1594                  hint->mvhint.intra = intra;                          MBCoding(current, pMB, qcoeff, bs, data->sStat);
1595          } else {                          stop_coding_timer();
                 BitstreamInit(&bs, hint->hintstream, 0);  
                 BitstreamPutBit(&bs, intra);  
         }  
1596    
         if (intra) {  
                 if (!hint->rawhints) {  
                         BitstreamPadAlways(&bs);  
                         hint->hintlength = BitstreamLength(&bs);  
1597                  }                  }
                 return;  
1598          }          }
1599    
1600          length = pEnc->current->fcode + 5;          emms();
1601          high = 1 << (length - 1);          BitstreamPadAlways(bs);
   
         if (hint->rawhints) {  
                 hint->mvhint.fcode = pEnc->current->fcode;  
         } else {  
                 BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);  
1602          }          }
1603    
1604          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {  static __inline void
1605                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {  SerializeBitstreams(Encoder *pEnc, FRAMEINFO *current, Bitstream *bs, int num_threads)
1606                          MACROBLOCK *pMB =  {
1607                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];          int k;
1608                          MVBLOCKHINT *bhint =          uint32_t pos = BitstreamLength(bs);
                                 &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];  
                         VECTOR tmp;  
   
                         if (hint->rawhints) {  
                                 bhint->mode = pMB->mode;  
                         } else {  
                                 BitstreamPutBits(&bs, pMB->mode, MODEBITS);  
                         }  
1609    
1610                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          for (k = 1; k < num_threads; k++) {
1611                                  tmp.x = pMB->mvs[0].x;                  uint32_t len = BitstreamLength(pEnc->smpData[k].bs);
                                 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;  
1612    
1613                                  for (vec = 0; vec < 4; ++vec) {                  memcpy((void *)((ptr_t)bs->start + pos),
1614                                          tmp.x = pMB->mvs[vec].x;                             (void *)((ptr_t)pEnc->smpData[k].bs->start), len);
1615                                          tmp.y = pMB->mvs[vec].y;  
1616                                          tmp.x += (tmp.x < 0) ? high * 2 : 0;                  current->length += len;
1617                                          tmp.y += (tmp.y < 0) ? high * 2 : 0;                  pos += len;
1618    
1619                                          if (hint->rawhints) {                  /* collect stats */
1620                                                  bhint->mvs[vec].x = tmp.x;                  current->sStat.iTextBits += pEnc->smpData[k].sStat->iTextBits;
1621                                                  bhint->mvs[vec].y = tmp.y;                  current->sStat.kblks += pEnc->smpData[k].sStat->kblks;
1622                                          } else {                  current->sStat.mblks += pEnc->smpData[k].sStat->mblks;
1623                                                  BitstreamPutBits(&bs, tmp.x, length);                  current->sStat.ublks += pEnc->smpData[k].sStat->ublks;
1624                                                  BitstreamPutBits(&bs, tmp.y, length);                  current->sStat.iMVBits += pEnc->smpData[k].sStat->iMVBits;
1625                                          }          }
1626                                  }  
1627                          }          if (num_threads > 1) {
1628                  }                  uint32_t pos32 = pos>>2;
1629                    bs->tail = bs->start + pos32;
1630                    bs->pos = 8*(pos - (pos32<<2));
1631                    bs->buf = 0;
1632    
1633                    if (bs->pos > 0) {
1634                            uint32_t pos8 = bs->pos/8;
1635                            memset((void *)((ptr_t)bs->tail+pos8), 0, (4-pos8));
1636                            pos = *bs->tail;
1637    #ifndef ARCH_IS_BIG_ENDIAN
1638                            BSWAP(pos);
1639    #endif
1640                            bs->buf = pos;
1641          }          }
   
         if (!hint->rawhints) {  
                 BitstreamPad(&bs);  
                 hint->hintlength = BitstreamLength(&bs);  
1642          }          }
1643  }  }
1644    
   
1645  static int  static int
1646  FrameCodeI(Encoder * pEnc,  FrameCodeI(Encoder * pEnc,
1647                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
1648  {  {
1649          int mb_width = pEnc->mbParam.mb_width;          int bits = BitstreamPos(bs);
1650            int bound = 0, num_slices = pEnc->num_slices;
1651            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1652            int slices_per_thread = (num_slices*1024 / num_threads);
1653          int mb_height = pEnc->mbParam.mb_height;          int mb_height = pEnc->mbParam.mb_height;
1654    #ifdef HAVE_PTHREAD
1655            void * status = NULL;
1656    #endif
1657            uint16_t k;
1658    
         DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);  
   
         uint16_t x, y;  
   
         if ((pEnc->current->global_flags & XVID_REDUCED))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
   
                 /* 16x16->8x8 downsample requires 1 additional edge pixel*/  
                 /* XXX: setedges is overkill */  
                 start_timer();  
                 image_setedges(&pEnc->current->image,  
                         pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,  
                         pEnc->mbParam.width, pEnc->mbParam.height);  
                 stop_edges_timer();  
         }  
   
         pEnc->iFrameNum = 0;  
1659          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1660          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;  
1661          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1662    
1663          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1664    
1665            SetMacroblockQuants(&pEnc->mbParam, pEnc->current);
1666    
1667            BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current, num_slices);
1668    
1669          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1670    
1671          BitstreamPadAlways(bs);          BitstreamPad(bs);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  
1672    
1673          *pBits = BitstreamPos(bs);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant);
1674    
1675          pEnc->current->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = 0;
         pEnc->current->sStat.kblks = mb_width * mb_height;  
         pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;  
1676    
1677          for (y = 0; y < mb_height; y++)          /* multithreaded intra coding - dispatch threads */
1678                  for (x = 0; x < mb_width; x++) {          for (k = 0; k < num_threads; k++) {
1679                          MACROBLOCK *pMB =                  int add = ((slices_per_thread + 512) >> 10);
                                 &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1680    
1681                          CodeIntraMB(pEnc, pMB);                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
1682    
1683                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                  pEnc->smpData[k].pEnc = (void *) pEnc;
1684                                                            dct_codes, qcoeff);                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
1685                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
1686    
1687                          start_timer();                  bound += add;
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1688    
1689                          start_timer();                  if (k > 0) {
1690                          if (pEnc->current->global_flags & XVID_GREYSCALE)                          BitstreamReset(pEnc->smpData[k].bs);
1691                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */                          pEnc->smpData[k].sStat->iTextBits = 0;
                                 qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */  
                                 qcoeff[5*64+0]=0;  
1692                          }                          }
                         MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);  
                         stop_coding_timer();  
1693                  }                  }
1694            pEnc->smpData[0].bs = bs;
1695            pEnc->smpData[0].sStat = &pEnc->current->sStat;
1696    
1697          if ((pEnc->current->global_flags & XVID_REDUCED))  #ifdef HAVE_PTHREAD
1698          {          /* create threads */
1699                  image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,          for (k = 1; k < num_threads; k++) {
1700                          pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,                  pthread_create(&pEnc->smpData[k].handle, NULL,
1701                          16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                                 (void*)SliceCodeI, (void*)&pEnc->smpData[k]);
1702          }          }
1703          emms();  #endif
   
         *pBits = BitstreamPos(bs) - *pBits;  
         pEnc->fMvPrevSigma = -1;  
         pEnc->mbParam.m_fcode = 2;  
1704    
1705          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {          SliceCodeI(&pEnc->smpData[0]);
                 HintedMEGet(pEnc, 1);  
         }  
1706    
1707          return 1;                                       // intra  #ifdef HAVE_PTHREAD
1708            /* wait until all threads are finished */
1709            for (k = 1; k < num_threads; k++) {
1710                    pthread_join(pEnc->smpData[k].handle, &status);
1711  }  }
1712    #endif
1713    
1714            pEnc->current->length = BitstreamLength(bs) - (bits/8);
1715    
1716  #define INTRA_THRESHOLD 0.5          /* reassemble the pieces together */
1717  #define BFRAME_SKIP_THRESHHOLD 30          SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
   
   
 /* FrameCodeP also handles S(GMC)-VOPs */  
 static int  
 FrameCodeP(Encoder * pEnc,  
                    Bitstream * bs,  
                    uint32_t * pBits,  
                    bool force_inter,  
                    bool vol_header)  
 {  
         float fSigma;  
   
         DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);  
   
         int mb_width = pEnc->mbParam.mb_width;  
         int mb_height = pEnc->mbParam.mb_height;  
   
         int iLimit;  
         int x, y, k;  
         int iSearchRange;  
         int bIntra, skip_possible;  
   
         /* IMAGE *pCurrent = &pEnc->current->image; */  
         IMAGE *pRef = &pEnc->reference->image;  
   
         if ((pEnc->current->global_flags & XVID_REDUCED))  
         {  
                 mb_width = (pEnc->mbParam.width + 31) / 32;  
                 mb_height = (pEnc->mbParam.height + 31) / 32;  
         }  
1718    
1719            pEnc->current->sStat.iMVBits = 0;
1720            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1721            pEnc->current->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1722    
1723          start_timer();          pEnc->fMvPrevSigma = -1;
1724          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,          pEnc->mbParam.m_fcode = 2;
                                    pEnc->mbParam.width, pEnc->mbParam.height);  
         stop_edges_timer();  
   
         pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;  
         pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;  
         pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;  
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
1725    
1726          if (!force_inter)          pEnc->current->is_edged = 0; /* not edged */
1727                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);          pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
         else  
                 iLimit = mb_width * mb_height + 1;  
1728    
1729          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          return 1;                                       /* intra */
                 start_timer();  
                 image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,  
                                                   &pEnc->vInterHV, pEnc->mbParam.edged_width,  
                                                   pEnc->mbParam.edged_height,  
                                                   pEnc->mbParam.m_quarterpel,  
                                                   pEnc->current->rounding_type);  
                 stop_inter_timer();  
1730          }          }
1731    
1732          pEnc->current->coding_type = P_VOP;  static __inline void
1733    updateFcode(Statistics * sStat, Encoder * pEnc)
1734          start_timer();  {
1735          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          float fSigma;
1736                  HintedMESet(pEnc, &bIntra);          int iSearchRange;
         else  
                 bIntra =  
                         MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,  
                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,  
                          iLimit);  
1737    
1738          stop_motion_timer();          if (sStat->iMvCount == 0)
1739                    sStat->iMvCount = 1;
1740    
1741          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);          fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount);
1742    
1743          if ( ( pEnc->current->global_flags & XVID_GMC )          iSearchRange = 16 << pEnc->mbParam.m_fcode;
                 && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )  
         {  
                 pEnc->current->coding_type = S_VOP;  
1744    
1745                  generate_GMCparameters( 2, 16, &pEnc->current->warp,          if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) )
1746                                          pEnc->mbParam.width, pEnc->mbParam.height,                  pEnc->mbParam.m_fcode++;
                                         &pEnc->current->gmc_data);  
   
                 generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,  
                                 pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,  
                                 pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,  
                                 pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,  
                                 pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);  
1747    
1748          }          else if ((5.0 * fSigma < iSearchRange)
1749                               && (4.0 * pEnc->fMvPrevSigma < iSearchRange)
1750                               && (pEnc->mbParam.m_fcode >= 2) )
1751                    pEnc->mbParam.m_fcode--;
1752    
1753          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);          pEnc->fMvPrevSigma = fSigma;
         if (vol_header)  
         {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);  
                 BitstreamPadAlways(bs);  
1754          }          }
1755    
1756          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);  #define BFRAME_SKIP_THRESHHOLD 30
1757    
1758    static void
1759    SliceCodeP(SMPData *data)
1760    {
1761            Encoder *pEnc = (Encoder *) data->pEnc;
1762            Bitstream *bs = (Bitstream *) data->bs;
1763    
1764          *pBits = BitstreamPos(bs);          int x, y, k;
1765            FRAMEINFO *const current = pEnc->current;
1766            FRAMEINFO *const reference = pEnc->reference;
1767            MBParam * const pParam = &pEnc->mbParam;
1768            int mb_width = pParam->mb_width;
1769            int mb_height = pParam->mb_height;
1770    
1771          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1772                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1773    
1774            int bound = 0, num_slices = pEnc->num_slices;
1775    
1776          for (y = 0; y < mb_height; y++) {          if (data->start_y > 0) { /* write resync marker */
1777                  for (x = 0; x < mb_width; x++) {                  bound = data->start_y*mb_width;
1778                          MACROBLOCK *pMB =                  write_video_packet_header(bs, pParam, current, bound);
1779                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];          }
1780    
1781            for (y = data->start_y; y < data->stop_y; y++) {
1782                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
1783    
1784  /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */                  if (new_bound > bound) {
1785  /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */                          bound = new_bound;
1786                            BitstreamPadAlways(bs);
1787                            write_video_packet_header(bs, pParam, current, bound);
1788                    }
1789    
1790                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                  for (x = 0; x < mb_width; x++) {
1791                            MACROBLOCK *pMB = &current->mbs[x + y * pParam->mb_width];
1792                            int skip_possible;
1793    
1794                          if (bIntra) {                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
1795                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pMB);
1796                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,                                  MBTransQuantIntra(pParam, current, pMB, x, y,
1797                                                                    dct_codes, qcoeff);                                                                    dct_codes, qcoeff);
1798    
1799                                  start_timer();                                  start_timer();
1800                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                                  MBPrediction(current, x, y, pParam->mb_width, qcoeff, bound);
1801                                  stop_prediction_timer();                                  stop_prediction_timer();
1802    
1803                                  pEnc->current->sStat.kblks++;                                  data->sStat->kblks++;
1804    
1805                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                  MBCoding(current, pMB, qcoeff, bs, data->sStat);
1806                                  stop_coding_timer();                                  stop_coding_timer();
1807                                  continue;                                  continue;
1808                          }                          }
1809    
                         if (pEnc->current->coding_type == S_VOP) {  
   
                                 int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,  
                                         pEnc->mbParam.edged_width, 65536);  
   
                                 if (pEnc->current->motion_flags & PMV_CHROMA16) {  
                                         iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
   
                                         iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,  
                                         pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);  
                                 }  
   
                                 if (iSAD <= pMB->sad16) {               /* mode decision GMC */  
   
                                         if (pEnc->mbParam.m_quarterpel)  
                                                 pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;  
                                         else  
                                                 pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
                                         pMB->mode = MODE_INTER;  
                                         pMB->mcsel = 1;  
                                         pMB->sad16 = iSAD;  
                                 } else {  
                                         pMB->mcsel = 0;  
                                 }  
                         } else {  
                                 pMB->mcsel = 0; /* just a precaution */  
                         }  
   
1810                          start_timer();                          start_timer();
1811                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,                          MBMotionCompensation(pMB, x, y, &reference->image,
1812                                                                   &pEnc->vInterH, &pEnc->vInterV,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1813                                                                   &pEnc->vInterHV, &pEnc->vGMC,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1814                                                                   &pEnc->current->image,                                                                   &current->image,
1815                                                                   dct_codes, pEnc->mbParam.width,                                                                   dct_codes, pParam->width,
1816                                                                   pEnc->mbParam.height,                                                                   pParam->height,
1817                                                                   pEnc->mbParam.edged_width,                                                                   pParam->edged_width,
1818                                                                   pEnc->mbParam.m_quarterpel,                                                                   (current->vol_flags & XVID_VOL_QUARTERPEL),
1819                                                                   (pEnc->current->global_flags & XVID_REDUCED),                                                                   current->rounding_type,
1820                                                                   pEnc->current->rounding_type);                                                                   data->RefQ);
1821    
1822                          stop_comp_timer();                          stop_comp_timer();
1823    
                         if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {  
                                 if (pMB->dquant != NO_CHANGE) {  
                                         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;  
                                 }  
                         }  
                         pMB->quant = pEnc->current->quant;  
   
1824                          pMB->field_pred = 0;                          pMB->field_pred = 0;
1825    
1826                          if (pMB->mode != MODE_NOT_CODED)                          if (pMB->cbp != 0) {
1827                          {       pMB->cbp =                                  pMB->cbp = MBTransQuantInter(pParam, current, pMB, x, y,
                                         MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,  
1828                                                                            dct_codes, qcoeff);                                                                            dct_codes, qcoeff);
1829                          }                          }
1830    
1831                            if (pMB->dquant != 0)
1832                                    MBSetDquant(pMB, x, y, pParam);
1833    
1834    
1835                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1836                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1837                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1838                                  pEnc->current->sStat.mblks++;                                  data->sStat->mblks++;
1839                          }  else {                          }  else {
1840                                  pEnc->current->sStat.ublks++;                                  data->sStat->ublks++;
1841                          }                          }
1842    
1843                          start_timer();                          start_timer();
1844    
1845                          /* Finished processing the MB, now check if to CODE or SKIP */                          /* Finished processing the MB, now check if to CODE or SKIP */
1846    
1847                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER);
                                                         (pMB->dquant == NO_CHANGE);  
1848    
1849                          if (pEnc->current->coding_type == S_VOP)                          if (current->coding_type == S_VOP)
1850                                  skip_possible &= (pMB->mcsel == 1);                                  skip_possible &= (pMB->mcsel == 1);
1851                          else if (pEnc->current->coding_type == P_VOP) {                          else { /* PVOP */
1852                                  if (pEnc->mbParam.m_quarterpel)                                  const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ?
1853                                          skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );                                                                                  pMB->qmvs : pMB->mvs;
1854                                  else                                  skip_possible &= ((mv->x|mv->y) == 0);
                                         skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );  
1855                          }                          }
1856    
1857                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
   
1858  /* 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 */
   
                                 if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */  
                                 {  
1859                                          int bSkip = 1;                                          int bSkip = 1;
1860    
1861                                          for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)                                  if (current->coding_type == P_VOP) {    /* special rule for P-VOP's SKIP */
1862                                          {                                          for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) {
1863                                                  int iSAD;                                                  int iSAD;
1864                                                  iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                  iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x,
1865                                                                          pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,                                                                                  pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x,
1866                                                                  pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);                                                                                  pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant);
1867                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)                                                  if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant || ((bound > 1) &&
1868                                                  {       bSkip = 0;                                                          ((y*mb_width+x == bound) || (y*mb_width+x == bound+1)))) { /* Some third-party decoders have problems with coloc skip MB before or after
1869                                                          break;                                                                                                                                                                             resync marker in BVOP. We avoid any ambiguity and force no skip at slice boundary */
1870                                                  }                                                          bSkip = 0; /* could not SKIP */
1871                                          }                                                          if (pParam->vol_flags & XVID_VOL_QUARTERPEL) {
1872                                                                    VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
                                         if (!bSkip) {   /* no SKIP, but trivial block */  
                                                 if(pEnc->mbParam.m_quarterpel) {  
                                                         VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
1873                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1874                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1875                                                  }                                                          } else {
1876                                                  else {                                                                  VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, bound, x, y, 0);
                                                         VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
1877                                                          pMB->pmvs[0].x = - predMV.x;                                                          pMB->pmvs[0].x = - predMV.x;
1878                                                          pMB->pmvs[0].y = - predMV.y;                                                          pMB->pmvs[0].y = - predMV.y;
1879                                                  }                                                  }
1880                                                  pMB->mode = MODE_INTER;                                                  pMB->mode = MODE_INTER;
1881                                                  pMB->cbp = 0;                                                  pMB->cbp = 0;
1882                                                  MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                                                          break;
1883                                                  stop_coding_timer();                                                  }
   
                                                 continue;       /* next MB */  
1884                                          }                                          }
1885                                  }                                  }
                                 /* do SKIP */  
1886    
1887                                    if (bSkip) {
1888                                            /* do SKIP */
1889                                  pMB->mode = MODE_NOT_CODED;                                  pMB->mode = MODE_NOT_CODED;
1890                                  MBSkip(bs);                                  MBSkip(bs);
1891                                  stop_coding_timer();                                  stop_coding_timer();
1892                                  continue;       /* next MB */                                  continue;       /* next MB */
1893                          }                          }
1894                            }
1895    
1896                          /* ordinary case: normal coded INTER/INTER4V block */                          /* ordinary case: normal coded INTER/INTER4V block */
1897                            MBCoding(current, pMB, qcoeff, bs, data->sStat);
1898                            stop_coding_timer();
1899                    }
1900            }
1901    
1902            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
1903            emms();
1904    }
1905    
1906    /* FrameCodeP also handles S(GMC)-VOPs */
1907    static int
1908    FrameCodeP(Encoder * pEnc, Bitstream * bs)
1909    {
1910            int bits = BitstreamPos(bs);
1911    
1912            FRAMEINFO *const current = pEnc->current;
1913            FRAMEINFO *const reference = pEnc->reference;
1914            MBParam * const pParam = &pEnc->mbParam;
1915            int mb_width = pParam->mb_width;
1916            int mb_height = pParam->mb_height;
1917            int coded = 1;
1918    
1919            int k = 0, bound = 0, num_slices = pEnc->num_slices;
1920            int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
1921    #ifdef HAVE_PTHREAD
1922            void * status = NULL;
1923            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
1924    #endif
1925            int slices_per_thread = (num_slices*1024 / num_threads);
1926    
1927            IMAGE *pRef = &reference->image;
1928    
1929            if (!reference->is_edged) {
1930                    start_timer();
1931                    image_setedges(pRef, pParam->edged_width, pParam->edged_height,
1932                                               pParam->width, pParam->height, XVID_BS_VERSION);
1933                    stop_edges_timer();
1934                    reference->is_edged = 1;
1935            }
1936    
1937            pParam->m_rounding_type = 1 - pParam->m_rounding_type;
1938            current->rounding_type = pParam->m_rounding_type;
1939            current->fcode = pParam->m_fcode;
1940    
1941            if ((current->vop_flags & XVID_VOP_HALFPEL)) {
1942                    if (reference->is_interpolated != current->rounding_type) {
1943                            start_timer();
1944                            image_interpolate(pRef->y, pEnc->vInterH.y, pEnc->vInterV.y,
1945                                                              pEnc->vInterHV.y, pParam->edged_width,
1946                                                              pParam->edged_height,
1947                                                              (pParam->vol_flags & XVID_VOL_QUARTERPEL),
1948                                                              current->rounding_type);
1949                            stop_inter_timer();
1950                            reference->is_interpolated = current->rounding_type;
1951                    }
1952            }
1953    
1954            current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount =
1955                    current->sStat.kblks = current->sStat.mblks = current->sStat.ublks =
1956                    current->sStat.iMVBits = 0;
1957    
1958                          if (pEnc->current->global_flags & XVID_GREYSCALE)          current->coding_type = P_VOP;
1959                          {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */  
1960                                  qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */          if (current->vop_flags & XVID_VOP_RD_PSNRHVSM) {
1961                                  qcoeff[5*64+0]=0;                  image_block_variance(&current->image, pParam->edged_width, current->mbs,
1962                                         pParam->mb_width, pParam->mb_height);
1963                          }                          }
1964    
1965                          if(pEnc->mbParam.m_quarterpel) {          call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
1966                                  VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);  
1967                                  pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;          SetMacroblockQuants(&pEnc->mbParam, current);
1968                                  pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;  
1969                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);          start_timer();
1970            if (current->vol_flags & XVID_VOL_GMC)  /* GMC only for S(GMC)-VOPs */
1971            {       int gmcval;
1972                    current->warp = GlobalMotionEst( current->mbs, pParam, current, reference,
1973                                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, num_slices);
1974    
1975                    if (current->motion_flags & XVID_ME_GME_REFINE) {
1976                            gmcval = GlobalMotionEstRefine(&current->warp,
1977                                                                                       current->mbs, pParam,
1978                                                                                       current, reference,
1979                                                                                       &current->image,
1980                                                                                       &reference->image,
1981                                                                                       &pEnc->vInterH,
1982                                                                                       &pEnc->vInterV,
1983                                                                                       &pEnc->vInterHV);
1984                          } else {                          } else {
1985                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);                          gmcval = globalSAD(&current->warp, pParam, current->mbs,
1986                                  pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;                                                             current,
1987                                  pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;                                                             &reference->image,
1988                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);                                                             &current->image,
1989                                                               pEnc->vGMC.y);
1990                          }                          }
1991    
1992                    gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height);
1993    
1994                    /* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */
1995                    generate_GMCparameters( 3, 3, &current->warp,
1996                                    pParam->width, pParam->height,
1997                                    &current->new_gmc_data);
1998    
1999                    if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) ||
2000                             (current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) )
2001                    {
2002                            current->coding_type = S_VOP;
2003    
2004                            generate_GMCimage(&current->new_gmc_data, &reference->image,
2005                                    pParam->mb_width, pParam->mb_height,
2006                                    pParam->edged_width, pParam->edged_width/2,
2007                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
2008                                    current->rounding_type, current->mbs, &pEnc->vGMC);
2009    
                         if (pMB->mode == MODE_INTER4V)  
                         {       int k;  
                                 for (k=1;k<4;k++)  
                                 {  
                                         if(pEnc->mbParam.m_quarterpel) {  
                                                 VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);  
                                                 pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;  
                                                 pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;  
                                 DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);  
2010                                          } else {                                          } else {
2011                                                  VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);  
2012                                                  pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;                          generate_GMCimage(&current->new_gmc_data, &reference->image,
2013                                                  pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;                                  pParam->mb_width, pParam->mb_height,
2014                                  DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);                                  pParam->edged_width, pParam->edged_width/2,
2015                                    pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0,
2016                                    current->rounding_type, current->mbs, NULL);    /* no warping, just AMV */
2017                    }
2018            }
2019    
2020    #ifdef HAVE_PTHREAD
2021            if (pEnc->num_threads > 0) {
2022    
2023                    /* multithreaded motion estimation - dispatch threads */
2024                    while (k < pEnc->num_threads) {
2025                            int i, add_s = (slices_per_thread + 512) >> 10;
2026                            int add_t = (threads_per_slice + 512) >> 10;
2027    
2028                            int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2029                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2030                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2031    
2032                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2033                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2034    
2035                            for (i = 0; i < add_t; i++) {
2036                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2037    
2038                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2039                                    pEnc->smpData[k+i].y_row = i;
2040                                    pEnc->smpData[k+i].y_step = add_t;
2041                                    pEnc->smpData[k+i].stop_y = stop_y;
2042                                    pEnc->smpData[k+i].start_y = start_y;
2043    
2044                                    /* todo: sort out temp space once and for all */
2045                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2046                                                                                            16*((k+i)>>1)*pParam->edged_width;
2047                                          }                                          }
2048    
2049                            pEnc->smpData[k].complete_count_above =
2050                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2051    
2052                            bound += add_s;
2053                            k += add_t;
2054                                  }                                  }
2055    
2056                    for (k = 1; k < pEnc->num_threads; k++) {
2057                            pthread_create(&pEnc->smpData[k].handle, NULL,
2058                                    (void*)MotionEstimateSMP, (void*)&pEnc->smpData[k]);
2059                          }                          }
2060    
2061                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);                  MotionEstimateSMP(&pEnc->smpData[0]);
                         stop_coding_timer();  
2062    
2063                    for (k = 1; k < pEnc->num_threads; k++) {
2064                            pthread_join(pEnc->smpData[k].handle, &status);
2065                  }                  }
2066    
2067                    current->fcode = 0;
2068                    for (k = 0; k < pEnc->num_threads; k++) {
2069                            current->sStat.iMvSum += pEnc->smpData[k].mvSum;
2070                            current->sStat.iMvCount += pEnc->smpData[k].mvCount;
2071                            if (pEnc->smpData[k].minfcode > current->fcode)
2072                                    current->fcode = pEnc->smpData[k].minfcode;
2073          }          }
2074    
2075          if ((pEnc->current->global_flags & XVID_REDUCED))          } else
2076    #endif
2077          {          {
                 image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,  
                         pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,  
                         16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);  
         }  
2078    
2079          emms();                  /* regular ME */
2080    
2081                    MotionEstimation(&pEnc->mbParam, current, reference,
2082                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2083                                                     &pEnc->vGMC, 256*4096, num_slices);
2084    
         if (pEnc->current->global_flags & XVID_HINTEDME_GET) {  
                 HintedMEGet(pEnc, 0);  
2085          }          }
2086    
2087          if (pEnc->current->sStat.iMvCount == 0)          stop_motion_timer();
                 pEnc->current->sStat.iMvCount = 1;  
2088    
2089          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);          set_timecodes(current,reference,pParam->fbase);
2090    
2091          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant);
2092    
2093          if ((fSigma > iSearchRange / 3)          /* multithreaded inter coding - dispatch threads */
2094                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128  
2095          {          bound = 0;
2096                  pEnc->mbParam.m_fcode++;          slices_per_thread = (num_slices*1024 / num_threads);
2097                  iSearchRange *= 2;  
2098          } else if ((fSigma < iSearchRange / 6)          for (k = 0; k < num_threads; k++) {
2099                             && (pEnc->fMvPrevSigma >= 0)                  int add = ((slices_per_thread + 512) >> 10);
2100                             && (pEnc->fMvPrevSigma < iSearchRange / 6)  
2101                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
2102          {  
2103                  pEnc->mbParam.m_fcode--;                  pEnc->smpData[k].pEnc = (void *) pEnc;
2104                  iSearchRange /= 2;                  pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2105                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2106                    pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2107    
2108                    bound += add;
2109    
2110                    if (k > 0) {
2111                            pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2112                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks =
2113                            pEnc->smpData[k].sStat->iMVBits = 0;
2114    
2115                            BitstreamReset(pEnc->smpData[k].bs);
2116          }          }
2117            }
2118            pEnc->smpData[0].bs = bs;
2119            pEnc->smpData[0].sStat = &current->sStat;
2120    
2121          pEnc->fMvPrevSigma = fSigma;  #ifdef HAVE_PTHREAD
2122            /* create threads */
2123            for (k = 1; k < num_threads; k++) {
2124                    pthread_create(&pEnc->smpData[k].handle, NULL,
2125                            (void*)SliceCodeP, (void*)&pEnc->smpData[k]);
2126            }
2127    #endif
2128    
2129            SliceCodeP(&pEnc->smpData[0]);
2130    
2131    #ifdef HAVE_PTHREAD
2132            /* wait until all threads are finished */
2133            for (k = 1; k < num_threads; k++) {
2134                    pthread_join(pEnc->smpData[k].handle, &status);
2135            }
2136    #endif
2137    
2138            current->length = BitstreamLength(bs) - (bits/8);
2139    
2140            /* reassemble the pieces together */
2141            SerializeBitstreams(pEnc, pEnc->current, bs, num_threads);
2142    
2143            updateFcode(&current->sStat, pEnc);
2144    
2145          /* frame drop code */          /* frame drop code */
2146          DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);  #if 0
2147          if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <          DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks);
2148                  (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)  #endif
2149    
2150            if (current->sStat.kblks + current->sStat.mblks <
2151                    (pParam->frame_drop_ratio * mb_width * mb_height) / 100 &&
2152                    ( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) &&
2153                    (current->coding_type == P_VOP) )
2154          {          {
2155                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;                  current->sStat.kblks = current->sStat.mblks = current->sStat.iTextBits = 0;
2156                  pEnc->current->sStat.ublks = mb_width * mb_height;                  current->sStat.ublks = mb_width * mb_height;
2157    
2158                  BitstreamReset(bs);                  BitstreamReset(bs);
2159    
2160                  set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);                  set_timecodes(current,reference,pParam->fbase);
2161                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);                  BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant);
2162    
2163                    /* copy reference frame details into the current frame */
2164                    current->quant = reference->quant;
2165                    current->motion_flags = reference->motion_flags;
2166                    current->rounding_type = reference->rounding_type;
2167                    current->fcode = reference->fcode;
2168                    current->bcode = reference->bcode;
2169                    current->stamp = reference->stamp;
2170                    image_copy(&current->image, &reference->image, pParam->edged_width, pParam->height);
2171                    memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2172                    coded = 0;
2173    
2174                    BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2175    
2176                  // copy reference frame details into the current frame                  current->length = (BitstreamPos(bs) - bits) / 8;
2177                  pEnc->current->quant = pEnc->reference->quant;  
2178                  pEnc->current->motion_flags = pEnc->reference->motion_flags;          } else {
2179                  pEnc->current->rounding_type = pEnc->reference->rounding_type;  
2180                  pEnc->current->quarterpel =  pEnc->reference->quarterpel;                  pEnc->current->is_edged = 0; /* not edged */
2181                  pEnc->current->fcode = pEnc->reference->fcode;                  pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */
2182                  pEnc->current->bcode = pEnc->reference->bcode;  
2183                  image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  /* what was this frame's interpolated reference will become
2184                  memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);                          forward (past) reference in b-frame coding */
2185    
2186                    image_swap(&pEnc->vInterH, &pEnc->f_refh);
2187                    image_swap(&pEnc->vInterV, &pEnc->f_refv);
2188                    image_swap(&pEnc->vInterHV, &pEnc->f_refhv);
2189          }          }
2190    
2191          /* XXX: debug          /* XXX: debug
2192          {          {
2193                  char s[100];                  char s[100];
2194                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2195                  image_dump_yuvpgm(&pEnc->current->image,                  image_dump_yuvpgm(&current->image,
2196                          pEnc->mbParam.edged_width,                          pParam->edged_width,
2197                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
2198    
2199                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);                  sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2200                  image_dump_yuvpgm(&pEnc->reference->image,                  image_dump_yuvpgm(&reference->image,
2201                          pEnc->mbParam.edged_width,                          pParam->edged_width,
2202                          pEnc->mbParam.width, pEnc->mbParam.height, s);                          pParam->width, pParam->height, s);
2203          }          }
2204          */          */
2205    
2206            return coded;
2207    }
2208    
2209    static void
2210    SliceCodeB(SMPData *data)
2211    {
2212            Encoder *pEnc = (Encoder *) data->pEnc;
2213            Bitstream *bs = (Bitstream *) data->bs;
2214    
2215            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2216            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2217    
2218            int x, y;
2219            FRAMEINFO * const frame = (FRAMEINFO * const) data->current;
2220            MBParam * const pParam = &pEnc->mbParam;
2221            int mb_width = pParam->mb_width;
2222            int mb_height = pParam->mb_height;
2223            IMAGE *f_ref = &pEnc->reference->image;
2224            IMAGE *b_ref = &pEnc->current->image;
2225    
2226            int bound = data->start_y*mb_width;
2227            int num_slices = pEnc->num_slices;
2228    
2229          *pBits = BitstreamPos(bs) - *pBits;          if (data->start_y > 0) { /* write resync marker */
2230                    write_video_packet_header(bs, pParam, frame, bound+1);
2231            }
2232    
2233            for (y = data->start_y; y < MIN(data->stop_y+1, mb_height); y++) {
2234                    int new_bound = mb_width * ((((y*num_slices) / mb_height) * mb_height + (num_slices-1)) / num_slices);
2235                    int stop_x = (y == data->stop_y) ? 1 : mb_width;
2236                    int start_x = (y == data->start_y && y > 0) ? 1 : 0;
2237    
2238                    for (x = start_x; x < stop_x; x++) {
2239                            MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2240    
2241                            /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
2242                            if (mb->mode == MODE_NOT_CODED) {
2243                                    if (pParam->plugin_flags & XVID_REQORIGINAL) {
2244                                            MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image,
2245                                                                                     NULL, 0, 0, pParam->edged_width, 0, 0, data->RefQ);
2246                                    }
2247                                    continue;
2248                            }
2249    
2250                            if (new_bound > bound && x > 0) {
2251                                    bound = new_bound;
2252                                    BitstreamPadAlways(bs);
2253                                    write_video_packet_header(bs, pParam, frame, y*mb_width+x);
2254                            }
2255    
2256                            mb->quant = frame->quant;
2257    
2258                            if (mb->cbp != 0 || pParam->plugin_flags & XVID_REQORIGINAL) {
2259                                    /* we have to motion-compensate, transfer etc,
2260                                            because there might be blocks to code */
2261    
2262                                    MBMotionCompensationBVOP(pParam, mb, x, y, &frame->image,
2263                                                                                     f_ref, &pEnc->f_refh, &pEnc->f_refv,
2264                                                                                     &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2265                                                                                     &pEnc->vInterV, &pEnc->vInterHV, dct_codes,
2266                                                                                     data->RefQ);
2267    
2268                                    mb->cbp = MBTransQuantInterBVOP(pParam, frame, mb, x, y,  dct_codes, qcoeff);
2269                            }
2270    
2271          return 0;                                       // inter                          if (mb->mode == MODE_DIRECT_NO4V)
2272                                    mb->mode = MODE_DIRECT;
2273    
2274                            if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0)
2275                                    mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
2276                            else
2277                                    if (frame->vop_flags & XVID_VOP_GREYSCALE)
2278                                            /* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */
2279                                            mb->cbp &= 0x3C;
2280    
2281                            start_timer();
2282                            MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, data->sStat);
2283                            stop_coding_timer();
2284                    }
2285  }  }
2286    
2287            BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */
2288            emms();
2289    }
2290    
2291  static void  static void
2292  FrameCodeB(Encoder * pEnc,  FrameCodeB(Encoder * pEnc,
2293                     FRAMEINFO * frame,                     FRAMEINFO * frame,
2294                     Bitstream * bs,                     Bitstream * bs)
                    uint32_t * pBits)  
2295  {  {
2296          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          int bits = BitstreamPos(bs);
2297          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);          int k = 0, bound = 0, num_slices = pEnc->num_slices;
2298          uint32_t x, y;          int num_threads = MAX(1, MIN(pEnc->num_threads, num_slices));
2299    #ifdef HAVE_PTHREAD
2300            void * status = NULL;
2301            int threads_per_slice = (pEnc->num_threads*1024 / num_threads);
2302    #endif
2303            int slices_per_thread = (num_slices*1024 / num_threads);
2304    
2305          IMAGE *f_ref = &pEnc->reference->image;          IMAGE *f_ref = &pEnc->reference->image;
2306          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2307    
2308            MBParam * const pParam = &pEnc->mbParam;
2309            int mb_height = pParam->mb_height;
2310    
2311  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2312          FILE *fp;          FILE *fp;
2313          static char first=0;          static char first=0;
# Line 2117  Line 2315 
2315                  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); \
2316          }          }
2317    
         pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */  
   
2318          if (!first){          if (!first){
2319                  fp=fopen("C:\\XVIDDBGE.TXT","w");                  fp=fopen("C:\\XVIDDBGE.TXT","w");
2320          }          }
2321  #endif  #endif
2322    
2323          frame->quarterpel =  pEnc->mbParam.m_quarterpel;          /* forward  */
2324            if (!pEnc->reference->is_edged) {
         // forward  
2325          image_setedges(f_ref, pEnc->mbParam.edged_width,          image_setedges(f_ref, pEnc->mbParam.edged_width,
2326                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2327                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2328                    pEnc->reference->is_edged = 1;
2329            }
2330    
2331            if (pEnc->reference->is_interpolated != 0) {
2332          start_timer();          start_timer();
2333          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  image_interpolate(f_ref->y, pEnc->f_refh.y, pEnc->f_refv.y, pEnc->f_refhv.y,
2334                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2335                                            pEnc->mbParam.m_quarterpel, 0);                                                    (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2336          stop_inter_timer();          stop_inter_timer();
2337                    pEnc->reference->is_interpolated = 0;
2338            }
2339    
2340          // backward          /* backward */
2341            if (!pEnc->current->is_edged) {
2342          image_setedges(b_ref, pEnc->mbParam.edged_width,          image_setedges(b_ref, pEnc->mbParam.edged_width,
2343                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,                                     pEnc->mbParam.edged_height, pEnc->mbParam.width,
2344                                     pEnc->mbParam.height);                                             pEnc->mbParam.height, XVID_BS_VERSION);
2345                    pEnc->current->is_edged = 1;
2346            }
2347    
2348            if (pEnc->current->is_interpolated != 0) {
2349          start_timer();          start_timer();
2350          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(b_ref->y, pEnc->vInterH.y, pEnc->vInterV.y, pEnc->vInterHV.y,
2351                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2352                                            pEnc->mbParam.m_quarterpel, 0);                                                  (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0);
2353          stop_inter_timer();          stop_inter_timer();
2354                    pEnc->current->is_interpolated = 0;
2355            }
2356    
2357            frame->coding_type = B_VOP;
2358    
2359            if ((frame->vop_flags & XVID_VOP_RD_PSNRHVSM) && (frame->vop_flags & XVID_VOP_RD_BVOP)) {
2360                    image_block_variance(&frame->image, pEnc->mbParam.edged_width, frame->mbs,
2361                                         pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);
2362            }
2363    
2364            call_plugins(pEnc, frame, NULL, XVID_PLG_FRAME, NULL, NULL, NULL);
2365    
2366            frame->fcode = frame->bcode = pEnc->current->fcode;
2367    
2368          start_timer();          start_timer();
2369    
2370          MotionEstimationBVOP(&pEnc->mbParam, frame,  #ifdef HAVE_PTHREAD
2371                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp          if (pEnc->num_threads > 0) {
                 ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp  
                         pEnc->reference->mbs, f_ref,  
                                                  &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,  
                                                  pEnc->current, b_ref, &pEnc->vInterH,  
                                                  &pEnc->vInterV, &pEnc->vInterHV);  
2372    
2373                    /* multithreaded motion estimation - dispatch threads */
2374                    while (k < pEnc->num_threads) {
2375                            int i, add_s = (slices_per_thread + 512) >> 10;
2376                            int add_t = (threads_per_slice + 512) >> 10;
2377    
2378          stop_motion_timer();                          int start_y = (bound * mb_height + (num_slices-1)) / num_slices;
2379                            int stop_y = ((bound+add_s) * mb_height + (num_slices-1)) / num_slices;
2380                            int rows_per_thread = (stop_y - start_y + add_t - 1) / add_t;
2381    
2382                            slices_per_thread += ((num_slices*1024 / num_threads) - add_s*1024);
2383                            threads_per_slice += ((pEnc->num_threads*1024 / num_threads) - add_t*1024);
2384    
2385                            for (i = 0; i < add_t; i++) {
2386                                    memset(pEnc->smpData[k+i].complete_count_self, 0, rows_per_thread * sizeof(int));
2387    
2388                                    pEnc->smpData[k+i].pEnc = (void *) pEnc;
2389                                    pEnc->smpData[k+i].current = frame;
2390    
2391                                    pEnc->smpData[k+i].y_row = i;
2392                                    pEnc->smpData[k+i].y_step = add_t;
2393                                    pEnc->smpData[k+i].stop_y = stop_y;
2394                                    pEnc->smpData[k+i].start_y = start_y;
2395    
2396                                    /* todo: sort out temp space once and for all */
2397                                    pEnc->smpData[k+i].RefQ = (((k+i)&1) ? pEnc->vInterV.u : pEnc->vInterV.v) +
2398                                                                                            16*((k+i)>>1)*pParam->edged_width;
2399                            }
2400    
2401                            pEnc->smpData[k].complete_count_above =
2402                                    pEnc->smpData[k+add_t-1].complete_count_self - 1;
2403    
2404                            bound += add_s;
2405                            k += add_t;
2406                    }
2407    
2408                    for (k = 1; k < pEnc->num_threads; k++) {
2409                            pthread_create(&pEnc->smpData[k].handle, NULL,
2410                                    (void*)SMPMotionEstimationBVOP, (void*)&pEnc->smpData[k]);
2411                    }
2412    
2413          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))                  SMPMotionEstimationBVOP(&pEnc->smpData[0]);
2414    
2415                    for (k = 1; k < pEnc->num_threads; k++) {
2416                            pthread_join(pEnc->smpData[k].handle, &status);
2417                    }
2418    
2419                    frame->fcode = frame->bcode = 0;
2420                    for (k = 0; k < pEnc->num_threads; k++) {
2421                            if (pEnc->smpData[k].minfcode > frame->fcode)
2422                                    frame->fcode = pEnc->smpData[k].minfcode;
2423                            if (pEnc->smpData[k].minbcode > frame->bcode)
2424                                    frame->bcode = pEnc->smpData[k].minbcode;
2425                    }
2426            } else
2427    #endif
2428             {             {
            BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);  
            } */  
2429    
2430          frame->coding_type = B_VOP;                  MotionEstimationBVOP(&pEnc->mbParam, frame,
2431                                                             ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
2432                                                             ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
2433                                                             pEnc->reference->mbs, f_ref,
2434                                                             &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2435                                                             pEnc->current, b_ref, &pEnc->vInterH,
2436                                                             &pEnc->vInterV, &pEnc->vInterHV,
2437                                                             pEnc->num_slices);
2438            }
2439            stop_motion_timer();
2440    
2441          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2442          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant);
   
         *pBits = BitstreamPos(bs);  
2443    
2444            /* reset stats */
2445          frame->sStat.iTextBits = 0;          frame->sStat.iTextBits = 0;
2446            frame->sStat.iMVBits = 0;
2447          frame->sStat.iMvSum = 0;          frame->sStat.iMvSum = 0;
2448          frame->sStat.iMvCount = 0;          frame->sStat.iMvCount = 0;
2449          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;          frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2450            frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
2451            frame->sStat.kblks = frame->sStat.ublks = 0;
2452    
2453            /* multithreaded inter coding - dispatch threads */
2454            bound = 0;
2455            slices_per_thread = (num_slices*1024 / num_threads);
2456    
2457          for (y = 0; y < pEnc->mbParam.mb_height; y++) {          for (k = 0; k < num_threads; k++) {
2458                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {                  int add = ((slices_per_thread + 512) >> 10);
                         MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
                         int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;  
   
                         // decoder ignores mb when refence block is INTER(0,0), CBP=0  
                         if (mb->mode == MODE_NOT_CODED) {  
                                 //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;  
                                 continue;  
                         }  
2459    
2460                          if (mb->mode != MODE_DIRECT_NONE_MV) {                  slices_per_thread += ((num_slices*1024 / num_threads) - add*1024);
                                 MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,  
                                                                          f_ref, &pEnc->f_refh, &pEnc->f_refv,  
                                                                          &pEnc->f_refhv, b_ref, &pEnc->vInterH,  
                                                                          &pEnc->vInterV, &pEnc->vInterHV,  
                                                                          dct_codes);  
2461    
2462                                  if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;                  pEnc->smpData[k].pEnc = (void *) pEnc;
2463                                  mb->quant = frame->quant;                  pEnc->smpData[k].current = frame;
2464                    pEnc->smpData[k].stop_y = (((bound+add) * mb_height + (num_slices-1)) / num_slices);
2465                    pEnc->smpData[k].start_y = ((bound * mb_height + (num_slices-1)) / num_slices);
2466                    bound += add;
2467    
2468                                  mb->cbp =                  /* todo: sort out temp space once and for all */
2469                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);                  pEnc->smpData[k].RefQ = ((k&1) ? pEnc->vInterV.u : pEnc->vInterV.v) + 16*(k>>1)*pParam->edged_width;
2470    
2471                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)                  if (k > 0) {
2472                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {                          BitstreamReset(pEnc->smpData[k].bs);
2473                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped                          pEnc->smpData[k].sStat->iTextBits = pEnc->smpData[k].sStat->kblks =
2474                            pEnc->smpData[k].sStat->mblks = pEnc->smpData[k].sStat->ublks = pEnc->smpData[k].sStat->iMVBits = 0;
2475                                  }                                  }
2476                          }                          }
2477    
2478  #ifdef BFRAMES_DEC_DEBUG  #ifdef HAVE_PTHREAD
2479          BFRAME_DEBUG          for (k = 1; k < num_threads; k++) {
2480  #endif                  pthread_create(&pEnc->smpData[k].handle, NULL,
2481                          start_timer();                          (void*)SliceCodeB, (void*)&pEnc->smpData[k]);
                         MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,  
                                                  &frame->sStat, direction);  
                         stop_coding_timer();  
                 }  
2482          }          }
2483    #endif
2484    
2485          emms();          pEnc->smpData[0].bs = bs;
2486            pEnc->smpData[0].sStat = &frame->sStat;
2487            SliceCodeB(&pEnc->smpData[0]);
2488    
2489    #ifdef HAVE_PTHREAD
2490            for (k = 1; k < num_threads; k++) {
2491                    pthread_join(pEnc->smpData[k].handle, &status);
2492            }
2493    #endif
2494    
2495          // TODO: dynamic fcode/bcode ???          frame->length = BitstreamLength(bs) - (bits/8);
2496    
2497          *pBits = BitstreamPos(bs) - *pBits;          /* reassemble the pieces together */
2498            SerializeBitstreams(pEnc, frame, bs, num_threads);
2499    
2500  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
2501          if (!first){          if (!first){

Legend:
Removed from v.1.76.2.44  
changed lines
  Added in v.1.141

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