[cvs] / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/encoder.c

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

revision 1.40, Sun Jun 9 23:30:50 2002 UTC revision 1.73, Wed Aug 7 10:09:00 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()
# Line 40  Line 43 
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    #ifdef BFRAMES
58    #include "image/font.h"
59    #include "motion/sad.h"
60    #endif
61  #include "motion/motion.h"  #include "motion/motion.h"
62  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
63  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 61  Line 70 
70  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
71  #include "utils/mem_align.h"  #include "utils/mem_align.h"
72    
73    #ifdef _SMP
74    #include "motion/smp_motion_est.h"
75    #endif
76  /*****************************************************************************  /*****************************************************************************
77   * Local macros   * Local macros
78   ****************************************************************************/   ****************************************************************************/
# Line 93  Line 105 
105   * Local data   * Local data
106   ****************************************************************************/   ****************************************************************************/
107    
108  static int DQtab[4] =  static int DQtab[4] = {
 {  
109          -1, -2, 1, 2          -1, -2, 1, 2
110  };  };
111    
112  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
113          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
114  };  };
115    
116    
117  static void __inline image_null(IMAGE * image)  static void __inline
118    image_null(IMAGE * image)
119  {  {
120          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
121  }  }
# Line 132  Line 143 
143  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
144  {  {
145          Encoder *pEnc;          Encoder *pEnc;
146          uint32_t i;          int i;
147    
148          pParam->handle = NULL;          pParam->handle = NULL;
149    
# Line 145  Line 156 
156    
157          /* Fps */          /* Fps */
158    
159          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
160                  pParam->fincr = 1;                  pParam->fincr = 1;
161                  pParam->fbase = 25;                  pParam->fbase = 25;
162          }          }
# Line 157  Line 167 
167           */           */
168    
169          i = pParam->fincr;          i = pParam->fincr;
170          while (i > 1)          while (i > 1) {
171          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
172                          pParam->fincr /= i;                          pParam->fincr /= i;
173                          pParam->fbase /= i;                          pParam->fbase /= i;
174                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 177 
177                  i--;                  i--;
178          }          }
179    
180          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
181                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
182    
183                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
184                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
185          }          }
# Line 203  Line 211 
211    
212          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
213    
214          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
215                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
216    
   
217          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
218          if (pEnc == NULL)          if (pEnc == NULL)
219                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
220    
221            /* Zero the Encoder Structure */
222    
223            memset(pEnc, 0, sizeof(Encoder));
224    
225          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
226    
227          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 236 
236          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
237          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
238    
239            pEnc->mbParam.m_quant_type = H263_QUANT;
240    
241    #ifdef _SMP
242            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
243    #endif
244    
245          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
246    
247          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 261 
261    
262          /* try to allocate mb memory */          /* try to allocate mb memory */
263    
264          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
265                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
266                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
267                                           CACHE_LINE);          pEnc->reference->mbs =
268          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
269                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
270    
271          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
272                  goto xvid_err_memory2;                  goto xvid_err_memory2;
273    
274          /* try to allocate image memory */          /* try to allocate image memory */
275    
276  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
277          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
278  #endif  #endif
279  #ifdef BFRAMES  #ifdef BFRAMES
# Line 274  Line 289 
289          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
290          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
291    
292  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
293          if (image_create(&pEnc->sOriginal,          if (image_create
294                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
295                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
296                  goto xvid_err_memory3;                  goto xvid_err_memory3;
297  #endif  #endif
298  #ifdef BFRAMES  #ifdef BFRAMES
299          if (image_create(&pEnc->f_refh,          if (image_create
300                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
301                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
302                  goto xvid_err_memory3;                  goto xvid_err_memory3;
303          if (image_create(&pEnc->f_refv,          if (image_create
304                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
305                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
306                  goto xvid_err_memory3;                  goto xvid_err_memory3;
307          if (image_create(&pEnc->f_refhv,          if (image_create
308                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
309                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
310                  goto xvid_err_memory3;                  goto xvid_err_memory3;
311  #endif  #endif
312          if (image_create(&pEnc->current->image,          if (image_create
313                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
314                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
315                  goto xvid_err_memory3;                  goto xvid_err_memory3;
316          if (image_create(&pEnc->reference->image,          if (image_create
317                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
318                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
319                  goto xvid_err_memory3;                  goto xvid_err_memory3;
320          if (image_create(&pEnc->vInterH,          if (image_create
321                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
322                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
323                  goto xvid_err_memory3;                  goto xvid_err_memory3;
324          if (image_create(&pEnc->vInterV,          if (image_create
325                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
326                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
327                  goto xvid_err_memory3;                  goto xvid_err_memory3;
328          if (image_create(&pEnc->vInterVf,          if (image_create
329                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
330                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
331                  goto xvid_err_memory3;                  goto xvid_err_memory3;
332          if (image_create(&pEnc->vInterHV,          if (image_create
333                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
334                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
335                  goto xvid_err_memory3;                  goto xvid_err_memory3;
336          if (image_create(&pEnc->vInterHVf,          if (image_create
337                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
338                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
339                  goto xvid_err_memory3;                  goto xvid_err_memory3;
340    
# Line 328  Line 343 
343          /* B Frames specific init */          /* B Frames specific init */
344  #ifdef BFRAMES  #ifdef BFRAMES
345    
346            pEnc->global = pParam->global;
347          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
348          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
349            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
350          pEnc->bframes = NULL;          pEnc->bframes = NULL;
351    
352          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
353                  int n;                  int n;
354    
355                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
356                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
357                                              CACHE_LINE);                                              CACHE_LINE);
358    
359                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 347  Line 363 
363                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
364    
365    
366                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
367                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
368    
369                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
370                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
371    
372                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
373                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
374                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
375    
376                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
377                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
378    
379                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
380    
381                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
382                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
383                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
384                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
385    
# Line 376  Line 389 
389          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
390          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
391          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
392            pEnc->bframenum_dx50bvop = -1;
393    
394            pEnc->queue = NULL;
395    
396    
397            if (pEnc->mbParam.max_bframes > 0) {
398                    int n;
399    
400                    pEnc->queue =
401                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
402                                                    CACHE_LINE);
403    
404                    if (pEnc->queue == NULL)
405                            goto xvid_err_memory4;
406    
407                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
408                            image_null(&pEnc->queue[n]);
409    
410                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
411                            if (image_create
412                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
413                                     pEnc->mbParam.edged_height) < 0)
414                                    goto xvid_err_memory5;
415    
416                    }
417            }
418    
419            pEnc->queue_head = 0;
420            pEnc->queue_tail = 0;
421            pEnc->queue_size = 0;
422    
423    
424          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
425          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
426            pEnc->m_framenum = 0;
427            pEnc->last_pframe = 0;
428            pEnc->last_sync = 0;
429  #endif  #endif
430    
431          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
432    
433          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
434          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
435                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
436                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
437                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
438                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
439          }          }
440    
441          init_timer();          init_timer();
# Line 403  Line 446 
446           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
447           */           */
448  #ifdef BFRAMES  #ifdef BFRAMES
449      xvid_err_memory5:
450    
451    
452            if (pEnc->mbParam.max_bframes > 0) {
453    
454                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
455                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
456                                                      pEnc->mbParam.edged_height);
457                    }
458                    xvid_free(pEnc->queue);
459            }
460    
461   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
462    
463                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
464    
465                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
466                                pEnc->mbParam.edged_width,  
467                            if (pEnc->bframes[i] == NULL)
468                                    continue;
469    
470                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
471                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
472    
473                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 420  Line 477 
477          }          }
478    
479          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
480            }
481    
482  #endif  #endif
483    
484   xvid_err_memory3:   xvid_err_memory3:
485  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
486          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
487                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
488  #endif  #endif
489    
490  #ifdef BFRAMES  #ifdef BFRAMES
491          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
492                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
493          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
494                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
495          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
496                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
497  #endif  #endif
498    
499          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
500                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
501          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
502                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
503          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
504                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
505          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
506                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
507          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
508                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
509          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
510                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
511          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
512                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
513    
514   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 539 
539  int  int
540  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
541  {  {
542    #ifdef BFRAMES
543            int i;
544    #endif
545    
546          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
547    
548          /* B Frames specific */          /* B Frames specific */
549  #ifdef BFRAMES  #ifdef BFRAMES
550          int i;          if (pEnc->mbParam.max_bframes > 0) {
551    
552          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
         {  
553    
554                  if (pEnc->bframes[i] == NULL) continue;                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
555                                              pEnc->mbParam.edged_height);
556                    }
557                    xvid_free(pEnc->queue);
558            }
559    
560                  image_destroy(&pEnc->bframes[i]->image,  
561                                pEnc->mbParam.edged_width,          if (pEnc->mbParam.max_bframes > 0) {
562    
563                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
564    
565                            if (pEnc->bframes[i] == NULL)
566                                    continue;
567    
568                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
569                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
570    
571                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
572    
573                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
574          }          }
575    
576          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
577    
578            }
579  #endif  #endif
580    
581          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
582    
583          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
584                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
585          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
586                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
587          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
588                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
589          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
590                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
591          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
592                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
593          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
594                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
595          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
596                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
597  #ifdef BFRAMES  #ifdef BFRAMES
598          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
599                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
600          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
601                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
602          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
603                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
604  #endif  #endif
605  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
606          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
607                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
608  #endif  #endif
609    
# Line 570  Line 620 
620          return XVID_ERR_OK;          return XVID_ERR_OK;
621  }  }
622    
623    
624    #ifdef BFRAMES
625    void inc_frame_num(Encoder * pEnc)
626    {
627            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
628    
629            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
630    
631    /*      fprintf(stderr, "ENC %c %i:%i %i\n",
632                    pEnc->current->coding_type == I_VOP ? 'I' : pEnc->current->coding_type == P_VOP ? 'P' : 'B',
633                    pEnc->mbParam.m_seconds, pEnc->mbParam.m_ticks,pEnc->last_sync);
634    */
635    
636            if (pEnc->mbParam.m_ticks < pEnc->last_sync)
637                    pEnc->mbParam.m_seconds = 1;            // more than 1 second since last I or P is not supported.
638            else
639                    pEnc->mbParam.m_seconds = 0;
640    
641            if (pEnc->current->coding_type != B_VOP)
642                    pEnc->last_sync = pEnc->mbParam.m_ticks;
643    
644    }
645    #endif
646    
647    
648    #ifdef BFRAMES
649    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
650    {
651            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
652            {
653                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
654                    return;
655            }
656    
657            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
658                                    pEnc->bframenum_head, pEnc->bframenum_tail,
659                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
660    
661    
662            start_timer();
663            if (image_input
664                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
665                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
666                    return;
667            stop_conv_timer();
668    
669            pEnc->queue_size++;
670            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
671    }
672    #endif
673    
674    
675    #ifdef BFRAMES
676  /*****************************************************************************  /*****************************************************************************
677   * 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.  
678   *   *
679   * Returned values :   * Returned values :
680   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 582  Line 682 
682   *                        format   *                        format
683   ****************************************************************************/   ****************************************************************************/
684    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
685  int  int
686  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
687                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
688                 XVID_ENC_STATS * pResult)                 XVID_ENC_STATS * pResult)
689  {  {
# Line 596  Line 691 
691          Bitstream bs;          Bitstream bs;
692          uint32_t bits;          uint32_t bits;
693    
694  #ifdef _DEBUG          int input_valid = 1;
695    
696    #ifdef _DEBUG_PSNR
697          float psnr;          float psnr;
698          char temp[128];          char temp[128];
699  #endif  #endif
700    
701          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
702          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
703            ENC_CHECK(pFrame->image);
704    
705          start_global_timer();          start_global_timer();
706    
707          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
708    
709    ipvop_loop:
710    
711          /*          /*
712           * bframe "flush" code           * bframe "flush" code
713           */           */
714    
715          if ( (pFrame->image == NULL || pEnc->flush_bframes) &&          if ((pFrame->image == NULL || pEnc->flush_bframes)
716               (pEnc->bframenum_head < pEnc->bframenum_tail))                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
         {  
717    
718                  if (pEnc->flush_bframes == 0)                  if (pEnc->flush_bframes == 0) {
                 {  
719                          /*                          /*
720                           * we have reached the end of stream without getting                           * we have reached the end of stream without getting
721                           * a future reference frame... so encode last final                           * a future reference frame... so encode last final
722                           * frame as a pframe                           * frame as a pframe
723                           */                           */
724    
725                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
726                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
727                            dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
728                          */  
729                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
730                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
731    
732                          SWAP(pEnc->current,                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                              pEnc->bframes[pEnc->bframenum_tail]);  
733    
734                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
735    
736                          BitstreamPad(&bs);                          BitstreamPad(&bs);
737                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
738                          pFrame->intra = 0;                          pFrame->intra = 0;
739    
740                          return XVID_ERR_OK;                          return XVID_ERR_OK;
741                  }                  }
742    
                 /* ToDo : remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (flush) --- ");  
                 */  
                 FrameCodeB(pEnc,  
                            pEnc->bframes[pEnc->bframenum_head],  
                            &bs,  
                            &bits);  
                 pEnc->bframenum_head++;  
743    
744                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
745                                    pEnc->bframenum_head, pEnc->bframenum_tail,
746                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
747    
748                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
749                    pEnc->bframenum_head++;
750    
751                  BitstreamPad(&bs);                  BitstreamPad(&bs);
752                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
753                  pFrame->intra = 0;                  pFrame->intra = 0;
754    
755                    if (input_valid)
756                            queue_image(pEnc, pFrame);
757    
758                  return XVID_ERR_OK;                  return XVID_ERR_OK;
759          }          }
760    
761          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
762          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
763                  pFrame->length = 0;  
764                  pFrame->input_consumed = 1;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
765    
766                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
767                                    pEnc->bframenum_head, pEnc->bframenum_tail,
768                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
769    
770                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
771                            BitstreamPad(&bs);
772                            BitstreamPutBits(&bs, 0x7f, 8);
773    
774                            pFrame->length = BitstreamLength(&bs);
775                  pFrame->intra = 0;                  pFrame->intra = 0;
776    
777                            if (input_valid)
778                                    queue_image(pEnc, pFrame);
779    
780                  return XVID_ERR_OK;                  return XVID_ERR_OK;
781          }          }
782            }
783    
784          if (pEnc->bframenum_head > 0)  
785    bvop_loop:
786    
787            if (pEnc->bframenum_dx50bvop != -1)
788          {          {
789                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
790                    SWAP(pEnc->current, pEnc->reference);
791                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
792    
793                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
794                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
795                    }
796    
797                    if (input_valid)
798                    {
799                            queue_image(pEnc, pFrame);
800                            input_valid = 0;
801                    }
802    
803            } else if (input_valid) {
804    
805                    SWAP(pEnc->current, pEnc->reference);
806    
807                    start_timer();
808                    if (image_input
809                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
810                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
811                            return XVID_ERR_FORMAT;
812                    stop_conv_timer();
813    
814                    // queue input frame, and dequue next image
815                    if (pEnc->queue_size > 0)
816                    {
817                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
818                            if (pEnc->queue_head != pEnc->queue_tail)
819                            {
820                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
821                            }
822                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
823                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
824                    }
825    
826            } else if (pEnc->queue_size > 0) {
827    
828                    SWAP(pEnc->current, pEnc->reference);
829    
830                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
831                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
832                    pEnc->queue_size--;
833    
834            } else if (BitstreamPos(&bs) == 0) {
835    
836                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
837                                    pEnc->bframenum_head, pEnc->bframenum_tail,
838                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
839    
840                    pFrame->intra = 0;
841    
842                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
843                    BitstreamPad(&bs);
844                    pFrame->length = BitstreamLength(&bs);
845    
846                    return XVID_ERR_OK;
847    
848            } else {
849    
850                    pFrame->length = BitstreamLength(&bs);
851                    return XVID_ERR_OK;
852          }          }
853    
854          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 684  Line 858 
858           * comment style :-)           * comment style :-)
859           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
860    
861          SWAP(pEnc->current, pEnc->reference);          emms();
   
         EMMS();  
862    
863            // only inc frame num, adapt quant, etc. if we havent seen it before
864            if (pEnc->bframenum_dx50bvop < 0 )
865            {
866          if (pFrame->quant == 0)          if (pFrame->quant == 0)
867                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
868          else          else
869                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
870    
871          if(pEnc->current->quant < 1)  /*              if (pEnc->current->quant < 1)
872                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
873    
874          if(pEnc->current->quant > 31)          if(pEnc->current->quant > 31)
875                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
876    */
877          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
878          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
879          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
880          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
881          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
882          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
883    
884          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
885          if (image_input(&pEnc->current->image,                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
                         pEnc->mbParam.width,  
                         pEnc->mbParam.height,  
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace))  
                 return XVID_ERR_FORMAT;  
         stop_conv_timer();  
886    
887  #ifdef _DEBUG                  inc_frame_num(pEnc);
888          image_copy(&pEnc->sOriginal,  
889                     &pEnc->current->image,  #ifdef _DEBUG_PSNR
890                     pEnc->mbParam.edged_width,                  image_copy(&pEnc->sOriginal, &pEnc->current->image,
891                     pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
892  #endif  #endif
893    
894          EMMS();                  emms();
895    
896                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
897                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
898                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
899                    }
900    
901          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
902           * Luminance masking           * Luminance masking
903           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
904    
905          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
         {  
906                  int *temp_dquants =                  int *temp_dquants =
907                          (int *) xvid_malloc(pEnc->mbParam.mb_width * \                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
908                                              pEnc->mbParam.mb_height * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                             sizeof(int),  
909                                              CACHE_LINE);                                              CACHE_LINE);
910    
911                  pEnc->current->quant =                  pEnc->current->quant =
912                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
913                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
914                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
915                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
916                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
917                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
918    
919                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
920    
921                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
922    
923                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
924                          {                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
925                                  MACROBLOCK *pMB =  
926                                          &pEnc->current->mbs[OFFSET(x,y)];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                 pMB->dquant =  
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
927                          }                          }
928    
929                          #undef OFFSET                          #undef OFFSET
   
930                  }                  }
931    
932                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
933          }          }
934    
935            }
936    
937          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
938           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
939           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
940    
941    
942          if (pEnc->iFrameNum == 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
943              pFrame->intra   == 1 ||                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
944                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
945              (pFrame->intra < 0  &&                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
946               pEnc->iMaxKeyInterval > 0 &&                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
947               pEnc->iFrameNum >= pEnc->iMaxKeyInterval) ||                                           pEnc->mbParam.height) > 30) {
   
             image_mad(&pEnc->reference->image,  
                       &pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.width,  
                       pEnc->mbParam.height) > 30)  
         {  
948                  /*                  /*
949                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
950                   */                   */
951    
952                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
953                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
954                    dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                 */  
955    
956                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
957                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
958                    }
959    
960                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
961    
962                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
963    
964                            pEnc->bframenum_tail--;
965                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
966    
967                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
968                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
969                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
970                            }
971                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
972    
973                            pFrame->intra = 0;
974    
975                    } else {
976    
977                            FrameCodeI(pEnc, &bs, &bits);
978                  pFrame->intra = 1;                  pFrame->intra = 1;
979    
980                            pEnc->bframenum_dx50bvop = -1;
981                    }
982    
983                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
984    
985                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
986                            BitstreamPad(&bs);
987                            input_valid = 0;
988                            goto ipvop_loop;
989                    }
990    
991                  /*                  /*
992                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
993                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
994                   */                   */
995          }          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
996                  /*                  /*
997                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
998                   */                   */
999    
1000                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1001                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1002                    dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1003                  */  
1004                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1005                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1006                    }
1007    
1008                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1009                  pFrame->intra = 0;                  pFrame->intra = 0;
1010                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1011    
1012                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1013                            BitstreamPad(&bs);
1014                            input_valid = 0;
1015                            goto ipvop_loop;
1016          }          }
1017          else  
1018          {          } else {
1019                  /*                  /*
1020                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1021                   */                   */
1022    
1023                  /* ToDo : Remove dprintf calls */                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1024                  /*                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1025                    dprintf("--- BFRAME (store) ---  head=%i tail=%i",                  }
                   pEnc->bframenum_head,  
                   pEnc->bframenum_tail);  
                 */  
1026    
1027                  if (pFrame->bquant < 1)                  if (pFrame->bquant < 1) {
                 {  
1028                          pEnc->current->quant =                          pEnc->current->quant =
1029                                  ((pEnc->reference->quant+pEnc->current->quant)*\                                  ((pEnc->reference->quant +
1030                                   pEnc->bquant_ratio) / 200;                                    pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1031                  }                  } else {
                 else  
                 {  
1032                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1033                  }                  }
1034                    if (pEnc->current->quant < 1)
1035                            pEnc->current->quant = 1;
1036    
1037                    if (pEnc->current->quant > 31)
1038                            pEnc->current->quant = 31;
1039    
1040    
1041                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1042                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1043                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1044    
1045    
1046    
1047                  /* store frame into bframe buffer & swap ref back to current */                  /* store frame into bframe buffer & swap ref back to current */
1048                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
# Line 852  Line 1052 
1052    
1053                  pFrame->intra = 0;                  pFrame->intra = 0;
1054                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1055    
1056                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1057                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
1058                  }                  }
1059    
1060                  return XVID_ERR_OK;          pEnc->iFrameNum++;
         }  
1061    
1062          BitstreamPad(&bs);          BitstreamPad(&bs);
1063          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1064    
1065          if (pResult)          if (pResult) {
         {  
1066                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1067                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);
1068                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 876  Line 1070 
1070                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1071          }          }
1072    
1073          EMMS();          emms();
1074    
1075  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1076          psnr = image_psnr(&pEnc->sOriginal,          psnr =
1077                            &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1078                            pEnc->mbParam.edged_width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
                           pEnc->mbParam.width,  
1079                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1080    
1081          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1082          DEBUG(temp);          DEBUG(temp);
1083  #endif  #endif
1084    
1085          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1086          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1087                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
1088          }          }
1089    
         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;  
         }  
         pFrame->input_consumed = 1;  
1090    
1091          stop_global_timer();          stop_global_timer();
1092          write_timer();          write_timer();
1093    
1094          return XVID_ERR_OK;          return XVID_ERR_OK;
1095  }  }
1096  #else  
1097    #endif
1098    
1099    
1100    
1101  /*****************************************************************************  /*****************************************************************************
1102   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1103     *
1104     * Returned values :
1105     *    - XVID_ERR_OK     - no errors
1106     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1107     *                        format
1108   ****************************************************************************/   ****************************************************************************/
1109    
1110  int  int
1111  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1112                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 1116 
1116          Bitstream bs;          Bitstream bs;
1117          uint32_t bits;          uint32_t bits;
1118          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1119  #ifdef _DEBUG  
1120    #ifdef _DEBUG_PSNR
1121          float psnr;          float psnr;
1122          uint8_t temp[128];          uint8_t temp[128];
1123  #endif  #endif
# Line 940  Line 1133 
1133    
1134          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1135          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1136    #ifdef BFRAMES
1137            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1138            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1139    #endif
1140          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1141    
1142          start_timer();          start_timer();
1143          if (image_input(&pEnc->current->image,          if (image_input
1144                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1145                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
1146                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1147          stop_conv_timer();          stop_conv_timer();
1148    
1149  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1150          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1151                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
1152  #endif  #endif
1153    
1154          EMMS();          emms();
1155    
1156          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1157    
1158          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1159                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1160          }          } else {
         else  
         {  
1161                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1162          }          }
1163    
1164          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1165          {                  int *temp_dquants =
1166                  int * temp_dquants = (int *)                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1167                          xvid_malloc(pEnc->mbParam.mb_width * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                     pEnc->mbParam.mb_height * \  
                                     sizeof(int),  
1168                                      CACHE_LINE);                                      CACHE_LINE);
1169    
1170                  pEnc->current->quant =                  pEnc->current->quant =
1171                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
1172                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1173                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
1174                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
1175                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
1176                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
1177    
1178                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
1179    
1180                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1181    
1182                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
1183    
1184    
1185                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1186                                          &pEnc->current->mbs[OFFSET(x,y)];  
1187                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
1188                          }                          }
1189    
1190                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 1193 
1193                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1194          }          }
1195    
1196          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
1197                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1198                          write_vol_header = 1;                          write_vol_header = 1;
1199                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1200          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
1201                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
1202    
1203                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 1209 
1209    
1210                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1211                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1212                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
1213                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1214                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1215                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
1216                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1217                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1218                  }                  }
# Line 1044  Line 1220 
1220                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
1221          }          }
1222    
1223          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1224          {                  if ((pEnc->iFrameNum == 0)
1225                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
1226                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
1227                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1228                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
1229                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1230                          pFrame->intra = FrameCodeI(pEnc,                  }
1231                                                     &bs,          } else {
1232                                                     &bits);                  if (pFrame->intra == 1) {
1233                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1234                  else                  } else {
1235                  {                          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);  
1236                  }                  }
1237    
1238          }          }
# Line 1088  Line 1242 
1242          BitstreamPad(&bs);          BitstreamPad(&bs);
1243          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1244    
1245          if (pResult)          if (pResult) {
         {  
1246                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1247                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1248                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 1250 
1250                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1251          }          }
1252    
1253          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1254    
1255  #ifdef _DEBUG          if (pFrame->quant == 0) {
1256          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1257                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
1258                            pEnc->mbParam.edged_width,          }
1259                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
1260            psnr =
1261                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1262                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1263                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1264    
1265          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1266          DEBUG(temp);          DEBUG(temp);
1267  #endif  #endif
1268    
1269    #ifdef BFRAMES
1270            inc_frame_num(pEnc);
1271    #endif
1272          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1273    
1274          stop_global_timer();          stop_global_timer();
# Line 1125  Line 1276 
1276    
1277          return XVID_ERR_OK;          return XVID_ERR_OK;
1278  }  }
 #endif  
1279    
1280    
1281  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1282    CodeIntraMB(Encoder * pEnc,
1283                            MACROBLOCK * pMB)
1284    {
1285    
1286          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1287    
# Line 1139  Line 1292 
1292          pMB->sad16 = 0;          pMB->sad16 = 0;
1293    
1294          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1295                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1296                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1297                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1298    
1299                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1300                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1301                            if (pEnc->current->quant < 1)
1302                                    pEnc->current->quant = 1;
1303                  }                  }
1304          }          }
1305    
# Line 1156  Line 1310 
1310  #define FCODEBITS       3  #define FCODEBITS       3
1311  #define MODEBITS        5  #define MODEBITS        5
1312    
1313  void HintedMESet(Encoder * pEnc, int * intra)  void
1314    HintedMESet(Encoder * pEnc,
1315                            int *intra)
1316  {  {
1317          HINTINFO * hint;          HINTINFO * hint;
1318          Bitstream bs;          Bitstream bs;
# Line 1165  Line 1321 
1321    
1322          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1323    
1324          if (hint->rawhints)          if (hint->rawhints) {
         {  
1325                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1326          }          } else {
         else  
         {  
1327                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1328                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1329          }          }
1330    
1331          if (*intra)          if (*intra) {
         {  
1332                  return;                  return;
1333          }          }
1334    
1335          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1336                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1337                                                                                                                                     FCODEBITS);
1338    
1339          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1340          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1341    
1342          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1343          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1344                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1345                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1346                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1347                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1348                          VECTOR pred[4];                          VECTOR pred;
1349                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1350                          int vec;                          int vec;
1351    
1352                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1353                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1354                                                                                                                                      MODEBITS);
1355    
1356                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1357                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1358    
1359                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1360                          {                                  tmp.x =
1361                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1362                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1363                                    tmp.y =
1364                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1365                                                                                                                                                      length);
1366                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1367                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1368    
1369                                  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);
1370    
1371                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1372                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1373                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1374                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1375                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
1376                          }                          }
1377                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
1378                          {                                  for (vec = 0; vec < 4; ++vec) {
1379                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
1380                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
1381                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
1382                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
1383                                                    (hint->rawhints) ? bhint->mvs[vec].
1384                                                    y : BitstreamGetBits(&bs, length);
1385                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1386                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1387    
1388                                          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);
1389    
1390                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1391                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1392                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1393                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1394                                  }                                  }
1395                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1396                                  {                                  {
1397                                    for (vec = 0; vec < 4; ++vec) {
1398                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1399                                  }                                  }
1400                          }                          }
1401    
1402                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1403                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1404                          {                                  && pMB->dquant != NO_CHANGE) {
1405                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1406    
1407                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1408                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1409                                  }                                  }
1410                          }                          }
# Line 1258  Line 1413 
1413  }  }
1414    
1415    
1416  void HintedMEGet(Encoder * pEnc, int intra)  void
1417    HintedMEGet(Encoder * pEnc,
1418                            int intra)
1419  {  {
1420          HINTINFO * hint;          HINTINFO * hint;
1421          Bitstream bs;          Bitstream bs;
# Line 1267  Line 1424 
1424    
1425          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1426    
1427          if (hint->rawhints)          if (hint->rawhints) {
         {  
1428                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1429          }          } else {
         else  
         {  
1430                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1431                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1432          }          }
1433    
1434          if (intra)          if (intra) {
1435          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1436                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1437                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1438                  }                  }
# Line 1290  Line 1442 
1442          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1443          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1444    
1445          if (hint->rawhints)          if (hint->rawhints) {
         {  
1446                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1447          }          } else {
         else  
         {  
1448                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1449          }          }
1450    
1451          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1452          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1453                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1454                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1455                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1456                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1457                          VECTOR tmp;                          VECTOR tmp;
1458    
1459                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1460                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1461                          }                          } else {
                         else  
                         {  
1462                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1463                          }                          }
1464    
1465                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1466                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1467                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1468                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1469                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1470    
1471                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1472                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1473                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1474                                  }                                  } else {
                                 else  
                                 {  
1475                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1476                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1477                                  }                                  }
1478                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1479                                  int vec;                                  int vec;
1480    
1481                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1482                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1483                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1484                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1485                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1486    
1487                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1488                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1489                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1490                                          }                                          } else {
                                         else  
                                         {  
1491                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1492                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1493                                          }                                          }
# Line 1360  Line 1496 
1496                  }                  }
1497          }          }
1498    
1499          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1500                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1501                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1502          }          }
1503  }  }
1504    
1505    
1506  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1507    FrameCodeI(Encoder * pEnc,
1508                       Bitstream * bs,
1509                       uint32_t * pBits)
1510  {  {
1511    
1512          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 1382  Line 1520 
1520          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1521    
1522          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1523          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1524    #define DIVX501B481P "DivX501b481p"
1525            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1526                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1527            }
1528    #endif
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 16
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;
1596    
# Line 1443  Line 1598 
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,  
1603                         pEnc->current->global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
1604          stop_edges_timer();          stop_edges_timer();
1605    
# Line 1456  Line 1608 
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->current->rounding_type);                                    pEnc->current->rounding_type);
1623                  stop_inter_timer();                  stop_inter_timer();
1624          }          }
1625    
1626          start_timer();          start_timer();
1627          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1628                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1629          }          } else {
1630    
1631    #ifdef _SMP
1632            if (pEnc->mbParam.num_threads > 1)
1633                    bIntra =
1634                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1635                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1636                                                     iLimit);
1637          else          else
1638          {  #endif
1639                  bIntra = MotionEstimation(                  bIntra =
1640                          &pEnc->mbParam,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1641                          pEnc->current,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1642                          iLimit);                          iLimit);
1643    
1644          }          }
1645          stop_motion_timer();          stop_motion_timer();
1646    
1647          if (bIntra == 1)          if (bIntra == 1) {
         {  
1648                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1649          }          }
1650    
# Line 1496  Line 1653 
1653          if(vol_header)          if(vol_header)
1654                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1655    
1656          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1657    
1658          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1659    
# Line 1505  Line 1662 
1662          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1663          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1664    
1665          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1666          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1667                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1668                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1669    
1670                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1671    
1672                          if (!bIntra)                          if (!bIntra) {
                         {  
1673                                  start_timer();                                  start_timer();
1674                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1675                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1676                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1677                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1678                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1679                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1680                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1534  Line 1684 
1684                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1685                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1686                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1687                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1688                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1689                                                    else if (pEnc->current->quant < 1)
1690                                                            pEnc->current->quant = 1;
1691                                          }                                          }
1692                                  }                                  }
1693                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1694    
1695                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1696    
1697                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
1698                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1699                          else                                                                            dct_codes, qcoeff);
1700                          {                          } else {
1701                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1702                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1703                                                                      dct_codes, qcoeff);
1704                          }                          }
1705    
1706                          start_timer();                          start_timer();
1707                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1708                          stop_prediction_timer();                          stop_prediction_timer();
1709    
1710                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1711                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1712                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1713                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1714                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1715                                    pEnc->sStat.mblks++;
1716                            }  else {
1717                                    pEnc->sStat.ublks++;
1718                          }                          }
1719                          else if (pMB->cbp ||  
1720                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1721                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1722                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1723                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1724                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1725                                    pMB->mvs[0].y == 0) {
1726    
1727    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1728    
1729    #ifdef BFRAMES
1730                                    int iSAD=BFRAME_SKIP_THRESHHOLD;
1731                                    int bSkip=1;
1732    
1733                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1734                          {                          {
1735                                  pEnc->sStat.mblks++;                                          iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1736                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1737                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1738                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD)
1739                                            {       bSkip = 0;
1740                                                    break;
1741                          }                          }
1742                          else                                  }
1743                                    if (!bSkip)
1744                          {                          {
1745                                  pEnc->sStat.ublks++;                                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1746                                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1747                                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1748                                                    qcoeff[5*64+0]=0;
1749                                            }
1750                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1751                                            pMB->cbp = 0x80;                /* trick! so cbp!=0, but still nothing is written to bs */
1752                          }                          }
1753                                    else
1754                                            MBSkip(bs);
1755    
1756                          start_timer();  #else
1757                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1758    
1759    #endif
1760    
1761                            } else {
1762                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1763                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1764                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1765                                            qcoeff[5*64+0]=0;
1766                                    }
1767                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1768                            }
1769    
1770                          stop_coding_timer();                          stop_coding_timer();
1771                  }                  }
1772          }          }
1773    
1774          emms();          emms();
1775    
1776          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1777                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1778          }          }
1779    
# Line 1596  Line 1789 
1789          {          {
1790                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1791                  iSearchRange *= 2;                  iSearchRange *= 2;
1792          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1793                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1794                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1795                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1608  Line 1800 
1800    
1801          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1802    
1803    #ifdef BFRAMES
1804            /* frame drop code */
1805            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1806            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1807                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1808            {
1809                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1810                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1811    
1812                    BitstreamReset(bs);
1813                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1814    
1815                    // copy reference frame details into the current frame
1816                    pEnc->current->quant = pEnc->reference->quant;
1817                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1818                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1819                    pEnc->current->fcode = pEnc->reference->fcode;
1820                    pEnc->current->bcode = pEnc->reference->bcode;
1821                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1822                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1823    
1824            }
1825    #endif
1826    
1827          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1828    
1829    #ifdef BFRAMES
1830            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) %
1831                            (int32_t)pEnc->mbParam.fbase;
1832            pEnc->last_pframe = pEnc->current->ticks;
1833    #endif
1834    
1835          return 0;                                        // inter          return 0;                                        // inter
1836  }  }
1837    
1838    
1839  #ifdef BFRAMES  #ifdef BFRAMES
1840  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void
1841    FrameCodeB(Encoder * pEnc,
1842                       FRAMEINFO * frame,
1843                       Bitstream * bs,
1844                       uint32_t * pBits)
1845  {  {
1846      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1847      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
# Line 1626  Line 1852 
1852      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1853          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1854    
1855    #ifdef BFRAMES_DEC_DEBUG
1856            FILE *fp;
1857            static char first=0;
1858    #define BFRAME_DEBUG    if (!first && fp){ \
1859                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1860            }
1861    
1862            if (!first){
1863                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1864            }
1865    #endif
1866    
1867          // forward          // forward
1868          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,
1869                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1870                                       pEnc->mbParam.height,
1871                                       frame->global_flags & XVID_INTERLACING);
1872          start_timer();          start_timer();
1873          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,
1874                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1875                                              0);
1876          stop_inter_timer();          stop_inter_timer();
1877    
1878          // backward          // backward
1879          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,
1880                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1881                                       pEnc->mbParam.height,
1882                                       frame->global_flags & XVID_INTERLACING);
1883      start_timer();      start_timer();
1884          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1885                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1886                                              0);
1887          stop_inter_timer();          stop_inter_timer();
1888    
1889          start_timer();          start_timer();
1890          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1891                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase,
1892                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                  pEnc->time_pp,
1893                                                    pEnc->reference->mbs, f_ref,
1894                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1895                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1896                                                     &pEnc->vInterV, &pEnc->vInterHV);
1897    
1898    
1899          stop_motion_timer();          stop_motion_timer();
# Line 1654  Line 1904 
1904          }*/          }*/
1905    
1906      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1907      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1908    
1909      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1910    
# Line 1664  Line 1914 
1914          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1915    
1916    
1917      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
         {  
1918                  // reset prediction                  // reset prediction
1919    
1920                  forward.x = 0;                  forward.x = 0;
# Line 1673  Line 1922 
1922                  backward.x = 0;                  backward.x = 0;
1923                  backward.y = 0;                  backward.y = 0;
1924    
1925                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1926                  {                          MACROBLOCK *f_mb =
1927                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1928                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *b_mb =
1929                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1930                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1931    
1932                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1933                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
                         {  
1934                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1935                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1936    
1937                                    mb->cbp = 0;
1938    #ifdef BFRAMES_DEC_DEBUG
1939            BFRAME_DEBUG
1940    #endif
1941                                  continue;                                  continue;
1942                          }                          }
1943    
1944                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1945                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1946                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1947                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1948                                          dct_codes);                                          dct_codes);
1949    
1950                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1951                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);                          mb->cbp =
1952                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1953                                                                      qcoeff);
1954                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1955    
1956                            if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1957                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  && (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) {
1958                                  mb->cbp == 0 &&                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
1959                          }                          }
1960    
1961                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)  /* update predictors for forward and backward vectors */
1962                          {                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1963                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1964                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1965                                  forward.x = mb->mvs[0].x;                                  forward.x = mb->mvs[0].x;
1966                                  forward.y = mb->mvs[0].y;                                  forward.y = mb->mvs[0].y;
1967                          }                          }
1968    
1969                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
                         {  
1970                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1971                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1972                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1973                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1974                          }                          }
1975    
1976  //                      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);  //                      DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y);
1977    
1978    #ifdef BFRAMES_DEC_DEBUG
1979            BFRAME_DEBUG
1980    #endif
1981                          start_timer();                          start_timer();
1982                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1983                                                     &pEnc->sStat);
1984                          stop_coding_timer();                          stop_coding_timer();
1985                  }                  }
1986          }          }
# Line 1734  Line 1990 
1990          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1991    
1992          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1993    
1994    #ifdef BFRAMES_DEC_DEBUG
1995            if (!first){
1996                    first=1;
1997                    if (fp)
1998                            fclose(fp);
1999            }
2000    #endif
2001  }  }
2002  #endif  #endif
2003    
2004    
2005    /*      in case internal output is needed somewhere... */
2006    /*      {
2007            FILE *filehandle;
2008            filehandle=fopen("last-b.pgm","wb");
2009            if (filehandle)
2010            {
2011                    fprintf(filehandle,"P5\n\n");           //
2012                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
2013                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
2014                    fclose(filehandle);
2015                    }
2016            }
2017    */

Legend:
Removed from v.1.40  
changed lines
  Added in v.1.73

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