[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.34, Fri May 3 15:26:30 2002 UTC revision 1.95.2.1, Sat Feb 22 08:49:44 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
  ***************************************************************************/  
   
 /****************************************************************************  
  *  
  *  History  
  *  
  *  14.04.2002 added FrameCodeB()  
  *  
29   *  $Id$   *  $Id$
30   *   *
31   ***************************************************************************/   ****************************************************************************/
32    
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <stdio.h>  #include <stdio.h>
35  #include <math.h>  #include <math.h>
36    #include <string.h>
37    
38  #include "encoder.h"  #include "encoder.h"
39  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
40  #include "global.h"  #include "global.h"
41  #include "utils/timer.h"  #include "utils/timer.h"
42  #include "image/image.h"  #include "image/image.h"
43    #include "image/font.h"
44    #include "motion/sad.h"
45  #include "motion/motion.h"  #include "motion/motion.h"
46  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
47  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 59  Line 54 
54  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
55  #include "utils/mem_align.h"  #include "utils/mem_align.h"
56    
57  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
58  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }   * Local macros
59     ****************************************************************************/
60    
61    #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
62    
63  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
64  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
65  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits);   ****************************************************************************/
66    
67  static int DQtab[4] =  static int FrameCodeI(Encoder * pEnc,
68  {                                            Bitstream * bs);
69    
70    static int FrameCodeP(Encoder * pEnc,
71                                              Bitstream * bs,
72                                              bool force_inter,
73                                              bool vol_header);
74    
75    static void FrameCodeB(Encoder * pEnc,
76                                               FRAMEINFO * frame,
77                                               Bitstream * bs);
78    
79    /*****************************************************************************
80     * Local data
81     ****************************************************************************/
82    
83    static int DQtab[4] = {
84          -1, -2, 1, 2          -1, -2, 1, 2
85  };  };
86    
87  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
88          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
89  };  };
90    
91    
92  void static image_null(IMAGE * image)  /*****************************************************************************
93  {   * Encoder creation
94          image->y = image->u = image->v = NULL;   *
95     * This function creates an Encoder instance, it allocates all necessary
96     * image buffers (reference, current and bframes) and initialize the internal
97     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
98     *
99     * The code seems to be very long but is very basic, mainly memory allocation
100     * and cleaning code.
101     *
102     * Returned values :
103     *    - 0                - no errors
104     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
105     *                        cleans the structure before exiting.
106     *                        pParam->handle is also set to NULL.
107     *
108     ****************************************************************************/
109    
110    /*
111     * Simplify the "fincr/fbase" fraction
112    */
113    static void
114    simplify_time(int *inc, int *base)
115    {
116            /* common factor */
117            int i = *inc;
118            while (i > 1) {
119                    if (*inc % i == 0 && *base % i == 0) {
120                            *inc /= i;
121                            *base /= i;
122                            i = *inc;
123                            continue;
124                    }
125                    i--;
126  }  }
127    
128            /* if neccessary, round to 65535 accuracy */
129            if (*base > 65535) {
130                    float div = (float) *base / 65535;
131                    *base = (int) (*base / div);
132                    *inc = (int) (*inc / div);
133            }
134    }
135    
136  int encoder_create(XVID_ENC_PARAM * pParam)  
137    int
138    enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc)
139  {  {
140          Encoder *pEnc;          Encoder *pEnc;
141          uint32_t i;      int n;
142    
143          pParam->handle = NULL;          if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1))   /* v1.x.x */
144                    return XVID_ERR_VERSION;
145    
146          ENC_CHECK(pParam);          if (create->width%2 || create->height%2)
147                    return XVID_ERR_FAIL;
148    
149          ENC_CHECK(pParam->width > 0 && pParam->width <= 1920);          /* allocate encoder struct */
         ENC_CHECK(pParam->height > 0 && pParam->height <= 1280);  
         ENC_CHECK(!(pParam->width % 2));  
         ENC_CHECK(!(pParam->height % 2));  
150    
151          if (pParam->fincr <= 0 || pParam->fbase <= 0)          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
152          {          if (pEnc == NULL)
153                  pParam->fincr = 1;                  return XVID_ERR_MEMORY;
154                  pParam->fbase = 25;          memset(pEnc, 0, sizeof(Encoder));
         }  
155    
156          // simplify the "fincr/fbase" fraction          /* global flags */
157          // (neccessary, since windows supplies us with huge numbers)      pEnc->mbParam.global_flags = create->global;
158    
159          i = pParam->fincr;      /* width, height */
160          while (i > 1)          pEnc->mbParam.width = create->width;
161          {          pEnc->mbParam.height = create->height;
162                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0)          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;
163                  {          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;
164                          pParam->fincr /= i;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
165                          pParam->fbase /= i;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
                         i = pParam->fincr;  
                         continue;  
                 }  
                 i--;  
         }  
166    
167          if (pParam->fbase > 65535)      /* framerate */
168          {      pEnc->mbParam.fincr = MAX(create->fincr, 0);
169                  float div = (float)pParam->fbase / 65535;          pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase;
170                  pParam->fbase = (int)(pParam->fbase / div);          simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase);
                 pParam->fincr = (int)(pParam->fincr / div);  
         }  
171    
172          if (pParam->rc_bitrate <= 0)          /* bframes */
173                  pParam->rc_bitrate = 900000;          pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0);
174            pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0);
175            pEnc->mbParam.bquant_offset = create->bquant_offset;
176    
177          if (pParam->rc_reaction_delay_factor <= 0)          /* frame drop ratio */
178                  pParam->rc_reaction_delay_factor = 16;          pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0);
179    
180          if (pParam->rc_averaging_period <= 0)      /* max keyframe interval */
181                  pParam->rc_averaging_period = 100;      pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval;
182        /*XXX: replace 250 hard code with "10seconds * framerate" */
183    
184          if (pParam->rc_buffer <= 0)          /* Bitrate allocator defaults
                 pParam->rc_buffer = 100;  
185    
186          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if (create->rc_bitrate <= 0)
187                  pParam->min_quantizer = 1;                  create->rc_bitrate = 900000;
188    
189          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if (create->rc_reaction_delay_factor <= 0)
190                  pParam->max_quantizer = 31;                  create->rc_reaction_delay_factor = 16;
191    
192          if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */          if (create->rc_averaging_period <= 0)
193                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  create->rc_averaging_period = 100;
194    
195          if (pParam->max_quantizer < pParam->min_quantizer)          if (create->rc_buffer <= 0)
196                  pParam->max_quantizer = pParam->min_quantizer;                  create->rc_buffer = 100;
197    
         if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)  
                 return XVID_ERR_MEMORY;  
198    
         /* Fill members of Encoder structure */  
199    
200          pEnc->mbParam.width = pParam->width;          if ((create->min_quantizer <= 0) || (create->min_quantizer > 31))
201          pEnc->mbParam.height = pParam->height;                  create->min_quantizer = 1;
202    
203          pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16;          if ((create->max_quantizer <= 0) || (create->max_quantizer > 31))
204          pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16;                  create->max_quantizer = 31;
205    
206          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          if (create->max_quantizer < create->min_quantizer)
207          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;                  create->max_quantizer = create->min_quantizer;
   
         pEnc->mbParam.fbase = pParam->fbase;  
         pEnc->mbParam.fincr = pParam->fincr;  
208    
209          pEnc->sStat.fMvPrevSigma = -1;          pEnc->bitrate = create->rc_bitrate; */
210    
         /* Fill rate control parameters */  
211    
212          pEnc->bitrate = pParam->rc_bitrate;      /* allocate working frame-image memory */
   
         pEnc->iFrameNum = 0;  
         pEnc->iMaxKeyInterval = pParam->max_key_interval;  
213    
214          /* try to allocate frame memory */          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
215            pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
216    
217          pEnc->current = NULL;          if (pEnc->current == NULL || pEnc->reference == NULL)
218          pEnc->reference = NULL;                  goto xvid_err_memory1;
         if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL ||  
                  (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL)  
         {  
                 if (pEnc->current) xvid_free(pEnc->current);  
                 xvid_free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
219    
220          /* try to allocate mb memory */          /* allocate macroblock memory */
221    
222          pEnc->current->mbs = NULL;          pEnc->current->mbs =
223          pEnc->reference->mbs = NULL;                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
224                                            pEnc->mbParam.mb_height, CACHE_LINE);
225            pEnc->reference->mbs =
226                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
227                                            pEnc->mbParam.mb_height, CACHE_LINE);
228    
229          if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL ||          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
230                  (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)                  goto xvid_err_memory2;
         {  
                 if (pEnc->current->mbs) xvid_free(pEnc->current->mbs);  
                 xvid_free(pEnc->current);  
                 xvid_free(pEnc->reference);  
                 xvid_free(pEnc);  
         }  
231    
232          /* try to allocate image memory */          /* allocate interpolation image memory */
233    
234  #ifdef _DEBUG          if (create->global & XVID_EXTRASTATS_ENABLE)
235          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
236  #endif  
 #ifdef BFRAMES  
237          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
238          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
239          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
240  #endif  
241          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
242          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
243          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 221  Line 246 
246          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
247          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
248    
249          if (          if (create->global & XVID_EXTRASTATS_ENABLE)
250  #ifdef _DEBUG          {       if (image_create
251                  image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                          (&pEnc->sOriginal, pEnc->mbParam.edged_width,
252  #endif                           pEnc->mbParam.edged_height) < 0)
253  #ifdef BFRAMES                          goto xvid_err_memory3;
254                  image_create(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          }
255                  image_create(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
256                  image_create(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create
257  #endif                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
258                  image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                   pEnc->mbParam.edged_height) < 0)
259                  image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  goto xvid_err_memory3;
260                  image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||          if (image_create
261                  image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
262                  image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                   pEnc->mbParam.edged_height) < 0)
263                  image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||                  goto xvid_err_memory3;
264                  image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          if (image_create
265                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
266                     pEnc->mbParam.edged_height) < 0)
267                    goto xvid_err_memory3;
268    
269            if (image_create
270                    (&pEnc->current->image, pEnc->mbParam.edged_width,
271                     pEnc->mbParam.edged_height) < 0)
272                    goto xvid_err_memory3;
273            if (image_create
274                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
275                     pEnc->mbParam.edged_height) < 0)
276                    goto xvid_err_memory3;
277            if (image_create
278                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
279                     pEnc->mbParam.edged_height) < 0)
280                    goto xvid_err_memory3;
281            if (image_create
282                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
283                     pEnc->mbParam.edged_height) < 0)
284                    goto xvid_err_memory3;
285            if (image_create
286                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
287                     pEnc->mbParam.edged_height) < 0)
288                    goto xvid_err_memory3;
289            if (image_create
290                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
291                     pEnc->mbParam.edged_height) < 0)
292                    goto xvid_err_memory3;
293            if (image_create
294                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
295                     pEnc->mbParam.edged_height) < 0)
296                    goto xvid_err_memory3;
297    
298    /* Create full bitplane for GMC, this might be wasteful */
299            if (image_create
300                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
301                     pEnc->mbParam.edged_height) < 0)
302                    goto xvid_err_memory3;
303    
304            /* init bframe image buffers */
305    
306            pEnc->bframenum_head = 0;
307            pEnc->bframenum_tail = 0;
308            pEnc->flush_bframes = 0;
309            pEnc->closed_bframenum = -1;
310    
311            /* B Frames specific init */
312            pEnc->bframes = NULL;
313    
314            if (pEnc->mbParam.max_bframes > 0) {
315    
316                    pEnc->bframes =
317                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
318                                                    CACHE_LINE);
319    
320                    if (pEnc->bframes == NULL)
321                            goto xvid_err_memory3;
322    
323                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
324                            pEnc->bframes[n] = NULL;
325    
326    
327                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
328                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
329    
330                            if (pEnc->bframes[n] == NULL)
331                                    goto xvid_err_memory4;
332    
333                            pEnc->bframes[n]->mbs =
334                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
335                                                            pEnc->mbParam.mb_height, CACHE_LINE);
336    
337                            if (pEnc->bframes[n]->mbs == NULL)
338                                    goto xvid_err_memory4;
339    
340                            image_null(&pEnc->bframes[n]->image);
341    
342                            if (image_create
343                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
344                                     pEnc->mbParam.edged_height) < 0)
345                                    goto xvid_err_memory4;
346    
347                    }
348            }
349    
350        /* init incoming frame queue */
351            pEnc->queue_head = 0;
352            pEnc->queue_tail = 0;
353            pEnc->queue_size = 0;
354    
355            pEnc->queue =
356                    xvid_malloc((pEnc->mbParam.max_bframes+1) * sizeof(QUEUEINFO),
357                                            CACHE_LINE);
358    
359            if (pEnc->queue == NULL)
360                    goto xvid_err_memory4;
361    
362            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
363                    image_null(&pEnc->queue[n].image);
364    
365    
366            for (n = 0; n < pEnc->mbParam.max_bframes+1; n++)
367          {          {
368  #ifdef _DEBUG                  if (image_create
369                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          (&pEnc->queue[n].image, pEnc->mbParam.edged_width,
370  #endif                           pEnc->mbParam.edged_height) < 0)
371  #ifdef BFRAMES                          goto xvid_err_memory5;
372                  image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
373                  image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          }
374                  image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
375  #endif  
376                  image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* timestamp stuff */
377                  image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
378                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->mbParam.m_stamp = 0;
379                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->m_framenum = 0;
380                  image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->current->stamp = 0;
381                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->reference->stamp = 0;
382                  image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
383            /* other stuff */
384    
385            pEnc->iFrameNum = 0;
386            pEnc->fMvPrevSigma = -1;
387    
388        /* XXX: cleanup ratecontol when new code is ready */
389    /*      if (create->rc_bitrate) {
390                    RateControlInit(&pEnc->rate_control, create->rc_bitrate,
391                                                    create->rc_reaction_delay_factor,
392                                                    create->rc_averaging_period, create->rc_buffer,
393                                                    create->fbase * 1000 / create->fincr,
394                                                    create->max_quantizer, create->min_quantizer);
395            } */
396    
397        create->handle = (void *) pEnc;
398    
399            init_timer();
400    
401        return 0;   /* ok */
402    
403            /*
404             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
405             */
406    
407      xvid_err_memory5:
408    
409            if (pEnc->mbParam.max_bframes > 0) {
410            int i;
411    
412                    for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
413                            image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
414                                                      pEnc->mbParam.edged_height);
415                    }
416                    xvid_free(pEnc->queue);
417            }
418    
419      xvid_err_memory4:
420    
421            if (pEnc->mbParam.max_bframes > 0) {
422            int i;
423    
424                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
425    
426                            if (pEnc->bframes[i] == NULL)
427                                    continue;
428    
429                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
430                                                      pEnc->mbParam.edged_height);
431    
432                            xvid_free(pEnc->bframes[i]->mbs);
433    
434                            xvid_free(pEnc->bframes[i]);
435    
436                    }
437    
438                    xvid_free(pEnc->bframes);
439            }
440    
441      xvid_err_memory3:
442    
443            if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
444            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
445                                              pEnc->mbParam.edged_height);
446            }
447    
448            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
449                                      pEnc->mbParam.edged_height);
450            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
451                                      pEnc->mbParam.edged_height);
452            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
453                                      pEnc->mbParam.edged_height);
454    
455            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
456                                      pEnc->mbParam.edged_height);
457            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
458                                      pEnc->mbParam.edged_height);
459            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
460                                      pEnc->mbParam.edged_height);
461            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
462                                      pEnc->mbParam.edged_height);
463            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
464                                      pEnc->mbParam.edged_height);
465            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
466                                      pEnc->mbParam.edged_height);
467            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
468                                      pEnc->mbParam.edged_height);
469    
470    /* destroy GMC image */
471            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
472                                      pEnc->mbParam.edged_height);
473    
474    
475      xvid_err_memory2:
476            xvid_free(pEnc->current->mbs);
477            xvid_free(pEnc->reference->mbs);
478    
479      xvid_err_memory1:
480                  xvid_free(pEnc->current);                  xvid_free(pEnc->current);
481                  xvid_free(pEnc->reference);                  xvid_free(pEnc->reference);
482                  xvid_free(pEnc);                  xvid_free(pEnc);
483    
484            create->handle = NULL;
485    
486                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
487          }          }
488    
489  // ==============================================================================  /*****************************************************************************
490  #ifdef BFRAMES   * Encoder destruction
491     *
492     * This function destroy the entire encoder structure created by a previous
493     * successful enc_create call.
494     *
495     * Returned values (for now only one returned value) :
496     *    - 0     - no errors
497     *
498     ****************************************************************************/
499    
500          // TODO: handle malloc() == NULL  int
501          pEnc->max_bframes = pParam->max_bframes;  enc_destroy(Encoder * pEnc)
         pEnc->bquant_ratio = pParam->bquant_ratio;  
         if (pEnc->max_bframes > 0)  
502          {          {
503                  int n;          int i;
504    
505                  pEnc->bframes = malloc(pEnc->max_bframes * sizeof(FRAMEINFO *));          /* B Frames specific */
506            if (pEnc->mbParam.max_bframes > 0) {
507    
508                  for (n = 0; n < pEnc->max_bframes; n++)                  for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) {
                 {  
                         pEnc->bframes[n] = malloc(sizeof(FRAMEINFO));  
                         pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);  
509    
510                          if (image_create(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)                          image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width,
511                          {                                            pEnc->mbParam.edged_height);
                                 return XVID_ERR_MEMORY;  
                         }  
512                  }                  }
513                    xvid_free(pEnc->queue);
514          }          }
         pEnc->bframenum_head = 0;  
         pEnc->bframenum_tail = 0;  
         pEnc->flush_bframes = 0;  
515    
         pEnc->mbParam.m_seconds = 0;  
         pEnc->mbParam.m_ticks = 0;  
 #endif  
516    
517            if (pEnc->mbParam.max_bframes > 0) {
518    
519                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
520    
521  // ==============================================================================                          if (pEnc->bframes[i] == NULL)
522                                    continue;
523    
524                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
525                                              pEnc->mbParam.edged_height);
526    
527          pParam->handle = (void *)pEnc;                          xvid_free(pEnc->bframes[i]->mbs);
528    
529          if (pParam->rc_bitrate)                          xvid_free(pEnc->bframes[i]);
         {  
                 RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,  
                         pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,  
                         pParam->max_quantizer, pParam->min_quantizer);  
530          }          }
531    
532          init_timer();                  xvid_free(pEnc->bframes);
533    
         return XVID_ERR_OK;  
534  }  }
535    
536            /* All images, reference, current etc ... */
537    
538  int encoder_destroy(Encoder * pEnc)          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
539  {                                    pEnc->mbParam.edged_height);
540          ENC_CHECK(pEnc);          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
541                                      pEnc->mbParam.edged_height);
542            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
543                                      pEnc->mbParam.edged_height);
544            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
545                                      pEnc->mbParam.edged_height);
546            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
547                                      pEnc->mbParam.edged_height);
548            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
549                                      pEnc->mbParam.edged_height);
550            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
551                                      pEnc->mbParam.edged_height);
552    
553  // =================================================================          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
554  #ifdef BFRAMES                                    pEnc->mbParam.edged_height);
555          if (pEnc->max_bframes > 0)          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
556          {                                    pEnc->mbParam.edged_height);
557                  int n;          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
558                  for (n = 0; n < pEnc->max_bframes; n++)                                    pEnc->mbParam.edged_height);
559                  {  
560                          image_destroy(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE)
561                          free(pEnc->bframes[n]->mbs);          {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
562                          free(pEnc->bframes[n]);                                            pEnc->mbParam.edged_height);
                 }  
                 free(pEnc->bframes);  
563          }          }
 #endif  
 //====================================================================  
564    
565          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* Encoder structure */
566          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #ifdef BFRAMES  
         image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
         image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
567          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
568          xvid_free(pEnc->current);          xvid_free(pEnc->current);
569    
# Line 352  Line 571 
571          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
572    
573          xvid_free(pEnc);          xvid_free(pEnc);
         return XVID_ERR_OK;  
 }  
574    
575            return 0;  /* ok */
576    }
577    
578    
579  // ==================================================================  static __inline void inc_frame_num(Encoder * pEnc)
 #ifdef BFRAMES  
 int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
580  {  {
581          uint16_t x, y;          pEnc->current->stamp = pEnc->mbParam.m_stamp;   /* first frame is zero */
582          Bitstream bs;          pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
         uint32_t bits;  
   
         ENC_CHECK(pEnc);  
         ENC_CHECK(pFrame);  
583    
584          start_global_timer();      pEnc->m_framenum++; /* debug ticker */
585    }
586    
         BitstreamInit(&bs, pFrame->bitstream, 0);  
587    
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
588    
589          if ((pFrame->image == NULL || pEnc->flush_bframes) &&  static __inline void
590                  pEnc->bframenum_head < pEnc->bframenum_tail)  set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
591          {          {
592    
593                  if (pEnc->flush_bframes == 0)                  pCur->ticks = (int32_t)pCur->stamp % time_base;
594                  {                  pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
                         // we have reached end of stream without getting a future reference  
                         // .. so encode last final frame a pframe  
                         dprintf("--- PFRAME (final frame correction) --- ");  
                         pEnc->bframenum_tail--;  
                         SWAP(pEnc->current, pEnc->reference);  
595    
596                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  /* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */
597    
598                          FrameCodeP(pEnc, &bs, &bits, 1, 0);  /*              fprintf(stderr,"WriteVop:   %d - %d \n",
599                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
600                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
601                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
602                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
603                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
604    
605                          BitstreamPad(&bs);  */
                         pFrame->length = BitstreamLength(&bs);  
                         pFrame->input_consumed = 0;  
                         pFrame->intra = 0;  
                         return XVID_ERR_OK;  
606                  }                  }
607    
                 dprintf("--- BFRAME (flush) --- ");  
                 FrameCodeB(pEnc,  
                         pEnc->bframes[pEnc->bframenum_head],  
                          &bs, &bits);  
                 pEnc->bframenum_head++;  
   
608    
609                  BitstreamPad(&bs);  static void
610                  pFrame->length = BitstreamLength(&bs);  set_stats(xvid_enc_stats_t * stats, RateControl * rc,
611                  pFrame->input_consumed = 0;                    const MBParam * pParam, const FRAMEINFO * frame)
                 pFrame->intra = 0;  
                 return XVID_ERR_OK;  
         }  
   
         if (pFrame->image == NULL)  
612          {          {
613                  pFrame->length = 0;          if (stats)
614                  pFrame->input_consumed = 1;          {
615                  pFrame->intra = 0;                  stats->type = coding2type(frame->coding_type);
616                  return XVID_ERR_OK;                  stats->quant = frame->quant;
617                    stats->vol_flags = frame->vol_flags;
618                    stats->vop_flags = frame->vop_flags;
619                    stats->length = frame->length;
620                    stats->hlength = frame->length - (frame->sStat.iTextBits / 8);
621                    stats->kblks = frame->sStat.kblks;
622                    stats->mblks = frame->sStat.mblks;
623                    stats->ublks = frame->sStat.ublks;
624          }          }
625    
626          if (pEnc->bframenum_head > 0)          RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP );
         {  
                 pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
627          }          }
628    
         pEnc->flush_bframes = 0;  
629    
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
630    
631          SWAP(pEnc->current, pEnc->reference);  /*****************************************************************************
632     * IPB frame encoder entry point
633     *
634     * Returned values :
635     *    - >0               - output bytes
636     *    - 0                - no output
637     *    - XVID_ERR_FORMAT  - the image subsystem reported the image had a wrong
638     *                        format
639     ****************************************************************************/
640    
641    
642    /* XXX: dx50bvop broken! something todo with queue */
643    
644    int
645    enc_encode(Encoder * pEnc,
646                               xvid_enc_frame_t * xFrame,
647                               xvid_enc_stats_t * stats)
648    {
649            xvid_enc_frame_t * frame;
650            int type;
651            uint16_t x, y;
652            Bitstream bs;
653    
654            if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))     /* v1.x.x */
655                    return XVID_ERR_VERSION;
656    
657            xFrame->out_flags = 0;
658    
659          pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(0) : pFrame->quant;          start_global_timer();
660            BitstreamInit(&bs, xFrame->bitstream, 0);
661    
         if(pEnc->current->quant < 1)  
                 pEnc->current->quant = 1;  
662    
663          if(pEnc->current->quant > 31)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664                  pEnc->current->quant = 31;           * enqueue image to the encoding-queue
665             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
666    
667          pEnc->current->global_flags = pFrame->general;          if (xFrame->input.csp != XVID_CSP_NULL)
668          pEnc->current->motion_flags = pFrame->motion;          {
669          pEnc->current->seconds = pEnc->mbParam.m_seconds;                  QUEUEINFO * q = &pEnc->queue[pEnc->queue_tail];
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
         //@@@ TODO: dyanmic fcode (in both directions)  
         pEnc->current->fcode = pEnc->mbParam.m_fcode;  
         pEnc->current->bcode = pEnc->mbParam.m_fcode;  
670    
671          start_timer();          start_timer();
672          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,                  if (image_input
673                          pFrame->image, pFrame->colorspace))                          (&q->image, pEnc->mbParam.width, pEnc->mbParam.height,
674                            pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride,
675                            xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING))
676          {          {
677                            emms();
678                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
679          }          }
680          stop_conv_timer();          stop_conv_timer();
681    
682  #ifdef _DEBUG                  if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
683          image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);                          image_chroma_optimize(&q->image,
684  #endif                                  pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
685                    }
686    
687                    q->frame = *xFrame;
688    
689  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  if (xFrame->quant_intra_matrix)
690  // lumi masking                  {
691  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                          memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix));
692                            q->frame.quant_intra_matrix = q->quant_intra_matrix;
693          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  }
         {  
                 int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
   
                 pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,  
                                                             pEnc->mbParam.edged_width,  // stride  
                                                             temp_dquants,  
                                                             pEnc->current->quant,  
                                                             pEnc->current->quant,       // min_quant  
                                                             2*pEnc->current->quant,     // max_quant  
                                                             pEnc->mbParam.mb_width,  
                                                             pEnc->mbParam.mb_height);  
694    
695                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  if (xFrame->quant_inter_matrix)
                         for (x = 0; x < pEnc->mbParam.mb_width; x++)  
696                          {                          {
697                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix));
698                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          q->frame.quant_inter_matrix = q->quant_inter_matrix;
699                          }                          }
700                  xvid_free(temp_dquants);  
701                    pEnc->queue_tail = (pEnc->queue_tail + 1) % (pEnc->mbParam.max_bframes+1);
702                    pEnc->queue_size++;
703          }          }
704    
705    
706  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
707  // ivop/pvop/bvop selection           * bframe flush code
708  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
709    
710          if (pEnc->iFrameNum == 0 ||  repeat:
711                  pFrame->intra == 1 ||  
712                  (pFrame->intra < 0 && (pEnc->iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->iMaxKeyInterval)) ||          if (pEnc->flush_bframes)
713                  image_mad(&pEnc->reference->image, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.width, pEnc->mbParam.height) > 30)          {
714                    if (pEnc->bframenum_head < pEnc->bframenum_tail)
715          {          {
                 dprintf("--- IFRAME ---");  
716    
717                  FrameCodeI(pEnc, &bs, &bits);                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
718                                            pEnc->bframenum_head, pEnc->bframenum_tail,
719                                            pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
720    
721                  pFrame->intra = 1;                          FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs);
722                  pEnc->flush_bframes = 1;                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]);
723    
724                  /* note: sequences like "IIBB" decode fine with msfdam but,                          pEnc->bframenum_head++;
                    go screwy with divx5.00 */  
         }  
         else if (pEnc->bframenum_tail >= pEnc->max_bframes)  
         {  
                 dprintf("--- PFRAME ---");  
725    
                 FrameCodeP(pEnc, &bs, &bits, 1, 0);  
                 pFrame->intra = 0;  
                 pEnc->flush_bframes = 1;  
         }  
         else  
         {  
                 dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
726    
727                  if (pFrame->bquant < 1)                          goto done;
                 {  
                         pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;  
                 }  
                 else  
                 {  
                         pEnc->current->quant = pFrame->bquant;  
728                  }                  }
729    
730                  // store frame into bframe buffer & swap ref back to current                  /* flush complete: reset counters */
                 SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);  
                 SWAP(pEnc->current, pEnc->reference);  
731    
732                  pEnc->bframenum_tail++;                  pEnc->flush_bframes = 0;
733                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
734    
735                  pFrame->intra = 0;                  /* write an empty marker to the bitstream.
                 pFrame->length = 0;  
                 pFrame->input_consumed = 1;  
736    
737                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                     for divx5 decoder compatibility, this marker must consist
738                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                     of a not-coded p-vop, with a time_base of zero, and time_increment
739                  {                     indentical to the future-referece frame.
740                          pEnc->mbParam.m_seconds++;                  */
741                          pEnc->mbParam.m_ticks = 0;  
742                  }                  if ((pEnc->mbParam.global_flags & XVID_PACKED)) {
743                  return XVID_ERR_OK;                          int tmp;
744          }                          int bits;
745    
746                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
747                                    pEnc->bframenum_head, pEnc->bframenum_tail,
748                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
749    
750                            bits = BitstreamPos(&bs);
751    
752                            tmp = pEnc->current->seconds;
753                            pEnc->current->seconds = 0; /* force time_base = 0 */
754    
755                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
756          BitstreamPad(&bs);          BitstreamPad(&bs);
757          pFrame->length = BitstreamLength(&bs);                          pEnc->current->seconds = tmp;
758    
759          if (pResult)                          /* add the not-coded length to the reference frame size */
760          {                          pEnc->current->length += (BitstreamPos(&bs) - bits) / 8;
761                  pResult->quant = pEnc->current->quant;                          set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
                 pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->sStat.kblks;  
                 pResult->mblks = pEnc->sStat.mblks;  
                 pResult->ublks = pEnc->sStat.ublks;  
         }  
762    
763  #ifdef _DEBUG                          // xFrame->out_flags |= XVID_PACKED_FIXUP?;
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
764    
765          sprintf(temp, "PSNR: %f\n", psnr);                          goto done;
         DEBUG(temp);  
 #endif  
766    
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);  
767          }          }
   
         pEnc->iFrameNum++;  
         pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
         {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
768          }          }
         pFrame->input_consumed = 1;  
769    
         stop_global_timer();  
         write_timer();  
770    
771          return XVID_ERR_OK;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772  }           * dequeue frame from the encoding queue
773  // ==================================================================           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
774  #else  
775  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)          if (pEnc->queue_size == 0)              /* empty */
776  {  {
777          uint16_t x, y;                  if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */
778          Bitstream bs;                  {
779          uint32_t bits;                          if (pEnc->bframenum_tail > 0)   /* are bframes */
780          uint16_t write_vol_header = 0;                          {
781  #ifdef _DEBUG                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
782          float psnr;                                  pEnc->bframenum_tail--;
783          uint8_t temp[100];                                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
 #endif  
784    
785          start_global_timer();                                  /* XXX: convert B-VOP to P-VOP */
786                                    pEnc->current->quant *= 2;
787    
788          ENC_CHECK(pEnc);                                  FrameCodeP(pEnc, &bs, 1, 0);
789          ENC_CHECK(pFrame);                                  goto done_flush;
790          ENC_CHECK(pFrame->bitstream);                          }
         ENC_CHECK(pFrame->image);  
   
         SWAP(pEnc->current, pEnc->reference);  
   
         pEnc->current->global_flags = pFrame->general;  
         pEnc->current->motion_flags = pFrame->motion;  
         pEnc->mbParam.hint = &pFrame->hint;  
791    
792          start_timer();                          emms();
793          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,                          return XVID_ERR_END;    /* end of stream reached */
794                          pFrame->image, pFrame->colorspace))                  }
795          {                  goto done;      /* nothing to encode yet; encoder lag */
                 return XVID_ERR_FORMAT;  
796          }          }
         stop_conv_timer();  
797    
798  #ifdef _DEBUG          // the current FRAME becomes the reference
799          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);          SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
 #endif  
800    
801          EMMS();          // remove frame from encoding-queue (head), and move it into the current
802            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
803            frame = &pEnc->queue[pEnc->queue_head].frame;
804            pEnc->queue_head = (pEnc->queue_head+1) % (pEnc->mbParam.max_bframes+1);
805            pEnc->queue_size--;
806    
         BitstreamInit(&bs, pFrame->bitstream, 0);  
807    
808          if (pFrame->quant == 0)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
809          {           * init pEnc->current field
810                  pEnc->current->quant = RateControlGetQ(0);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
811    
812            emms();         /* float-point region */
813    
814        pEnc->current->vol_flags = pEnc->mbParam.vol_flags;
815        pEnc->current->vop_flags = frame->vop_flags;
816            pEnc->current->motion_flags = frame->motion;
817            pEnc->current->fcode = pEnc->mbParam.m_fcode;
818            pEnc->current->bcode = pEnc->mbParam.m_fcode;
819    
820        if ((xFrame->vop_flags & XVID_CHROMAOPT)) {
821                image_chroma_optimize(&pEnc->current->image,
822                        pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width);
823          }          }
824          else  
825          {      if (xFrame->vop_flags & XVID_EXTRASTATS)
826                  pEnc->current->quant = pFrame->quant;      {   image_copy(&pEnc->sOriginal, &pEnc->current->image,
827                           pEnc->mbParam.edged_width, pEnc->mbParam.height);
828          }          }
829    
830          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
831          {                  int *temp_dquants =     (int *) xvid_malloc(pEnc->mbParam.mb_width *
832                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                                          pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);
833    
834                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  pEnc->current->quant =
835                                                              pEnc->mbParam.edged_width,  // stride                          adaptive_quantization(pEnc->current->image.y,
836                                                              temp_dquants,                                                            pEnc->mbParam.edged_width, temp_dquants,
837                                                              pEnc->current->quant,                                                            pEnc->current->quant, pEnc->current->quant,
838                                                              pEnc->current->quant,       // min_quant                                                            2 * pEnc->current->quant,
                                                             2*pEnc->current->quant,     // max_quant  
839                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
840                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
841    
842                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
843                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
844                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
845                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
846                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
847                          }                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
                 xvid_free(temp_dquants);  
         }  
848    
849          if (pEnc->current->global_flags & XVID_H263QUANT) {                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                 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 ret1, ret2;  
   
                 ret1 = ret2 = 0;  
   
                 if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)  
                         write_vol_header = 1;  
   
                 pEnc->mbParam.m_quant_type = MPEG4_QUANT;  
   
                 if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {  
                         if(pFrame->quant_intra_matrix != NULL)  
                                 ret1 = set_intra_matrix(pFrame->quant_intra_matrix);  
                         if(pFrame->quant_inter_matrix != NULL)  
                                 ret2 = set_inter_matrix(pFrame->quant_inter_matrix);  
850                  }                  }
851                  else {  
852                          ret1 = set_intra_matrix(get_default_intra_matrix());  #undef OFFSET
                         ret2 = set_inter_matrix(get_default_inter_matrix());  
853                  }                  }
854                  if(write_vol_header == 0)  
855                          write_vol_header = ret1 | ret2;                  xvid_free(temp_dquants);
856          }          }
857    
858          if (pFrame->intra < 0)          if (frame->quant > 0)
859          {          {
860                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                  pEnc->current->quant = frame->quant;
861                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  type = frame->type;
   
                         pFrame->intra = FrameCodeI(pEnc, &bs, &bits);  
                 else  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);  
862          }          }
863          else          else
864          {          {
865                  if (pFrame->intra == 1)                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
866                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                  type = XVID_TYPE_AUTO;  //RateControlGetType(&pEnc->rate_control, ...);
                 else  
                         pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);  
867          }          }
868    
869          BitstreamPutBits(&bs, 0xFFFF, 16);          emms();         /* END floating-point region */
870          BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPad(&bs);  
         pFrame->length = BitstreamLength(&bs);  
871    
872          if (pResult)          if (type > 0)   /* XVID_TYPE_?VOP */
873          {          {
874                  pResult->quant = pEnc->current->quant;                  type = type2coding(type);       /* convert XVID_TYPE_?VOP to bitstream coding type */
                 pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->sStat.kblks;  
                 pResult->mblks = pEnc->sStat.mblks;  
                 pResult->ublks = pEnc->sStat.ublks;  
875          }          }
876            else                    /* XVID_TYPE_AUTO */
877          EMMS();          {
878                    if (pEnc->iFrameNum == 0 ||
879          if (pFrame->quant == 0)                          (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
880          {          {
881                  RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra);                          pEnc->iFrameNum = 0;
882                            type = I_VOP;
883                    }else{
884                            type = MEanalysis(&pEnc->reference->image, pEnc->current,
885                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
886                                            pEnc->iFrameNum, pEnc->bframenum_tail);
887          }          }
888            }
889  #ifdef _DEBUG          inc_frame_num(pEnc);
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
   
         sprintf(temp, "PSNR: %f\n", psnr);  
         DEBUG(temp);  
 #endif  
   
890          pEnc->iFrameNum++;          pEnc->iFrameNum++;
891    
         stop_global_timer();  
         write_timer();  
892    
893          return XVID_ERR_OK;          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
894                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
895                            "%i  st:%i  if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum);
896  }  }
 #endif  
897    
898            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899             * ivop/pvop/bvop selection
900             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
901    
902  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {          if (type == I_VOP) {
903    
904          pMB->mode = MODE_INTRA;                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
905                                    pEnc->bframenum_head, pEnc->bframenum_tail,
906                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
907    
908          /* zero mv statistics */                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
909          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
910          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;                  }
         pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;  
         pMB->sad16 = 0;  
911    
912          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                  // when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame
913                  if(pMB->dquant != NO_CHANGE)                  if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) {
                 {  
                         pMB->mode = MODE_INTRA_Q;  
                         pEnc->current->quant += DQtab[pMB->dquant];  
914    
915                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          // place this frame back on the encoding-queue (head)
916                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                          // we will deal with it next time
917                  }                          pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1);
918          }                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image);
919    
920          pMB->quant = pEnc->current->quant;                          // grab the last frame from the bframe-queue
 }  
921    
922                            pEnc->bframenum_tail--;
923                            SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
924    
925  #define FCODEBITS       3                          if ((pEnc->current->vop_flags & XVID_DEBUG)) {
926  #define MODEBITS        5                                  image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
927                            }
928    
929  void HintedMESet(Encoder * pEnc, int * intra)                          /* XXX: convert B-VOP to P-VOP */
930  {                          pEnc->current->quant *= 2;
         HINTINFO * hint;  
         Bitstream bs;  
         int length, high;  
         uint32_t x, y;  
931    
932          hint = pEnc->mbParam.hint;                          FrameCodeP(pEnc, &bs, 1, 0);
933    
934          if (hint->rawhints)                  } else {
         {  
                 *intra = hint->mvhint.intra;  
         }  
         else  
         {  
                 BitstreamInit(&bs, hint->hintstream, hint->hintlength);  
                 *intra = BitstreamGetBit(&bs);  
         }  
935    
936          if (*intra)                          /* ---- update vol flags at IVOP ----------- */
937          {                          pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags;
938                  return;  
939                if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) {
940                                    if (frame->quant_intra_matrix != NULL)
941                                            set_intra_matrix(frame->quant_intra_matrix);
942                                    if (frame->quant_inter_matrix != NULL)
943                                            set_inter_matrix(frame->quant_inter_matrix);
944          }          }
945    
946          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);              /* prevent vol/vop misuse */
947    
948          length  = pEnc->current->fcode + 5;              if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
949          high    = 1 << (length - 1);                  pEnc->current->vop_flags &= ~XVID_REDUCED;
950    
951          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)              if (!(pEnc->current->vol_flags & XVID_INTERLACING))
952          {                  pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN);
                 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[4];  
                         VECTOR tmp;  
                         int32_t dummy[4];  
                         int vec;  
953    
954                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          /* ^^^------------------------ */
955    
956                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          FrameCodeI(pEnc, &bs);
957                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          xFrame->out_flags |= XVID_KEYFRAME;
958                    }
959    
960                          if (pMB->mode == MODE_INTER)          } else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) ||
961                          {                                  pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
962                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                  /*
963                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                   * This will be coded as a Predicted Frame
964                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                   */
                                 tmp.y -= (tmp.y >= high) ? high*2 : 0;  
965    
966                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                  DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
967                                    pEnc->bframenum_head, pEnc->bframenum_tail,
968                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
969    
970                                  for (vec=0 ; vec<4 ; ++vec)                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
971                                  {                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
                                         pMB->mvs[vec].x  = tmp.x;  
                                         pMB->mvs[vec].y  = tmp.y;  
                                         pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;  
                                         pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;  
972                                  }                                  }
                         }  
                         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;  
973    
974                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                  FrameCodeP(pEnc, &bs, 1, 0);
975    
976                                          pMB->mvs[vec].x  = tmp.x;          } else {        /* type == B_VOP */
977                                          pMB->mvs[vec].y  = tmp.y;  
978                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                  if ((pEnc->current->vop_flags & XVID_DEBUG)) {
979                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                                 }  
                         }  
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
                                 {  
                                         pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;  
                                 }  
980                          }                          }
981    
982                          if (pMB->mode == MODE_INTER4V &&                  if (frame->bquant < 1) {
983                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                          pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
984                          {                                  pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
                                 pMB->mode = MODE_INTRA;  
985    
986                                  for (vec=0 ; vec<4 ; ++vec)                  } else {
987                                  {                          pEnc->current->quant = frame->bquant;
                                         pMB->mvs[vec].x = pMB->mvs[vec].y = 0;  
                                 }  
                         }  
                 }  
         }  
988  }  }
989    
990                    if (pEnc->current->quant < 1)
991                            pEnc->current->quant = 1;
992                    else if (pEnc->current->quant > 31)
993                pEnc->current->quant = 31;
994    
995  void HintedMEGet(Encoder * pEnc, int intra)                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
996  {                                  pEnc->bframenum_head, pEnc->bframenum_tail,
997          HINTINFO * hint;                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
998          Bitstream bs;  
999          uint32_t x, y;                  /* store frame into bframe buffer & swap ref back to current */
1000          int length, high;                  SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1001                    SWAP(FRAMEINFO*, pEnc->current, pEnc->reference);
1002    
1003          hint = pEnc->mbParam.hint;                  pEnc->bframenum_tail++;
1004    
1005          if (hint->rawhints)                  goto repeat;
         {  
                 hint->mvhint.intra = intra;  
         }  
         else  
         {  
                 BitstreamInit(&bs, hint->hintstream, 0);  
                 BitstreamPutBit(&bs, intra);  
         }  
1006    
         if (intra)  
         {  
                 if (!hint->rawhints)  
                 {  
                         BitstreamPad(&bs);  
                         hint->hintlength = BitstreamLength(&bs);  
                 }  
                 return;  
1007          }          }
1008    
1009          length  = pEnc->current->fcode + 5;          if (xFrame->vop_flags & XVID_EXTRASTATS)
1010          high    = 1 << (length - 1);          {       stats->sse_y =
1011                            plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1012                                               pEnc->mbParam.edged_width, pEnc->mbParam.width,
1013                                               pEnc->mbParam.height);
1014    
1015          if (hint->rawhints)                  stats->sse_u =
1016          {                          plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1017                  hint->mvhint.fcode = pEnc->current->fcode;                                             pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1018          }                                             pEnc->mbParam.height/2);
1019          else  
1020          {                  stats->sse_v =
1021                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                          plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1022                                               pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1023                                               pEnc->mbParam.height/2);
1024          }          }
1025    
1026          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1027          {           * okay, on next enc_encode call we must flush bframes
1028                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1029                  {  
1030                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  done_flush:
                         MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];  
                         VECTOR tmp;  
1031    
                         if (hint->rawhints)  
                         {  
                                 bhint->mode = pMB->mode;  
                         }  
                         else  
1032                          {                          {
1033                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                  char tmp[100];
1034                    wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum);
1035                    image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width,
1036                            pEnc->mbParam.width, pEnc->mbParam.height, tmp);
1037                          }                          }
1038    
1039                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          /* packed_mode: repeat */
1040                          {          pEnc->flush_bframes = 1;
1041                                  tmp.x  = pMB->mvs[0].x;          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) {
1042                                  tmp.y  = pMB->mvs[0].y;                  goto repeat;
1043                                  tmp.x += (tmp.x < 0) ? high*2 : 0;          }
                                 tmp.y += (tmp.y < 0) ? high*2 : 0;  
1044    
1045                                  if (hint->rawhints)          if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0)
1046                                  {                                  {
1047                                          bhint->mvs[0].x = tmp.x;                  /* packed(and low_delay) encoding gives us graceful reorded-stats output */
1048                                          bhint->mvs[0].y = tmp.y;                  set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current);
1049                                  }                                  }
1050                                  else                                  else
1051                                  {                                  {
1052                                          BitstreamPutBits(&bs, tmp.x, length);                  /* outherwise, output the previous frame */
1053                                          BitstreamPutBits(&bs, tmp.y, length);                  /* XXX: for this to work, refernce must be valid for IVOPs too */
1054                                  }                  if (pEnc->current->stamp > 0)
1055                            set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference);
1056                    else
1057                            stats->type = XVID_TYPE_NOTHING;
1058                          }                          }
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
                                 int vec;  
1059    
1060                                  for (vec=0 ; vec<4 ; ++vec)          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1061                                  {           * done; return number of bytes consumed
1062                                          tmp.x  = pMB->mvs[vec].x;           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
                                         tmp.y  = pMB->mvs[vec].y;  
                                         tmp.x += (tmp.x < 0) ? high*2 : 0;  
                                         tmp.y += (tmp.y < 0) ? high*2 : 0;  
1063    
1064                                          if (hint->rawhints)  done:
1065                                          {  
1066                                                  bhint->mvs[vec].x = tmp.x;          stop_global_timer();
1067                                                  bhint->mvs[vec].y = tmp.y;          write_timer();
1068    
1069            emms();
1070            return BitstreamLength(&bs);
1071                                          }                                          }
1072                                          else  
1073    
1074    static __inline void
1075    CodeIntraMB(Encoder * pEnc,
1076                            MACROBLOCK * pMB)
1077                                          {                                          {
1078                                                  BitstreamPutBits(&bs, tmp.x, length);  
1079                                                  BitstreamPutBits(&bs, tmp.y, length);          pMB->mode = MODE_INTRA;
1080                                          }  
1081                                  }          /* zero mv statistics */
1082                          }          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1083            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1084            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1085            pMB->sad16 = 0;
1086    
1087            if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1088                    if (pMB->dquant != NO_CHANGE) {
1089                            pMB->mode = MODE_INTRA_Q;
1090                            pEnc->current->quant += DQtab[pMB->dquant];
1091    
1092                            if (pEnc->current->quant > 31)
1093                                    pEnc->current->quant = 31;
1094                            if (pEnc->current->quant < 1)
1095                                    pEnc->current->quant = 1;
1096                  }                  }
1097          }          }
1098    
1099          if (!hint->rawhints)          pMB->quant = pEnc->current->quant;
         {  
                 BitstreamPad(&bs);  
                 hint->hintlength = BitstreamLength(&bs);  
         }  
1100  }  }
1101    
1102    
1103  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  
1104  {  static int
1105    FrameCodeI(Encoder * pEnc,
1106                       Bitstream * bs)
1107    {
1108        int bits = BitstreamPos(bs);
1109            int mb_width = pEnc->mbParam.mb_width;
1110            int mb_height = pEnc->mbParam.mb_height;
1111    
1112          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1113          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1114    
1115          uint16_t x, y;          uint16_t x, y;
1116    
1117          pEnc->iFrameNum = 0;          if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE))
1118            {
1119                    mb_width = (pEnc->mbParam.width + 31) / 32;
1120                    mb_height = (pEnc->mbParam.height + 31) / 32;
1121    
1122                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1123                    /* XXX: setedges is overkill */
1124                    start_timer();
1125                    image_setedges(&pEnc->current->image,
1126                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1127                            pEnc->mbParam.width, pEnc->mbParam.height);
1128                    stop_edges_timer();
1129            }
1130    
1131            //pEnc->iFrameNum = 0;
1132          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1133          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1134            //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1135          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1136    
1137          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1138    
1139          *pBits = BitstreamPos(bs);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1140    
1141          pEnc->sStat.iTextBits = 0;          BitstreamPadAlways(bs);
1142          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
         pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1143    
1144          for (y = 0; y < pEnc->mbParam.mb_height; y++)          pEnc->current->sStat.iTextBits = 0;
1145                  for (x = 0; x < pEnc->mbParam.mb_width; x++)          pEnc->current->sStat.kblks = mb_width * mb_height;
1146                  {          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1147                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1148            for (y = 0; y < mb_height; y++)
1149                    for (x = 0; x < mb_width; x++) {
1150                            MACROBLOCK *pMB =
1151                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1152    
1153                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1154    
1155                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1156                                                              dct_codes, qcoeff);
1157    
1158                          start_timer();                          start_timer();
1159                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1160                          stop_prediction_timer();                          stop_prediction_timer();
1161    
1162                          start_timer();                          start_timer();
1163                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->vop_flags & XVID_GREYSCALE)
1164                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1165                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1166                                    qcoeff[5*64+0]=0;
1167                            }
1168                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1169                          stop_coding_timer();                          stop_coding_timer();
1170                  }                  }
1171    
1172            if ((pEnc->current->vop_flags & XVID_REDUCED))
1173            {
1174                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1175                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1176                            16, 0);
1177            }
1178          emms();          emms();
1179    
1180          *pBits = BitstreamPos(bs) - *pBits;          /* for divx5 compatibility, we must always pad between the packed p and b frames */
1181          pEnc->sStat.fMvPrevSigma = -1;          if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1182          pEnc->sStat.iMvSum = 0;                  BitstreamPadAlways(bs);
1183          pEnc->sStat.iMvCount = 0;          else
1184                    BitstreamPad(bs);
1185        pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1186    
1187            pEnc->fMvPrevSigma = -1;
1188          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1189    
1190          if (pEnc->current->global_flags & XVID_HINTEDME_GET)      /* XXX: hinted me
1191          {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1192                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1193          }          }*/
1194    
1195          return 1;                                        // intra          return 1;                                       /* intra */
1196  }  }
1197    
1198    
1199  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1200    #define BFRAME_SKIP_THRESHHOLD 30
1201    
1202  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  
1203    /* FrameCodeP also handles S(GMC)-VOPs */
1204    static int
1205    FrameCodeP(Encoder * pEnc,
1206                       Bitstream * bs,
1207                       bool force_inter,
1208                       bool vol_header)
1209  {  {
1210          float fSigma;          float fSigma;
1211        int bits = BitstreamPos(bs);
1212    
1213          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1214          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1215    
1216            int mb_width = pEnc->mbParam.mb_width;
1217            int mb_height = pEnc->mbParam.mb_height;
1218    
1219          int iLimit;          int iLimit;
1220          uint32_t x, y;          int x, y, k;
1221          int iSearchRange;          int iSearchRange;
1222          int bIntra;          int bIntra, skip_possible;
1223    
1224          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1225          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1226    
1227            if ((pEnc->current->vop_flags & XVID_REDUCED))
1228            {
1229                    mb_width = (pEnc->mbParam.width + 31) / 32;
1230                    mb_height = (pEnc->mbParam.height + 31) / 32;
1231            }
1232    
1233    
1234          start_timer();          start_timer();
1235          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1236                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->current->global_flags & XVID_INTERLACING);  
1237          stop_edges_timer();          stop_edges_timer();
1238    
1239          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1240          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1241            //pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1242          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1243    
1244          if (!force_inter)          if (!force_inter)
1245                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1246          else          else
1247                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1248    
1249          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->vop_flags & XVID_HALFPEL)) {
1250                  start_timer();                  start_timer();
1251                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1252                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1253                                                      pEnc->mbParam.edged_height,
1254                                                      (pEnc->mbParam.vol_flags & XVID_QUARTERPEL),
1255                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1256                  stop_inter_timer();                  stop_inter_timer();
1257          }          }
1258    
1259            pEnc->current->coding_type = P_VOP;
1260    
1261          start_timer();          start_timer();
1262          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          /*if (pEnc->current->global_flags & XVID_HINTEDME_SET)
         {  
1263                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1264          }          else*/
1265          else                  bIntra =
1266          {                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1267                  bIntra = MotionEstimation(                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->mbParam,  
                         pEnc->current,  
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1268                          iLimit);                          iLimit);
1269          }  
1270          stop_motion_timer();          stop_motion_timer();
1271    
1272          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs);
1273    
1274            if ( ( pEnc->current->vol_flags & XVID_GMC )
1275                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1276          {          {
1277                  return FrameCodeI(pEnc, bs, pBits);                  pEnc->current->coding_type = S_VOP;
         }  
1278    
1279          pEnc->current->coding_type = P_VOP;                  generate_GMCparameters( 2, 16, &pEnc->current->warp,
1280                                            pEnc->mbParam.width, pEnc->mbParam.height,
1281                                            &pEnc->current->gmc_data);
1282    
1283                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1284                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1285                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1286                                    pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0,
1287                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1288    
1289            }
1290    
1291            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1292          if(vol_header)          if(vol_header)
1293                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam);
1294                    BitstreamPadAlways(bs);
1295            }
1296    
1297          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1298    
1299          *pBits = BitstreamPos(bs);          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1300                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1301    
         pEnc->sStat.iTextBits = 0;  
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1302    
1303          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < mb_height; y++) {
1304          {                  for (x = 0; x < mb_width; x++) {
1305                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1306                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1307                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1308    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1309    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1310    
1311                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1312    
1313                          if (!bIntra)                          if (bIntra) {
1314                          {                                  CodeIntraMB(pEnc, pMB);
1315                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1316                                                                      dct_codes, qcoeff);
1317    
1318                                    start_timer();
1319                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1320                                    stop_prediction_timer();
1321    
1322                                    pEnc->current->sStat.kblks++;
1323    
1324                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1325                                    stop_coding_timer();
1326                                    continue;
1327                            }
1328    
1329                            if (pEnc->current->coding_type == S_VOP) {
1330    
1331                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1332                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1333                                            pEnc->mbParam.edged_width, 65536);
1334    
1335                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1336                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1337                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1338    
1339                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1340                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1341                                    }
1342    
1343                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1344    
1345                                            if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1346                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1347                                            else
1348                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1349    
1350                                            pMB->mode = MODE_INTER;
1351                                            pMB->mcsel = 1;
1352                                            pMB->sad16 = iSAD;
1353                                    } else {
1354                                            pMB->mcsel = 0;
1355                                    }
1356                            } else {
1357                                    pMB->mcsel = 0; /* just a precaution */
1358                            }
1359    
1360                                  start_timer();                                  start_timer();
1361                                  MBMotionCompensation(pMB,                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1362                                                       x, y,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1363                                                       &pEnc->reference->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
                                                      &pEnc->vInterH,  
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
1364                                                       &pEnc->current->image,                                                       &pEnc->current->image,
1365                                                       dct_codes,                                                                   dct_codes, pEnc->mbParam.width,
                                                      pEnc->mbParam.width,  
1366                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1367                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1368                                                                     (pEnc->current->vol_flags & XVID_QUARTERPEL),
1369                                                                     (pEnc->current->vop_flags & XVID_REDUCED),
1370                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
1371    
1372                                  stop_comp_timer();                                  stop_comp_timer();
1373    
1374                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                          if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) {
1375                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1376                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1377                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1378                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                          if (pEnc->current->quant > 31)
1379                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                  pEnc->current->quant = 31;
1380                                            else if (pEnc->current->quant < 1)
1381                                                    pEnc->current->quant = 1;
1382                                          }                                          }
1383                                  }                                  }
1384                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1385    
1386                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1387    
1388                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          if (pMB->mode != MODE_NOT_CODED)
1389                            {       pMB->cbp =
1390                                            MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1391                                                                              dct_codes, qcoeff);
1392                          }                          }
1393                          else  
1394                          {                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1395                                  CodeIntraMB(pEnc, pMB);                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1396                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1397                                    pEnc->current->sStat.mblks++;
1398                            }  else {
1399                                    pEnc->current->sStat.ublks++;
1400                          }                          }
1401    
1402                          start_timer();                          start_timer();
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1403    
1404                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          /* Finished processing the MB, now check if to CODE or SKIP */
1405                          {  
1406                                  pEnc->sStat.kblks++;                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1407                                                            (pMB->dquant == NO_CHANGE);
1408    
1409                            if (pEnc->current->coding_type == S_VOP)
1410                                    skip_possible &= (pMB->mcsel == 1);
1411                            else if (pEnc->current->coding_type == P_VOP) {
1412                                    if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL))
1413                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1414                                    else
1415                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1416                          }                          }
1417                          else if (pMB->cbp ||  
1418                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1419                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1420                                   pMB->mvs[2].x || pMB->mvs[2].y ||  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1421                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1422                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1423                                    {
1424                                            int bSkip = 1;
1425    
1426                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1427                          {                          {
1428                                  pEnc->sStat.mblks++;                                                  int iSAD;
1429                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1430                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1431                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1432                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1433                                                    {       bSkip = 0;
1434                                                            break;
1435                          }                          }
1436                          else                                          }
1437    
1438                                            if (!bSkip) {   /* no SKIP, but trivial block */
1439                                                    if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1440                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1441                                                            pMB->pmvs[0].x = - predMV.x;
1442                                                            pMB->pmvs[0].y = - predMV.y;
1443                                                    }
1444                                                    else {
1445                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1446                                                            pMB->pmvs[0].x = - predMV.x;
1447                                                            pMB->pmvs[0].y = - predMV.y;
1448                                                    }
1449                                                    pMB->mode = MODE_INTER;
1450                                                    pMB->cbp = 0;
1451                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1452                                                    stop_coding_timer();
1453    
1454                                                    continue;       /* next MB */
1455                                            }
1456                                    }
1457                                    /* do SKIP */
1458    
1459                                    pMB->mode = MODE_NOT_CODED;
1460                                    MBSkip(bs);
1461                                    stop_coding_timer();
1462                                    continue;       /* next MB */
1463                            }
1464                            /* ordinary case: normal coded INTER/INTER4V block */
1465    
1466                            if ((pEnc->current->vop_flags & XVID_GREYSCALE))
1467                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1468                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1469                                    qcoeff[5*64+0]=0;
1470                            }
1471    
1472                            if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1473                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1474                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1475                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1476                                    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);
1477                            } else {
1478                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1479                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1480                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1481                                    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);
1482                            }
1483    
1484    
1485                            if (pMB->mode == MODE_INTER4V)
1486                            {       int k;
1487                                    for (k=1;k<4;k++)
1488                          {                          {
1489                                  pEnc->sStat.ublks++;                                          if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) {
1490                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1491                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1492                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1493                                    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);
1494                                            } else {
1495                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1496                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1497                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1498                                    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);
1499                          }                          }
1500    
1501                          start_timer();                                  }
1502                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          }
1503    
1504                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1505                          stop_coding_timer();                          stop_coding_timer();
1506    
1507                  }                  }
1508          }          }
1509    
1510            if ((pEnc->current->vop_flags & XVID_REDUCED))
1511            {
1512                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1513                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1514                            16, 0);
1515            }
1516    
1517          emms();          emms();
1518    
1519          if (pEnc->current->global_flags & XVID_HINTEDME_GET)      /* XXX: hinted me
1520          {          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1521                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1522          }          }*/
1523    
1524          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1525                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
1526    
1527          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
1528    
1529          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1530    
1531          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1532              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128          && (pEnc->mbParam.m_fcode <= (3 +  (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0)  ))) /* maximum search range 128 */
1533          {          {
1534                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1535                  iSearchRange *= 2;                  iSearchRange *= 2;
1536          }          } else if ((fSigma < iSearchRange / 6)
1537          else if ((fSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma >= 0)
1538                   && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1539                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                             && (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) )))        /* minimum search range 16 */
                  && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16  
1540          {          {
1541                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1542                  iSearchRange /= 2;                  iSearchRange /= 2;
1543          }          }
1544    
1545          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1546    
1547            /* frame drop code */
1548            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1549            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1550                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1551            {
1552                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
1553                    pEnc->current->sStat.ublks = mb_width * mb_height;
1554    
1555                    BitstreamReset(bs);
1556    
1557                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1558                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1559    
1560                    /* copy reference frame details into the current frame */
1561                    pEnc->current->quant = pEnc->reference->quant;
1562                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1563                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1564                    //pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1565                    pEnc->current->fcode = pEnc->reference->fcode;
1566                    pEnc->current->bcode = pEnc->reference->bcode;
1567                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1568                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1569            }
1570    
1571            /* XXX: debug
1572            {
1573                    char s[100];
1574                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1575                    image_dump_yuvpgm(&pEnc->current->image,
1576                            pEnc->mbParam.edged_width,
1577                            pEnc->mbParam.width, pEnc->mbParam.height, s);
1578    
1579                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1580                    image_dump_yuvpgm(&pEnc->reference->image,
1581                            pEnc->mbParam.edged_width,
1582                            pEnc->mbParam.width, pEnc->mbParam.height, s);
1583            }
1584            */
1585    
1586            /* for divx5 compatibility, we must always pad between the packed p and b frames */
1587            if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0)
1588                    BitstreamPadAlways(bs);
1589            else
1590                    BitstreamPad(bs);
1591    
1592          *pBits = BitstreamPos(bs) - *pBits;      pEnc->current->length = (BitstreamPos(bs) - bits) / 8;
1593    
1594          return 0;                                        // inter          return 0;                                       /* inter */
1595  }  }
1596    
1597    
1598  #ifdef BFRAMES  static void
1599  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  FrameCodeB(Encoder * pEnc,
1600                       FRAMEINFO * frame,
1601                       Bitstream * bs)
1602  {  {
1603      int16_t dct_codes[6*64];      int bits = BitstreamPos(bs);
1604      int16_t qcoeff[6*64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1605            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
1606      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1607    
1608      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1609          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1610    
1611          // forward      #ifdef BFRAMES_DEC_DEBUG
1612          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          FILE *fp;
1613            static char first=0;
1614    #define BFRAME_DEBUG    if (!first && fp){ \
1615                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1616            }
1617    
1618            /* XXX: pEnc->current->global_flags &= ~XVID_REDUCED;  reduced resoltion not yet supported */
1619    
1620            if (!first){
1621                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1622            }
1623    #endif
1624    
1625            //frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1626    
1627            /* forward  */
1628            image_setedges(f_ref, pEnc->mbParam.edged_width,
1629                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1630                                       pEnc->mbParam.height);
1631          start_timer();          start_timer();
1632          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1633                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1634                                              (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1635          stop_inter_timer();          stop_inter_timer();
1636    
1637          // backward          /* backward */
1638          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          image_setedges(b_ref, pEnc->mbParam.edged_width,
1639                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1640                                       pEnc->mbParam.height);
1641      start_timer();      start_timer();
1642          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1643                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1644                                              (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0);
1645          stop_inter_timer();          stop_inter_timer();
1646    
1647          start_timer();          start_timer();
1648    
1649          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1650                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   ((int32_t)(pEnc->current->stamp - frame->stamp)),                              /* time_bp */
1651                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                   ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),    /* time_pp */
1652                                                     pEnc->reference->mbs, f_ref,
1653                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1654                                                     pEnc->current, b_ref, &pEnc->vInterH,
1655                                                     &pEnc->vInterV, &pEnc->vInterHV);
1656    
1657    
1658          stop_motion_timer();          stop_motion_timer();
1659    
1660          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))          /*
1661          {          if (test_quant_type(&pEnc->mbParam, pEnc->current)) {
1662                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1663          }*/          }
1664            */
1665    
1666      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
     BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
   
     *pBits = BitstreamPos(bs);  
   
     pEnc->sStat.iTextBits = 0;  
     pEnc->sStat.iMvSum = 0;  
     pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1667    
1668            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1669            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1670    
1671      for (y = 0; y < pEnc->mbParam.mb_height; y++)          frame->sStat.iTextBits = 0;
1672          {          frame->sStat.iMvSum = 0;
1673                  // reset prediction          frame->sStat.iMvCount = 0;
1674            frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
1675                  forward.x = 0;          frame->sStat.mblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1676                  forward.y = 0;          frame->sStat.kblks = frame->sStat.ublks = 0;
1677                  backward.x = 0;  
1678                  backward.y = 0;          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1679                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1680                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1681                  {                          int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0;
1682                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
1683                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          /* decoder ignores mb when refence block is INTER(0,0), CBP=0 */
1684                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          if (mb->mode == MODE_NOT_CODED) {
1685                                    /* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */
                         // decoder ignores mb when refence block is INTER(0,0), CBP=0  
                         if (mb->mode == MODE_NOT_CODED)  
                         {  
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
1686                                  continue;                                  continue;
1687                          }                          }
1688    
1689                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1690                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1691                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1692                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1693                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1694                                          dct_codes);                                          dct_codes);
1695    
1696                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1697                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
1698    
1699                                    mb->cbp =
1700                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1701    
1702                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1703                                  mb->cbp == 0 &&                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1704                                  mb->mvs[0].x == 0 &&                                          mb->mode = MODE_DIRECT_NONE_MV; /* skipped */
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)  
                         {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1705                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)  
                         {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
1706                          }                          }
1707    
1708  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  #ifdef BFRAMES_DEC_DEBUG
1709            BFRAME_DEBUG
1710    #endif
1711                          start_timer();                          start_timer();
1712                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1713                                                     &frame->sStat, direction);
1714                          stop_coding_timer();                          stop_coding_timer();
1715                  }                  }
1716          }          }
1717    
1718          emms();          emms();
1719    
1720          // TODO: dynamic fcode/bcode ???          /* TODO: dynamic fcode/bcode ??? */
1721    
1722          *pBits = BitstreamPos(bs) - *pBits;      BitstreamPad(bs);
1723            frame->length = (BitstreamPos(bs) - bits) / 8;
1724    
1725    #ifdef BFRAMES_DEC_DEBUG
1726            if (!first){
1727                    first=1;
1728                    if (fp)
1729                            fclose(fp);
1730  }  }
1731  #endif  #endif
1732    }

Legend:
Removed from v.1.34  
changed lines
  Added in v.1.95.2.1

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