[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

trunk/xvidcore/src/encoder.c revision 194, Sun Jun 9 23:30:50 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 642, Mon Nov 11 20:53:34 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.40 2002-06-09 23:30:50 edgomez Exp $   *  $Id: encoder.c,v 1.76.2.17 2002-11-11 20:53:34 Isibaar Exp $
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46    
47  #include <stdlib.h>  #include <stdlib.h>
48  #include <stdio.h>  #include <stdio.h>
49  #include <math.h>  #include <math.h>
50    #include <string.h>
51    
52  #include "encoder.h"  #include "encoder.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "global.h"  #include "global.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "image/image.h"  #include "image/image.h"
57    #include "image/font.h"
58    #include "motion/sad.h"
59  #include "motion/motion.h"  #include "motion/motion.h"
60  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
61  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 82  Line 89 
89                        bool force_inter,                        bool force_inter,
90                        bool vol_header);                        bool vol_header);
91    
 #ifdef BFRAMES  
92  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
93                         FRAMEINFO * frame,                         FRAMEINFO * frame,
94                         Bitstream * bs,                         Bitstream * bs,
95                         uint32_t *pBits);                         uint32_t *pBits);
 #endif  
96    
97  /*****************************************************************************  /*****************************************************************************
98   * Local data   * Local data
99   ****************************************************************************/   ****************************************************************************/
100    
101  static int DQtab[4] =  static int DQtab[4] = {
 {  
102          -1, -2, 1, 2          -1, -2, 1, 2
103  };  };
104    
105  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
106          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
107  };  };
108    
109    
 static void __inline image_null(IMAGE * image)  
 {  
         image->y = image->u = image->v = NULL;  
 }  
   
   
110  /*****************************************************************************  /*****************************************************************************
111   * Encoder creation   * Encoder creation
112   *   *
# Line 132  Line 129 
129  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
130  {  {
131          Encoder *pEnc;          Encoder *pEnc;
132          uint32_t i;          int i;
133    
134          pParam->handle = NULL;          pParam->handle = NULL;
135    
# Line 145  Line 142 
142    
143          /* Fps */          /* Fps */
144    
145          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
146                  pParam->fincr = 1;                  pParam->fincr = 1;
147                  pParam->fbase = 25;                  pParam->fbase = 25;
148          }          }
# Line 157  Line 153 
153           */           */
154    
155          i = pParam->fincr;          i = pParam->fincr;
156          while (i > 1)          while (i > 1) {
157          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
158                          pParam->fincr /= i;                          pParam->fincr /= i;
159                          pParam->fbase /= i;                          pParam->fbase /= i;
160                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 163 
163                  i--;                  i--;
164          }          }
165    
166          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
167                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
168    
169                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
170                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
171          }          }
# Line 203  Line 197 
197    
198          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
199    
200          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
201                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
202    
   
203          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
204          if (pEnc == NULL)          if (pEnc == NULL)
205                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
206    
207            /* Zero the Encoder Structure */
208    
209            memset(pEnc, 0, sizeof(Encoder));
210    
211          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
212    
213          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 222 
222          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
223          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
224    
225            pEnc->mbParam.m_quant_type = H263_QUANT;
226    
227          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
228    
229          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 243 
243    
244          /* try to allocate mb memory */          /* try to allocate mb memory */
245    
246          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
247                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
248                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
249                                           CACHE_LINE);          pEnc->reference->mbs =
250          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
251                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
252    
253          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
254                  goto xvid_err_memory2;                  goto xvid_err_memory2;
255    
256          /* try to allocate image memory */          /* try to allocate image memory */
257    
258  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
259          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
260  #endif  #endif
261  #ifdef BFRAMES  
262          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
263          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
264          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
265  #endif  
266          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
267          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
268          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 274  Line 271 
271          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
272          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
273    
274  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
275          if (image_create(&pEnc->sOriginal,          if (image_create
276                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
277                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
278                  goto xvid_err_memory3;                  goto xvid_err_memory3;
279  #endif  #endif
280  #ifdef BFRAMES  
281          if (image_create(&pEnc->f_refh,          if (image_create
282                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
283                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
284                  goto xvid_err_memory3;                  goto xvid_err_memory3;
285          if (image_create(&pEnc->f_refv,          if (image_create
286                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
287                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
288                  goto xvid_err_memory3;                  goto xvid_err_memory3;
289          if (image_create(&pEnc->f_refhv,          if (image_create
290                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
291                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
292                  goto xvid_err_memory3;                  goto xvid_err_memory3;
293  #endif  
294          if (image_create(&pEnc->current->image,          if (image_create
295                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
296                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
297                  goto xvid_err_memory3;                  goto xvid_err_memory3;
298          if (image_create(&pEnc->reference->image,          if (image_create
299                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
300                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
301                  goto xvid_err_memory3;                  goto xvid_err_memory3;
302          if (image_create(&pEnc->vInterH,          if (image_create
303                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
304                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
305                  goto xvid_err_memory3;                  goto xvid_err_memory3;
306          if (image_create(&pEnc->vInterV,          if (image_create
307                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
308                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
309                  goto xvid_err_memory3;                  goto xvid_err_memory3;
310          if (image_create(&pEnc->vInterVf,          if (image_create
311                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
312                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
313                  goto xvid_err_memory3;                  goto xvid_err_memory3;
314          if (image_create(&pEnc->vInterHV,          if (image_create
315                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
316                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
317                  goto xvid_err_memory3;                  goto xvid_err_memory3;
318          if (image_create(&pEnc->vInterHVf,          if (image_create
319                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
320                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
321                  goto xvid_err_memory3;                  goto xvid_err_memory3;
322    
323    
324    
325          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
326    
327            pEnc->global = pParam->global;
328          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
329          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
330            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
331          pEnc->bframes = NULL;          pEnc->bframes = NULL;
332    
333          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
334                  int n;                  int n;
335    
336                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
337                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
338                                              CACHE_LINE);                                              CACHE_LINE);
339    
340                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 347  Line 344 
344                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
345    
346    
347                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
348                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
349    
350                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
351                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
352    
353                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
354                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
355                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
356    
357                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
358                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
359    
360                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
361    
362                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
363                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
364                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
365                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
366    
# Line 376  Line 370 
370          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
371          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
372          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
373            pEnc->bframenum_dx50bvop = -1;
374    
375          pEnc->mbParam.m_seconds = 0;          pEnc->queue = NULL;
376          pEnc->mbParam.m_ticks = 0;  
377  #endif  
378            if (pEnc->mbParam.max_bframes > 0) {
379                    int n;
380    
381                    pEnc->queue =
382                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
383                                                    CACHE_LINE);
384    
385                    if (pEnc->queue == NULL)
386                            goto xvid_err_memory4;
387    
388                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
389                            image_null(&pEnc->queue[n]);
390    
391                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
392                            if (image_create
393                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
394                                     pEnc->mbParam.edged_height) < 0)
395                                    goto xvid_err_memory5;
396    
397                    }
398            }
399    
400            pEnc->queue_head = 0;
401            pEnc->queue_tail = 0;
402            pEnc->queue_size = 0;
403    
404            pEnc->mbParam.m_stamp = 0;
405    
406            pEnc->m_framenum = 0;
407            pEnc->current->stamp = 0;
408            pEnc->reference->stamp = 0;
409    
410          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
411    
412          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
413          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
414                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
415                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
416                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
417                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
418          }          }
419    
420          init_timer();          init_timer();
# Line 402  Line 424 
424          /*          /*
425           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
426           */           */
427  #ifdef BFRAMES  
428      xvid_err_memory5:
429    
430    
431            if (pEnc->mbParam.max_bframes > 0) {
432    
433                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
434                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
435                                                      pEnc->mbParam.edged_height);
436                    }
437                    xvid_free(pEnc->queue);
438            }
439    
440   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
441    
442                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
443    
444                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
445                                pEnc->mbParam.edged_width,  
446                            if (pEnc->bframes[i] == NULL)
447                                    continue;
448    
449                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
450                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
451    
452                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 420  Line 456 
456          }          }
457    
458          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
459            }
 #endif  
460    
461   xvid_err_memory3:   xvid_err_memory3:
462  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
463          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
464                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
465  #endif  #endif
466    
467  #ifdef BFRAMES          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
468                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
469          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
470                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
471          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
472                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
 #endif  
473    
474          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
475                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
476          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
477                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
478          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
479                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
480          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
481                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
482          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
483                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
484          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
485                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
486          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
487                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
488    
489   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 514 
514  int  int
515  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
516  {  {
517            int i;
518    
519          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
520    
521          /* B Frames specific */          /* B Frames specific */
522  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
         int i;  
523    
524          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
525          {  
526                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
527                                              pEnc->mbParam.edged_height);
528                    }
529                    xvid_free(pEnc->queue);
530            }
531    
                 if (pEnc->bframes[i] == NULL) continue;  
532    
533                  image_destroy(&pEnc->bframes[i]->image,          if (pEnc->mbParam.max_bframes > 0) {
534                                pEnc->mbParam.edged_width,  
535                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
536    
537                            if (pEnc->bframes[i] == NULL)
538                                    continue;
539    
540                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
541                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
542    
543                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
544    
545                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
546          }          }
547    
548          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
549    
550  #endif          }
551    
552          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
553    
554          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
555                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
556          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
557                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
558          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
559                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
560          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
561                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
562          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
563                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
564          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
565                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
566          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
567                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
568  #ifdef BFRAMES  
569          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
570                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
571          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
572                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
573          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
574                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
575  #endif  
576  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
577          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
578                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
579  #endif  #endif
580    
# Line 570  Line 591 
591          return XVID_ERR_OK;          return XVID_ERR_OK;
592  }  }
593    
594    
595    static __inline void inc_frame_num(Encoder * pEnc)
596    {
597            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
598            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
599    }
600    
601    
602    static __inline void
603    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
604    {
605            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
606            {
607                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
608                    return;
609            }
610    
611            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
612                                    pEnc->bframenum_head, pEnc->bframenum_tail,
613                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
614    
615    
616            start_timer();
617            if (image_input
618                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
619                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
620                    return;
621            stop_conv_timer();
622    
623            pEnc->queue_size++;
624            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
625    }
626    
627    static __inline void
628    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
629    {
630    
631                    pCur->ticks = (int32_t)pCur->stamp % time_base;
632                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
633    
634                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
635    
636    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
637                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
638                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
639                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
640                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
641                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
642    
643    */
644    }
645    
646    
647    
648    
649    
650  /*****************************************************************************  /*****************************************************************************
651   * Frame encoder entry point   * IPB frame encoder entry point
  *  
  * At this moment 2 versions coexist : one for IPB compatible encoder,  
  *                                     another one for the old IP encoder.  
652   *   *
653   * Returned values :   * Returned values :
654   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 582  Line 656 
656   *                        format   *                        format
657   ****************************************************************************/   ****************************************************************************/
658    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
659  int  int
660  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
661                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
662                 XVID_ENC_STATS * pResult)                 XVID_ENC_STATS * pResult)
663  {  {
664          uint16_t x, y;          uint16_t x, y;
665          Bitstream bs;          Bitstream bs;
666          uint32_t bits;          uint32_t bits, mode;
667    
668  #ifdef _DEBUG          int input_valid = 1;
669    
670    #ifdef _DEBUG_PSNR
671          float psnr;          float psnr;
672          char temp[128];          char temp[128];
673  #endif  #endif
674    
675          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
676          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
677            ENC_CHECK(pFrame->image);
678    
679          start_global_timer();          start_global_timer();
680    
681          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
682    
683    ipvop_loop:
684    
685          /*          /*
686           * bframe "flush" code           * bframe "flush" code
687           */           */
688    
689          if ( (pFrame->image == NULL || pEnc->flush_bframes) &&          if ((pFrame->image == NULL || pEnc->flush_bframes)
690               (pEnc->bframenum_head < pEnc->bframenum_tail))                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
         {  
691    
692                  if (pEnc->flush_bframes == 0)                  if (pEnc->flush_bframes == 0) {
                 {  
693                          /*                          /*
694                           * we have reached the end of stream without getting                           * we have reached the end of stream without getting
695                           * a future reference frame... so encode last final                           * a future reference frame... so encode last final
696                           * frame as a pframe                           * frame as a pframe
697                           */                           */
698    
699                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
700                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
701                            dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
702                          */  
703                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
704                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
705    
706                          SWAP(pEnc->current,                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                              pEnc->bframes[pEnc->bframenum_tail]);  
707    
708                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
709    
710                          BitstreamPad(&bs);                          BitstreamPad(&bs);
711                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
712                          pFrame->intra = 0;                          pFrame->intra = 0;
713    
714                            emms();
715    
716                          return XVID_ERR_OK;                          return XVID_ERR_OK;
717                  }                  }
718    
                 /* ToDo : remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (flush) --- ");  
                 */  
                 FrameCodeB(pEnc,  
                            pEnc->bframes[pEnc->bframenum_head],  
                            &bs,  
                            &bits);  
                 pEnc->bframenum_head++;  
719    
720                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
721                                    pEnc->bframenum_head, pEnc->bframenum_tail,
722                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
723    
724                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
725                    pEnc->bframenum_head++;
726    
727                  BitstreamPad(&bs);                  BitstreamPad(&bs);
728                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
729                  pFrame->intra = 0;                  pFrame->intra = 0;
730    
731                    if (input_valid)
732                            queue_image(pEnc, pFrame);
733    
734                    emms();
735    
736                  return XVID_ERR_OK;                  return XVID_ERR_OK;
737          }          }
738    
739          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
740          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
741                  pFrame->length = 0;  
742                  pFrame->input_consumed = 1;                  /* write an empty marker to the bitstream.
743    
744                       for divx5 decoder compatibility, this marker must consist
745                       of a not-coded p-vop, with a time_base of zero, and time_increment
746                       indentical to the future-referece frame.
747                    */
748    
749                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
750                            int tmp;
751    
752                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
753                                    pEnc->bframenum_head, pEnc->bframenum_tail,
754                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
755    
756                            BitstreamPad(&bs);
757    
758                            tmp = pEnc->current->seconds;
759                            pEnc->current->seconds = 0; /* force time_base = 0 */
760                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
761                            pEnc->current->seconds = tmp;
762    
763                            pFrame->length = BitstreamLength(&bs);
764                  pFrame->intra = 0;                  pFrame->intra = 0;
765    
766                            if (input_valid)
767                                    queue_image(pEnc, pFrame);
768    
769                            emms();
770    
771                  return XVID_ERR_OK;                  return XVID_ERR_OK;
772          }          }
773            }
774    
775    
776    bvop_loop:
777    
778          if (pEnc->bframenum_head > 0)          if (pEnc->bframenum_dx50bvop != -1)
779          {          {
780                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
781                    SWAP(pEnc->current, pEnc->reference);
782                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
783    
784                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
785                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
786          }          }
787    
788          pEnc->flush_bframes = 0;                  if (input_valid)
789                    {
790                            queue_image(pEnc, pFrame);
791                            input_valid = 0;
792                    }
793    
794          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (input_valid) {
          * Well there was a separation here so i put it in ANSI C  
          * comment style :-)  
          * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */  
795    
796          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
797    
798          EMMS();                  start_timer();
799                    if (image_input
800                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
801                            pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
802                    {
803                            emms();
804                            return XVID_ERR_FORMAT;
805                    }
806                    stop_conv_timer();
807    
808                    // queue input frame, and dequue next image
809                    if (pEnc->queue_size > 0)
810                    {
811                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
812                            if (pEnc->queue_head != pEnc->queue_tail)
813                            {
814                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
815                            }
816                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
817                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
818                    }
819    
820            } else if (pEnc->queue_size > 0) {
821    
822                    SWAP(pEnc->current, pEnc->reference);
823    
824                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
825                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
826                    pEnc->queue_size--;
827    
828            } else {
829    
830                    /* if nothing was encoded, write an 'ignore this frame' flag
831                       to the bitstream */
832    
833                    if (BitstreamPos(&bs) == 0) {
834    
835                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
836                                    pEnc->bframenum_head, pEnc->bframenum_tail,
837                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
838    
839                            BitstreamPutBits(&bs, 0x7f, 8);
840                            pFrame->intra = 0;
841                    }
842    
843                    pFrame->length = BitstreamLength(&bs);
844                    emms();
845                    return XVID_ERR_OK;
846            }
847    
848            pEnc->flush_bframes = 0;
849    
850            emms();
851    
852            // only inc frame num, adapt quant, etc. if we havent seen it before
853            if (pEnc->bframenum_dx50bvop < 0 )
854            {
855          if (pFrame->quant == 0)          if (pFrame->quant == 0)
856                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
857          else          else
858                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
859    
860          if(pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
861                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
862    
863          if(pEnc->current->quant > 31)          if(pEnc->current->quant > 31)
864                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
865    */
866          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
867          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
868          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
869          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
870          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
871          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
872    
873          start_timer();                  inc_frame_num(pEnc);
         if (image_input(&pEnc->current->image,  
                         pEnc->mbParam.width,  
                         pEnc->mbParam.height,  
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace))  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
874    
875  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
876          image_copy(&pEnc->sOriginal,                  image_copy(&pEnc->sOriginal, &pEnc->current->image,
877                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
878  #endif  #endif
879    
880          EMMS();                  emms();
881    
882                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
883                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
884                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
885                    }
886    
887          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888           * Luminance masking           * Luminance masking
889           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
890    
891          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
         {  
892                  int *temp_dquants =                  int *temp_dquants =
893                          (int *) xvid_malloc(pEnc->mbParam.mb_width * \                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
894                                              pEnc->mbParam.mb_height * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                             sizeof(int),  
895                                              CACHE_LINE);                                              CACHE_LINE);
896    
897                  pEnc->current->quant =                  pEnc->current->quant =
898                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
899                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
900                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
901                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
902                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
903                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
904    
905                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
906    
907                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
908    
909                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
910                          {                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
911                                  MACROBLOCK *pMB =  
912                                          &pEnc->current->mbs[OFFSET(x,y)];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                 pMB->dquant =  
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
913                          }                          }
914    
915                          #undef OFFSET                          #undef OFFSET
   
916                  }                  }
917    
918                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
919          }          }
920    
921            }
922    
923          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
925           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
926    
927    
928          if (pEnc->iFrameNum == 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
929              pFrame->intra   == 1 ||                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
930                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
931                    || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
932                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
933                                             pEnc->mbParam.height) > 30) {*/
934                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
935                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
936    
             (pFrame->intra < 0  &&  
              pEnc->iMaxKeyInterval > 0 &&  
              pEnc->iFrameNum >= pEnc->iMaxKeyInterval) ||  
   
             image_mad(&pEnc->reference->image,  
                       &pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.width,  
                       pEnc->mbParam.height) > 30)  
         {  
937                  /*                  /*
938                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
939                   */                   */
940    
941                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
942                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
943                    dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                 */  
944    
945                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
946                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
947                    }
948    
949                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
950    
951                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
952    
953                            pEnc->bframenum_tail--;
954                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
955    
956                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
957                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
958                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
959                            }
960                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
961    
962                            pFrame->intra = 0;
963    
964                    } else {
965    
966                            FrameCodeI(pEnc, &bs, &bits);
967                  pFrame->intra = 1;                  pFrame->intra = 1;
968    
969                            pEnc->bframenum_dx50bvop = -1;
970                    }
971    
972                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
973    
974                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
975                            BitstreamPadAlways(&bs);
976                            input_valid = 0;
977                            goto ipvop_loop;
978                    }
979    
980                  /*                  /*
981                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
982                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
983                   */                   */
984          }          } else if ((pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) || (mode != 0)) {
985          else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
986                  /*                  /*
987                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
988                   */                   */
989    
990                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
991                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
992                    dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
993                  */  
994                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
995                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
996                    }
997    
998                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
999                  pFrame->intra = 0;                  pFrame->intra = 0;
1000                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1001    
1002                    if ((pEnc->global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1003                            BitstreamPadAlways(&bs);
1004                            input_valid = 0;
1005                            goto ipvop_loop;
1006          }          }
         else  
         {  
                 /*  
                  * This will be coded as a Bidirectional Frame  
                  */  
1007    
1008                  /* ToDo : Remove dprintf calls */          } else {
1009                  /*                  /*
1010                    dprintf("--- BFRAME (store) ---  head=%i tail=%i",                   * This will be coded as a Bidirectional Frame
                   pEnc->bframenum_head,  
                   pEnc->bframenum_tail);  
1011                  */                  */
1012    
1013                  if (pFrame->bquant < 1)                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1014                  {                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                         pEnc->current->quant =  
                                 ((pEnc->reference->quant+pEnc->current->quant)*\  
                                  pEnc->bquant_ratio) / 200;  
1015                  }                  }
1016                  else  
1017                  {                  if (pFrame->bquant < 1) {
1018                            pEnc->current->quant =
1019                                    ((pEnc->reference->quant +
1020                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1021                    } else {
1022                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1023                  }                  }
1024                    if (pEnc->current->quant < 1)
1025                            pEnc->current->quant = 1;
1026    
1027                    if (pEnc->current->quant > 31)
1028                            pEnc->current->quant = 31;
1029    
1030    
1031                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1032                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1033                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1034    
1035    
1036    
1037                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1038                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 852  Line 1042 
1042    
1043                  pFrame->intra = 0;                  pFrame->intra = 0;
1044                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1045    
1046                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1047                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1048                  }                  }
1049    
1050                  return XVID_ERR_OK;          pEnc->iFrameNum++;
         }  
1051    
1052          BitstreamPad(&bs);          BitstreamPad(&bs);
1053          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1054    
1055          if (pResult)          if (pResult) {
         {  
1056                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1057                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);
1058                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 876  Line 1060 
1060                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1061          }          }
1062    
1063          EMMS();          emms();
1064    
1065  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1066          psnr = image_psnr(&pEnc->sOriginal,          psnr =
1067                            &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1068                            pEnc->mbParam.edged_width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
                           pEnc->mbParam.width,  
1069                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1070    
1071          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1072          DEBUG(temp);          DEBUG(temp);
1073  #endif  #endif
1074    
1075          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1076          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1077                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
   
         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;  
1078          }          }
         pFrame->input_consumed = 1;  
1079    
1080          stop_global_timer();          stop_global_timer();
1081          write_timer();          write_timer();
1082    
1083            emms();
1084          return XVID_ERR_OK;          return XVID_ERR_OK;
1085  }  }
1086  #else  
1087    
1088    
1089  /*****************************************************************************  /*****************************************************************************
1090   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1091     *
1092     * Returned values :
1093     *    - XVID_ERR_OK     - no errors
1094     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1095     *                        format
1096   ****************************************************************************/   ****************************************************************************/
1097    
1098  int  int
1099  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1100                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 1104 
1104          Bitstream bs;          Bitstream bs;
1105          uint32_t bits;          uint32_t bits;
1106          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1107  #ifdef _DEBUG  
1108    #ifdef _DEBUG_PSNR
1109          float psnr;          float psnr;
1110          uint8_t temp[128];          uint8_t temp[128];
1111  #endif  #endif
# Line 942  Line 1123 
1123          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1124          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1125    
1126            inc_frame_num(pEnc);
1127    
1128            /* disable alternate scan flag if interlacing is not enabled */
1129            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1130                    !(pEnc->current->global_flags & XVID_INTERLACING))
1131            {
1132                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1133            }
1134    
1135          start_timer();          start_timer();
1136          if (image_input(&pEnc->current->image,          if (image_input
1137                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1138                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
1139                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1140          stop_conv_timer();          stop_conv_timer();
1141    
1142  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1143          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1144                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
1145  #endif  #endif
1146    
1147          EMMS();          emms();
1148    
1149          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1150    
1151          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1152                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1153          }          } else {
         else  
         {  
1154                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1155          }          }
1156    
1157          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1158          {                  pEnc->mbParam.m_quarterpel = 1;
1159                  int * temp_dquants = (int *)          else
1160                          xvid_malloc(pEnc->mbParam.mb_width * \                  pEnc->mbParam.m_quarterpel = 0;
1161                                      pEnc->mbParam.mb_height * \  
1162                                      sizeof(int),          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1163                    int *temp_dquants =
1164                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
1165                                                                    pEnc->mbParam.mb_height * sizeof(int),
1166                                      CACHE_LINE);                                      CACHE_LINE);
1167    
1168                  pEnc->current->quant =                  pEnc->current->quant =
1169                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
1170                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1171                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
1172                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
1173                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
1174                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
1175    
1176                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
1177    
1178                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1179    
1180                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
1181    
1182    
1183                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1184                                          &pEnc->current->mbs[OFFSET(x,y)];  
1185                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
1186                          }                          }
1187    
1188                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 1191 
1191                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1192          }          }
1193    
1194          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
1195                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1196                          write_vol_header = 1;                          write_vol_header = 1;
1197                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1198          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
1199                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
1200    
1201                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 1207 
1207    
1208                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1209                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1210                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
1211                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1212                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1213                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
1214                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1215                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1216                  }                  }
# Line 1044  Line 1218 
1218                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
1219          }          }
1220    
1221          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1222          {                  if ((pEnc->iFrameNum == 0)
1223                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
1224                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
1225                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1226                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
1227                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1228                          pFrame->intra = FrameCodeI(pEnc,                  }
1229                                                     &bs,          } else {
1230                                                     &bits);                  if (pFrame->intra == 1) {
1231                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1232                  else                  } else {
1233                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    0,  
                                                    write_vol_header);  
                 }  
         }  
         else  
         {  
                 if (pFrame->intra == 1)  
                 {  
                         pFrame->intra = FrameCodeI(pEnc,  
                                                    &bs,  
                                                    &bits);  
                 }  
                 else  
                 {  
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    1,  
                                                    write_vol_header);  
1234                  }                  }
1235    
1236          }          }
# Line 1088  Line 1240 
1240          BitstreamPad(&bs);          BitstreamPad(&bs);
1241          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1242    
1243          if (pResult)          if (pResult) {
         {  
1244                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1245                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1246                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 1248 
1248                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1249          }          }
1250    
1251          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1252    
1253  #ifdef _DEBUG          if (pFrame->quant == 0) {
1254          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1255                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
1256                            pEnc->mbParam.edged_width,          }
1257                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
1258            psnr =
1259                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1260                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1261                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1262    
1263          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
# Line 1125  Line 1271 
1271    
1272          return XVID_ERR_OK;          return XVID_ERR_OK;
1273  }  }
 #endif  
1274    
1275    
1276  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1277    CodeIntraMB(Encoder * pEnc,
1278                            MACROBLOCK * pMB)
1279    {
1280    
1281          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1282    
# Line 1139  Line 1287 
1287          pMB->sad16 = 0;          pMB->sad16 = 0;
1288    
1289          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1290                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1291                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1292                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1293    
1294                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1295                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1296                            if (pEnc->current->quant < 1)
1297                                    pEnc->current->quant = 1;
1298                  }                  }
1299          }          }
1300    
# Line 1156  Line 1305 
1305  #define FCODEBITS       3  #define FCODEBITS       3
1306  #define MODEBITS        5  #define MODEBITS        5
1307    
1308  void HintedMESet(Encoder * pEnc, int * intra)  void
1309    HintedMESet(Encoder * pEnc,
1310                            int *intra)
1311  {  {
1312          HINTINFO * hint;          HINTINFO * hint;
1313          Bitstream bs;          Bitstream bs;
# Line 1165  Line 1316 
1316    
1317          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1318    
1319          if (hint->rawhints)          if (hint->rawhints) {
         {  
1320                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1321          }          } else {
         else  
         {  
1322                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1323                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1324          }          }
1325    
1326          if (*intra)          if (*intra) {
         {  
1327                  return;                  return;
1328          }          }
1329    
1330          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1331                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1332                                                                                                                                     FCODEBITS);
1333    
1334          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1335          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1336    
1337          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1338          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1339                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1340                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1341                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1342                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1343                          VECTOR pred[4];                          VECTOR pred;
1344                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1345                          int vec;                          int vec;
1346    
1347                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1348                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1349                                                                                                                                      MODEBITS);
1350    
1351                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1352                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1353    
1354                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1355                          {                                  tmp.x =
1356                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1357                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1358                                    tmp.y =
1359                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1360                                                                                                                                                      length);
1361                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1362                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1363    
1364                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1365    
1366                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1367                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1368                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1369                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1370                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1371                                  }                                  }
1372                          }                          } else if (pMB->mode == MODE_INTER4V) {
1373                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1374                          {                                          tmp.x =
1375                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1376                                  {                                                  x : BitstreamGetBits(&bs, length);
1377                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1378                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1379                                                    y : BitstreamGetBits(&bs, length);
1380                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1381                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1382    
1383                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
1384    
1385                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1386                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1387                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1388                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
                                 }  
1389                          }                          }
1390                          else    // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1391                                  {                                  {
1392                                    for (vec = 0; vec < 4; ++vec) {
1393                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1394                                  }                                  }
1395                          }                          }
1396    
1397                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1398                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1399                          {                                  && pMB->dquant != NO_CHANGE) {
1400                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1401    
1402                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1403                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1404                                  }                                  }
1405                          }                          }
# Line 1258  Line 1408 
1408  }  }
1409    
1410    
1411  void HintedMEGet(Encoder * pEnc, int intra)  void
1412    HintedMEGet(Encoder * pEnc,
1413                            int intra)
1414  {  {
1415          HINTINFO * hint;          HINTINFO * hint;
1416          Bitstream bs;          Bitstream bs;
# Line 1267  Line 1419 
1419    
1420          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1421    
1422          if (hint->rawhints)          if (hint->rawhints) {
         {  
1423                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1424          }          } else {
         else  
         {  
1425                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1426                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1427          }          }
1428    
1429          if (intra)          if (intra) {
1430          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1431                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1432                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1433                  }                  }
# Line 1290  Line 1437 
1437          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1438          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1439    
1440          if (hint->rawhints)          if (hint->rawhints) {
         {  
1441                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1442          }          } else {
         else  
         {  
1443                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1444          }          }
1445    
1446          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1447          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1448                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1449                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1450                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1451                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1452                          VECTOR tmp;                          VECTOR tmp;
1453    
1454                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1455                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1456                          }                          } else {
                         else  
                         {  
1457                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1458                          }                          }
1459    
1460                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1461                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1462                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1463                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1464                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1465    
1466                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1467                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1468                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1469                                  }                                  } else {
                                 else  
                                 {  
1470                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1471                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1472                                  }                                  }
1473                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1474                                  int vec;                                  int vec;
1475    
1476                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1477                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1478                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1479                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1480                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1481    
1482                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1483                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1484                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1485                                          }                                          } else {
                                         else  
                                         {  
1486                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1487                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1488                                          }                                          }
# Line 1360  Line 1491 
1491                  }                  }
1492          }          }
1493    
1494          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1495                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1496                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1497          }          }
1498  }  }
1499    
1500    
1501  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1502    FrameCodeI(Encoder * pEnc,
1503                       Bitstream * bs,
1504                       uint32_t * pBits)
1505  {  {
1506    
1507          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 1379  Line 1512 
1512          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1513          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1514          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1515            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1516          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1517    
1518          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1519          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1520    #define DIVX501B481P "DivX501b481p"
1521            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1522                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1523            }
1524    
1525    #define XVID_ID "XviD" XVID_BS_VERSION
1526            BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID));
1527    
1528            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1529            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1530    
1531          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1532    
# Line 1391  Line 1535 
1535          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1536    
1537          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1538                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1539                  {                          MACROBLOCK *pMB =
1540                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1541    
1542                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1543    
1544                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1545                                                              dct_codes, qcoeff);
1546    
1547                          start_timer();                          start_timer();
1548                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1549                          stop_prediction_timer();                          stop_prediction_timer();
1550    
1551                          start_timer();                          start_timer();
1552                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1553                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1554                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1555                                    qcoeff[5*64+0]=0;
1556                            }
1557                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1558                          stop_coding_timer();                          stop_coding_timer();
1559                  }                  }
# Line 1416  Line 1566 
1566          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1567          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1568    
1569          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1570                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1571          }          }
1572    
# Line 1426  Line 1575 
1575    
1576    
1577  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1578    #define BFRAME_SKIP_THRESHHOLD 30
1579    
1580  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1581    FrameCodeP(Encoder * pEnc,
1582                       Bitstream * bs,
1583                       uint32_t * pBits,
1584                       bool force_inter,
1585                       bool vol_header)
1586  {  {
1587          float fSigma;          float fSigma;
1588    
# Line 1435  Line 1590 
1590          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1591    
1592          int iLimit;          int iLimit;
1593          uint32_t x, y;          int x, y, k;
1594          int iSearchRange;          int iSearchRange;
1595          int bIntra;          int bIntra, skip_possible;
1596    
1597          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1598          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1599    
1600          start_timer();          start_timer();
1601          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1602                         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);  
1603          stop_edges_timer();          stop_edges_timer();
1604    
1605          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1606          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1607            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1608          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1609    
1610          if (!force_inter)          if (!force_inter)
1611                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1612                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1613                                       INTRA_THRESHOLD);
1614          else          else
1615                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1616    
1617          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1618                  start_timer();                  start_timer();
1619                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1620                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1621                                                      pEnc->mbParam.edged_height,
1622                                                      pEnc->mbParam.m_quarterpel,
1623                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1624                  stop_inter_timer();                  stop_inter_timer();
1625          }          }
1626    
1627            if (pEnc->current->global_flags & XVID_GMC) {
1628    //              printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1629                    DPRINTF(DPRINTF_HEADER, "Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel);
1630                    pEnc->current->coding_type = S_VOP;
1631            } else
1632                    pEnc->current->coding_type = P_VOP;
1633    
1634          start_timer();          start_timer();
1635          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1636                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1637            if (bIntra == 0) {
1638                            pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current);
1639                            MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1640                                                                                            &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1641          }          }
1642          else  
1643          {          } else {
1644                  bIntra = MotionEstimation(  
1645                          &pEnc->mbParam,                  bIntra =
1646                          pEnc->current,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1647                          pEnc->reference,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1648                          iLimit);                          iLimit);
1649          }          }
1650          stop_motion_timer();          stop_motion_timer();
1651    
1652          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1653          {  
1654                  return FrameCodeI(pEnc, bs, pBits);          if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) )
1655          }                          pEnc->current->coding_type = P_VOP;             /* no global motion -> no GMC */
1656    
         pEnc->current->coding_type = P_VOP;  
1657    
1658          if(vol_header)          if(vol_header)
1659                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1660    
1661          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1662            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1663            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1664    
1665          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1666    
1667          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1668          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1669    
1670          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1671          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1672                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1673                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1674    
1675                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1676    
1677                          if (!bIntra)                          if (!bIntra) {
                         {  
1678                                  start_timer();                                  start_timer();
1679                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1680                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1681                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1682                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1683                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1684                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1685                                                                             pEnc->mbParam.m_quarterpel,
1686                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
1687                                  stop_comp_timer();                                  stop_comp_timer();
1688    
# Line 1534  Line 1690 
1690                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1691                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1692                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1693                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1694                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1695                                                    else if (pEnc->current->quant < 1)
1696                                                            pEnc->current->quant = 1;
1697                                          }                                          }
1698                                  }                                  }
1699                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1700    
1701                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1702    
1703                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  if (pMB->mode != MODE_NOT_CODED)
1704                          }                                          pMB->cbp =
1705                          else                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1706                          {                                                                                    dct_codes, qcoeff);
1707                            } else {
1708                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1709                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1710                                                                      dct_codes, qcoeff);
1711                          }                          }
1712    
1713                          start_timer();                          start_timer();
1714                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1715                          stop_prediction_timer();                          stop_prediction_timer();
1716    
1717                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1718                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1719                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1720                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1721                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1722                                    pEnc->sStat.mblks++;
1723                            }  else {
1724                                    pEnc->sStat.ublks++;
1725                          }                          }
1726                          else if (pMB->cbp ||  
1727                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1728                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1729                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1730                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1731                            skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &
1732                                                            (pMB->dquant == NO_CHANGE);
1733    
1734                            if(pEnc->mbParam.m_quarterpel)
1735                            {       skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y);
1736                            }
1737                            else
1738                            {       skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y);
1739                            }
1740    
1741                            if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1742    
1743    /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1744                                    int bSkip = 1;
1745    
1746                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1747                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1748                          {                          {
1749                                  pEnc->sStat.mblks++;                                                  int iSAD;
1750                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1751                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1752                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1753                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1754                                                    {       bSkip = 0;
1755                                                            break;
1756                                                    }
1757                                            }
1758    
1759                                    if (!bSkip)
1760                                    {
1761                                            VECTOR predMV;
1762                                            if(pEnc->mbParam.m_quarterpel) {
1763                                                    predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1764                                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;  /* with GMC, qmvs doesn't have to be (0,0)! */
1765                                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1766                                            }
1767                                            else {
1768                                                    predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1769                                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */
1770                                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1771                                            }
1772                                            pMB->mode = MODE_INTER;
1773                                            pMB->cbp = 0;
1774                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1775                          }                          }
1776                          else                          else
1777                          {                          {
1778                                  pEnc->sStat.ublks++;                                          pMB->mode = MODE_NOT_CODED;
1779                                            MBSkip(bs);
1780                          }                          }
1781    
1782                          start_timer();                          } else {
1783                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1784                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1785                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1786                                            qcoeff[5*64+0]=0;
1787                                    }
1788                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1789                            }
1790    
1791                          stop_coding_timer();                          stop_coding_timer();
1792                  }                  }
1793          }          }
1794    
1795          emms();          emms();
1796    
1797          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1798                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1799          }          }
1800    
# Line 1592  Line 1806 
1806          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1807    
1808          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1809              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1810          {          {
1811                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1812                  iSearchRange *= 2;                  iSearchRange *= 2;
1813          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1814                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1815                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1816                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
1817          {          {
1818                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1819                  iSearchRange /= 2;                  iSearchRange /= 2;
# Line 1608  Line 1821 
1821    
1822          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1823    
1824    #ifdef FRAMEDROP
1825            /* frame drop code */
1826            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1827            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1828                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1829            {
1830                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1831                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1832    
1833                    BitstreamReset(bs);
1834    
1835                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1836                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1837    
1838                    // copy reference frame details into the current frame
1839                    pEnc->current->quant = pEnc->reference->quant;
1840                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1841                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1842                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1843                    pEnc->current->fcode = pEnc->reference->fcode;
1844                    pEnc->current->bcode = pEnc->reference->bcode;
1845                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1846                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1847    
1848            }
1849    #endif
1850    
1851          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1852    
1853          return 0;                                        // inter          return 0;                                        // inter
1854  }  }
1855    
1856    
1857  #ifdef BFRAMES  static __inline void
1858  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  FrameCodeB(Encoder * pEnc,
1859                       FRAMEINFO * frame,
1860                       Bitstream * bs,
1861                       uint32_t * pBits)
1862  {  {
1863      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1864      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
1865      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1866    
1867      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1868          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1869    
1870    #ifdef BFRAMES_DEC_DEBUG
1871            FILE *fp;
1872            static char first=0;
1873    #define BFRAME_DEBUG    if (!first && fp){ \
1874                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1875            }
1876    
1877            if (!first){
1878                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1879            }
1880    #endif
1881    
1882            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
1883    
1884          // forward          // forward
1885          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          image_setedges(f_ref, pEnc->mbParam.edged_width,
1886                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1887                                       pEnc->mbParam.height);
1888          start_timer();          start_timer();
1889          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,
1890                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1891                                              pEnc->mbParam.m_quarterpel, 0);
1892          stop_inter_timer();          stop_inter_timer();
1893    
1894          // backward          // backward
1895          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,
1896                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1897                                       pEnc->mbParam.height);
1898      start_timer();      start_timer();
1899          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1900                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1901                                              pEnc->mbParam.m_quarterpel, 0);
1902          stop_inter_timer();          stop_inter_timer();
1903    
1904          start_timer();          start_timer();
1905    
1906          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1907                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1908                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1909                            pEnc->reference->mbs, f_ref,
1910                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1911                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1912                                                     &pEnc->vInterV, &pEnc->vInterHV);
1913    
1914    
1915          stop_motion_timer();          stop_motion_timer();
# Line 1654  Line 1920 
1920          }*/          }*/
1921    
1922      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1923      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
1924            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1925            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1926    
1927      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1928    
# Line 1664  Line 1932 
1932          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1933    
1934    
1935      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1936          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1937                  // reset prediction                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1938                            int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
                 for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
1939    
1940                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1941                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
1942                          {                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
1943                                  continue;                                  continue;
1944                          }                          }
1945    
1946                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1947                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1948                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1949                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1950                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1951                                          dct_codes);                                          dct_codes);
1952    
1953                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1954                          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);  
   
1955    
1956                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  mb->cbp =
1957                                  mb->cbp == 0 &&                                          MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
1958    
1959                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1960                          {                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1961                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
1962                          }                          }
   
                         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;  
1963                          }                          }
1964    
1965  //                      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
1966            BFRAME_DEBUG
1967    #endif
1968                          start_timer();                          start_timer();
1969                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1970                                                     &pEnc->sStat, direction);
1971                          stop_coding_timer();                          stop_coding_timer();
1972                  }                  }
1973          }          }
# Line 1734  Line 1977 
1977          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1978    
1979          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1980    
1981    #ifdef BFRAMES_DEC_DEBUG
1982            if (!first){
1983                    first=1;
1984                    if (fp)
1985                            fclose(fp);
1986  }  }
1987  #endif  #endif
1988    }
1989    
1990    
1991    /*      in case internal output is needed somewhere... */
1992    /*      {
1993            FILE *filehandle;
1994            filehandle=fopen("last-b.pgm","wb");
1995            if (filehandle)
1996            {
1997                    fprintf(filehandle,"P5\n\n");           //
1998                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1999                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
2000                    fclose(filehandle);
2001                    }
2002            }
2003    */

Legend:
Removed from v.194  
changed lines
  Added in v.642

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