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

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

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

revision 194, Sun Jun 9 23:30:50 2002 UTC revision 324, Sun Jul 21 03:49:47 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.40 2002-06-09 23:30:50 edgomez Exp $   *  $Id: encoder.c,v 1.60 2002-07-21 03:41:44 suxen_drol Exp $
43   *   *
44   ****************************************************************************/   ****************************************************************************/
45    
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <stdio.h>  #include <stdio.h>
48  #include <math.h>  #include <math.h>
49    #include <string.h>
50    
51  #include "encoder.h"  #include "encoder.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "global.h"  #include "global.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "image/image.h"  #include "image/image.h"
56    #ifdef BFRAMES
57    #include "image/font.h"
58    #endif
59  #include "motion/motion.h"  #include "motion/motion.h"
60  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
61  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 61  Line 68 
68  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
69  #include "utils/mem_align.h"  #include "utils/mem_align.h"
70    
71    #ifdef _SMP
72    #include "motion/smp_motion_est.h"
73    #endif
74  /*****************************************************************************  /*****************************************************************************
75   * Local macros   * Local macros
76   ****************************************************************************/   ****************************************************************************/
# Line 93  Line 103 
103   * Local data   * Local data
104   ****************************************************************************/   ****************************************************************************/
105    
106  static int DQtab[4] =  static int DQtab[4] = {
 {  
107          -1, -2, 1, 2          -1, -2, 1, 2
108  };  };
109    
110  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
111          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
112  };  };
113    
114    
115  static void __inline image_null(IMAGE * image)  static void __inline
116    image_null(IMAGE * image)
117  {  {
118          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
119  }  }
# Line 132  Line 141 
141  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
142  {  {
143          Encoder *pEnc;          Encoder *pEnc;
144          uint32_t i;          int i;
145    
146          pParam->handle = NULL;          pParam->handle = NULL;
147    
# Line 145  Line 154 
154    
155          /* Fps */          /* Fps */
156    
157          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
158                  pParam->fincr = 1;                  pParam->fincr = 1;
159                  pParam->fbase = 25;                  pParam->fbase = 25;
160          }          }
# Line 157  Line 165 
165           */           */
166    
167          i = pParam->fincr;          i = pParam->fincr;
168          while (i > 1)          while (i > 1) {
169          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
170                          pParam->fincr /= i;                          pParam->fincr /= i;
171                          pParam->fbase /= i;                          pParam->fbase /= i;
172                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 175 
175                  i--;                  i--;
176          }          }
177    
178          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
179                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
180    
181                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
182                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
183          }          }
# Line 206  Line 212 
212          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval == 0)
213                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
214    
   
215          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
216          if (pEnc == NULL)          if (pEnc == NULL)
217                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
218    
219            /* Zero the Encoder Structure */
220    
221            memset(pEnc, 0, sizeof(Encoder));
222    
223          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
224    
225          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 234 
234          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
235          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
236    
237            pEnc->mbParam.m_quant_type = H263_QUANT;
238    
239    #ifdef _SMP
240            pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS);
241    #endif
242    
243          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
244    
245          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 259 
259    
260          /* try to allocate mb memory */          /* try to allocate mb memory */
261    
262          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
263                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
264                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
265                                           CACHE_LINE);          pEnc->reference->mbs =
266          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
267                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
268    
269          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
270                  goto xvid_err_memory2;                  goto xvid_err_memory2;
271    
272          /* try to allocate image memory */          /* try to allocate image memory */
273    
274  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
275          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
276  #endif  #endif
277  #ifdef BFRAMES  #ifdef BFRAMES
# Line 274  Line 287 
287          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
288          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
289    
290  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
291          if (image_create(&pEnc->sOriginal,          if (image_create
292                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
293                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
294                  goto xvid_err_memory3;                  goto xvid_err_memory3;
295  #endif  #endif
296  #ifdef BFRAMES  #ifdef BFRAMES
297          if (image_create(&pEnc->f_refh,          if (image_create
298                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
299                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
300                  goto xvid_err_memory3;                  goto xvid_err_memory3;
301          if (image_create(&pEnc->f_refv,          if (image_create
302                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
303                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
304                  goto xvid_err_memory3;                  goto xvid_err_memory3;
305          if (image_create(&pEnc->f_refhv,          if (image_create
306                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
307                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
308                  goto xvid_err_memory3;                  goto xvid_err_memory3;
309  #endif  #endif
310          if (image_create(&pEnc->current->image,          if (image_create
311                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
312                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
313                  goto xvid_err_memory3;                  goto xvid_err_memory3;
314          if (image_create(&pEnc->reference->image,          if (image_create
315                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
316                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
317                  goto xvid_err_memory3;                  goto xvid_err_memory3;
318          if (image_create(&pEnc->vInterH,          if (image_create
319                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
320                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
321                  goto xvid_err_memory3;                  goto xvid_err_memory3;
322          if (image_create(&pEnc->vInterV,          if (image_create
323                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
324                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
325                  goto xvid_err_memory3;                  goto xvid_err_memory3;
326          if (image_create(&pEnc->vInterVf,          if (image_create
327                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
328                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
329                  goto xvid_err_memory3;                  goto xvid_err_memory3;
330          if (image_create(&pEnc->vInterHV,          if (image_create
331                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
332                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
333                  goto xvid_err_memory3;                  goto xvid_err_memory3;
334          if (image_create(&pEnc->vInterHVf,          if (image_create
335                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
336                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
337                  goto xvid_err_memory3;                  goto xvid_err_memory3;
338    
# Line 328  Line 341 
341          /* B Frames specific init */          /* B Frames specific init */
342  #ifdef BFRAMES  #ifdef BFRAMES
343    
344            pEnc->global = pParam->global;
345          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
346          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
347            pEnc->frame_drop_ratio = pParam->frame_drop_ratio;
348          pEnc->bframes = NULL;          pEnc->bframes = NULL;
349    
350          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
351                  int n;                  int n;
352    
353                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
354                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
355                                              CACHE_LINE);                                              CACHE_LINE);
356    
357                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 347  Line 361 
361                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
362    
363    
364                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
365                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
366    
367                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
368                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
369    
370                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
371                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
372                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
373    
374                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
375                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
376    
377                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
378    
379                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
380                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
381                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
382                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
383    
# Line 376  Line 387 
387          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
388          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
389          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
390            pEnc->bframenum_dx50bvop = -1;
391    
392            pEnc->queue = NULL;
393    
394    
395            if (pEnc->mbParam.max_bframes > 0) {
396                    int n;
397    
398                    pEnc->queue =
399                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
400                                                    CACHE_LINE);
401    
402                    if (pEnc->queue == NULL)
403                            goto xvid_err_memory4;
404    
405                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
406                            image_null(&pEnc->queue[n]);
407    
408                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
409                            if (image_create
410                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
411                                     pEnc->mbParam.edged_height) < 0)
412                                    goto xvid_err_memory5;
413    
414                    }
415            }
416    
417            pEnc->queue_head = 0;
418            pEnc->queue_tail = 0;
419            pEnc->queue_size = 0;
420    
421    
422          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
423          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
424            pEnc->m_framenum = 0;
425            pEnc->last_pframe = 1;
426  #endif  #endif
427    
428          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
429    
430          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
431          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
432                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
433                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
434                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
435                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
436          }          }
437    
438          init_timer();          init_timer();
# Line 403  Line 443 
443           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
444           */           */
445  #ifdef BFRAMES  #ifdef BFRAMES
446      xvid_err_memory5:
447    
448    
449            if (pEnc->mbParam.max_bframes > 0) {
450    
451                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
452                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
453                                                      pEnc->mbParam.edged_height);
454                    }
455                    xvid_free(pEnc->queue);
456            }
457    
458   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
459    
460                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
461    
462                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
463                                pEnc->mbParam.edged_width,  
464                            if (pEnc->bframes[i] == NULL)
465                                    continue;
466    
467                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
468                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
469    
470                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 420  Line 474 
474          }          }
475    
476          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
477            }
478    
479  #endif  #endif
480    
481   xvid_err_memory3:   xvid_err_memory3:
482  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
483          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
484                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
485  #endif  #endif
486    
487  #ifdef BFRAMES  #ifdef BFRAMES
488          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
489                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
490          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
491                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
492          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
493                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
494  #endif  #endif
495    
496          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
497                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
498          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
499                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
500          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
501                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
502          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
503                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
504          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
505                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
506          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
507                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
508          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
509                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
510    
511   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 536 
536  int  int
537  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
538  {  {
539    #ifdef BFRAMES
540            int i;
541    #endif
542    
543          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
544    
545          /* B Frames specific */          /* B Frames specific */
546  #ifdef BFRAMES  #ifdef BFRAMES
547          int i;          if (pEnc->mbParam.max_bframes > 0) {
548    
549          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
         {  
550    
551                  if (pEnc->bframes[i] == NULL) continue;                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
552                                              pEnc->mbParam.edged_height);
553                    }
554                    xvid_free(pEnc->queue);
555            }
556    
557                  image_destroy(&pEnc->bframes[i]->image,  
558                                pEnc->mbParam.edged_width,          if (pEnc->mbParam.max_bframes > 0) {
559    
560                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
561    
562                            if (pEnc->bframes[i] == NULL)
563                                    continue;
564    
565                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
566                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
567    
568                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
569    
570                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
571          }          }
572    
573          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
574    
575            }
576  #endif  #endif
577    
578          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
579    
580          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
581                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
582          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
583                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
584          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
585                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
586          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
587                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
588          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
589                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
590          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
591                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
592          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
593                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
594  #ifdef BFRAMES  #ifdef BFRAMES
595          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
596                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
597          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
598                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
599          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
600                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
601  #endif  #endif
602  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
603          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
604                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
605  #endif  #endif
606    
# Line 570  Line 617 
617          return XVID_ERR_OK;          return XVID_ERR_OK;
618  }  }
619    
620    
621    #ifdef BFRAMES
622    void inc_frame_num(Encoder * pEnc)
623    {
624            pEnc->iFrameNum++;
625            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
626    
627            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
628            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
629    }
630    #endif
631    
632    
633    #ifdef BFRAMES
634    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
635    {
636            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
637            {
638                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
639                    return;
640            }
641    
642            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
643                                    pEnc->bframenum_head, pEnc->bframenum_tail,
644                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
645    
646    
647            start_timer();
648            if (image_input
649                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
650                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
651                    return;
652            stop_conv_timer();
653    
654            pEnc->queue_size++;
655            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
656    }
657    #endif
658    
659    
660    #ifdef BFRAMES
661  /*****************************************************************************  /*****************************************************************************
662   * 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.  
663   *   *
664   * Returned values :   * Returned values :
665   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 582  Line 667 
667   *                        format   *                        format
668   ****************************************************************************/   ****************************************************************************/
669    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
670  int  int
671  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
672                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
673                 XVID_ENC_STATS * pResult)                 XVID_ENC_STATS * pResult)
674  {  {
# Line 596  Line 676 
676          Bitstream bs;          Bitstream bs;
677          uint32_t bits;          uint32_t bits;
678    
679  #ifdef _DEBUG          int input_valid = 1;
680    
681    #ifdef _DEBUG_PSNR
682          float psnr;          float psnr;
683          char temp[128];          char temp[128];
684  #endif  #endif
685    
686          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
687          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
688            ENC_CHECK(pFrame->image);
689    
690          start_global_timer();          start_global_timer();
691    
692          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
693    
694    ipvop_loop:
695    
696          /*          /*
697           * bframe "flush" code           * bframe "flush" code
698           */           */
699    
700          if ( (pFrame->image == NULL || pEnc->flush_bframes) &&          if ((pFrame->image == NULL || pEnc->flush_bframes)
701               (pEnc->bframenum_head < pEnc->bframenum_tail))                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
         {  
702    
703                  if (pEnc->flush_bframes == 0)                  if (pEnc->flush_bframes == 0) {
                 {  
704                          /*                          /*
705                           * we have reached the end of stream without getting                           * we have reached the end of stream without getting
706                           * a future reference frame... so encode last final                           * a future reference frame... so encode last final
707                           * frame as a pframe                           * frame as a pframe
708                           */                           */
709    
710                          /* ToDo : remove dprintf calls */                          DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
711                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
712                            dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
713                          */  
714                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
715                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
716    
717                          SWAP(pEnc->current,                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                              pEnc->bframes[pEnc->bframenum_tail]);  
718    
719                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
720    
721                          BitstreamPad(&bs);                          BitstreamPad(&bs);
722                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
723                          pFrame->intra = 0;                          pFrame->intra = 0;
724    
725                          return XVID_ERR_OK;                          return XVID_ERR_OK;
726                  }                  }
727    
                 /* ToDo : remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (flush) --- ");  
                 */  
                 FrameCodeB(pEnc,  
                            pEnc->bframes[pEnc->bframenum_head],  
                            &bs,  
                            &bits);  
                 pEnc->bframenum_head++;  
728    
729                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
730                                    pEnc->bframenum_head, pEnc->bframenum_tail,
731                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
732    
733                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
734                    pEnc->bframenum_head++;
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                    if (input_valid)
741                            queue_image(pEnc, pFrame);
742    
743                  return XVID_ERR_OK;                  return XVID_ERR_OK;
744          }          }
745    
746          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
747          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
748                  pFrame->length = 0;  
749                  pFrame->input_consumed = 1;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
750    
751                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
752                                    pEnc->bframenum_head, pEnc->bframenum_tail,
753                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
754    
755                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
756                            BitstreamPad(&bs);
757                            BitstreamPutBits(&bs, 0x7f, 8);
758    
759                            pFrame->length = BitstreamLength(&bs);
760                  pFrame->intra = 0;                  pFrame->intra = 0;
761    
762                            if (input_valid)
763                                    queue_image(pEnc, pFrame);
764    
765                  return XVID_ERR_OK;                  return XVID_ERR_OK;
766          }          }
767            }
768    
769    
770    bvop_loop:
771    
772            if (pEnc->bframenum_dx50bvop != -1)
773            {
774    
775                    SWAP(pEnc->current, pEnc->reference);
776                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
777    
778                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
779                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
780                    }
781    
782          if (pEnc->bframenum_head > 0)                  if (input_valid)
783          {          {
784                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                          queue_image(pEnc, pFrame);
785                            input_valid = 0;
786                    }
787    
788            } else if (input_valid) {
789    
790                    SWAP(pEnc->current, pEnc->reference);
791    
792                    start_timer();
793                    if (image_input
794                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
795                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
796                            return XVID_ERR_FORMAT;
797                    stop_conv_timer();
798    
799                    // queue input frame, and dequue next image
800                    if (pEnc->queue_size > 0)
801                    {
802                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
803                            if (pEnc->queue_head != pEnc->queue_tail)
804                            {
805                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
806                            }
807                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
808                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
809                    }
810    
811            } else if (pEnc->queue_size > 0) {
812    
813                    SWAP(pEnc->current, pEnc->reference);
814    
815                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
816                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
817                    pEnc->queue_size--;
818    
819            } else if (BitstreamPos(&bs) == 0) {
820    
821                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
822                                    pEnc->bframenum_head, pEnc->bframenum_tail,
823                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
824    
825                    pFrame->intra = 0;
826    
827                    BitstreamPutBits(&bs, 0x7f, 8);
828                    BitstreamPad(&bs);
829                    pFrame->length = BitstreamLength(&bs);
830    
831                    return XVID_ERR_OK;
832    
833            } else {
834    
835                    pFrame->length = BitstreamLength(&bs);
836                    return XVID_ERR_OK;
837          }          }
838    
839          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 684  Line 843 
843           * comment style :-)           * comment style :-)
844           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
845    
846          SWAP(pEnc->current, pEnc->reference);          emms();
   
         EMMS();  
847    
848            // only inc frame num, adapt quant, etc. if we havent seen it before
849            if (pEnc->bframenum_dx50bvop < 0 )
850            {
851          if (pFrame->quant == 0)          if (pFrame->quant == 0)
852                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
853          else          else
# Line 701  Line 861 
861    
862          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
863          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
864          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
         pEnc->current->ticks = pEnc->mbParam.m_ticks;  
865          /* ToDo : dynamic fcode (in both directions) */          /* ToDo : dynamic fcode (in both directions) */
866          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
867          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
868    
869          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
870          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();  
871    
872  #ifdef _DEBUG                  inc_frame_num(pEnc);
873          image_copy(&pEnc->sOriginal,  
874                     &pEnc->current->image,  #ifdef _DEBUG_PSNR
875                     pEnc->mbParam.edged_width,                  image_copy(&pEnc->sOriginal, &pEnc->current->image,
876                     pEnc->mbParam.height);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
877  #endif  #endif
878    
879          EMMS();                  emms();
880    
881                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
882                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
883                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
884                    }
885    
886          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887           * Luminance masking           * Luminance masking
888           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
889    
890          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
         {  
891                  int *temp_dquants =                  int *temp_dquants =
892                          (int *) xvid_malloc(pEnc->mbParam.mb_width * \                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
893                                              pEnc->mbParam.mb_height * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                             sizeof(int),  
894                                              CACHE_LINE);                                              CACHE_LINE);
895    
896                  pEnc->current->quant =                  pEnc->current->quant =
897                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
898                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
899                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
900                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
901                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
902                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
903    
904                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
905    
906                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
907    
908                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
909                          {                                          MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
910                                  MACROBLOCK *pMB =  
911                                          &pEnc->current->mbs[OFFSET(x,y)];                                          pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                 pMB->dquant =  
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
912                          }                          }
913    
914                          #undef OFFSET                          #undef OFFSET
   
915                  }                  }
916    
917                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
918          }          }
919    
920            }
921    
922          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923           * ivop/pvop/bvop selection           * ivop/pvop/bvop selection
924           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
925    
926    
927          if (pEnc->iFrameNum == 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
928              pFrame->intra   == 1 ||                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
929                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
930              (pFrame->intra < 0  &&                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
931               pEnc->iMaxKeyInterval > 0 &&                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
932               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)  
         {  
933                  /*                  /*
934                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
935                   */                   */
936    
937                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
938                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
939                    dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                 */  
940    
941                  FrameCodeI(pEnc, &bs, &bits);                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
942                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
943                    }
944    
945                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
946    
947                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
948    
949                            pEnc->bframenum_tail--;
950                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
951    
952                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
953                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
954                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
955                            }
956                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
957    
958                            pFrame->intra = 0;
959    
960                    } else {
961    
962                            FrameCodeI(pEnc, &bs, &bits);
963                  pFrame->intra = 1;                  pFrame->intra = 1;
964    
965                            pEnc->bframenum_dx50bvop = -1;
966                    }
967    
968                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
969    
970                    if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
971                            BitstreamPad(&bs);
972                            input_valid = 0;
973                            goto ipvop_loop;
974                    }
975    
976                  /*                  /*
977                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
978                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
979                   */                   */
980          }          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
981                  /*                  /*
982                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
983                   */                   */
984    
985                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
986                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
987                    dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
988                  */  
989                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
990                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
991                    }
992    
993                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
994                  pFrame->intra = 0;                  pFrame->intra = 0;
995                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
996    
997                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
998                            BitstreamPad(&bs);
999                            input_valid = 0;
1000                            goto ipvop_loop;
1001          }          }
1002          else  
1003          {          } else {
1004                  /*                  /*
1005                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
1006                   */                   */
1007    
1008                  /* ToDo : Remove dprintf calls */                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1009                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
1010                    dprintf("--- BFRAME (store) ---  head=%i tail=%i",                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                   pEnc->bframenum_head,  
                   pEnc->bframenum_tail);  
                 */  
1011    
1012                  if (pFrame->bquant < 1)                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1013                  {                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                         pEnc->current->quant =  
                                 ((pEnc->reference->quant+pEnc->current->quant)*\  
                                  pEnc->bquant_ratio) / 200;  
1014                  }                  }
1015                  else  
1016                  {                  if (pFrame->bquant < 1) {
1017                            pEnc->current->quant =
1018                                    ((pEnc->reference->quant +
1019                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1020                    } else {
1021                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1022                  }                  }
1023    
# Line 852  Line 1029 
1029    
1030                  pFrame->intra = 0;                  pFrame->intra = 0;
1031                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
   
                 pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
                 if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
1032    
1033                  return XVID_ERR_OK;                  input_valid = 0;
1034                    goto bvop_loop;
1035          }          }
1036    
1037          BitstreamPad(&bs);          BitstreamPad(&bs);
1038          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1039    
1040          if (pResult)          if (pResult) {
         {  
1041                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1042                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);
1043                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 876  Line 1045 
1045                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1046          }          }
1047    
1048          EMMS();          emms();
1049    
1050  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1051          psnr = image_psnr(&pEnc->sOriginal,          psnr =
1052                            &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1053                            pEnc->mbParam.edged_width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
                           pEnc->mbParam.width,  
1054                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1055    
1056          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1057          DEBUG(temp);          DEBUG(temp);
1058  #endif  #endif
1059    
1060          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1061          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1062                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
1063          }          }
1064    
         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;  
1065    
1066          stop_global_timer();          stop_global_timer();
1067          write_timer();          write_timer();
1068    
1069          return XVID_ERR_OK;          return XVID_ERR_OK;
1070  }  }
1071  #else  
1072    #endif
1073    
1074    
1075    
1076  /*****************************************************************************  /*****************************************************************************
1077   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1078     *
1079     * Returned values :
1080     *    - XVID_ERR_OK     - no errors
1081     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1082     *                        format
1083   ****************************************************************************/   ****************************************************************************/
1084    
1085  int  int
1086  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1087                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 1091 
1091          Bitstream bs;          Bitstream bs;
1092          uint32_t bits;          uint32_t bits;
1093          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1094  #ifdef _DEBUG  
1095    #ifdef _DEBUG_PSNR
1096          float psnr;          float psnr;
1097          uint8_t temp[128];          uint8_t temp[128];
1098  #endif  #endif
# Line 940  Line 1108 
1108    
1109          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1110          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1111    #ifdef BFRAMES
1112            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1113            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1114    #endif
1115          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1116    
1117          start_timer();          start_timer();
1118          if (image_input(&pEnc->current->image,          if (image_input
1119                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1120                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
1121                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1122          stop_conv_timer();          stop_conv_timer();
1123    
1124  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1125          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1126                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
1127  #endif  #endif
1128    
1129          EMMS();          emms();
1130    
1131          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1132    
1133          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1134                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1135          }          } else {
         else  
         {  
1136                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1137          }          }
1138    
1139          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1140          {                  int *temp_dquants =
1141                  int * temp_dquants = (int *)                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1142                          xvid_malloc(pEnc->mbParam.mb_width * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                     pEnc->mbParam.mb_height * \  
                                     sizeof(int),  
1143                                      CACHE_LINE);                                      CACHE_LINE);
1144    
1145                  pEnc->current->quant =                  pEnc->current->quant =
1146                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
1147                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1148                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
1149                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
1150                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
1151                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
1152    
1153                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
1154    
1155                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1156    
1157                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
1158    
1159    
1160                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1161                                          &pEnc->current->mbs[OFFSET(x,y)];  
1162                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
1163                          }                          }
1164    
1165                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 1168 
1168                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1169          }          }
1170    
1171          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
1172                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1173                          write_vol_header = 1;                          write_vol_header = 1;
1174                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1175          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
1176                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
1177    
1178                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 1184 
1184    
1185                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1186                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1187                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
1188                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1189                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1190                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
1191                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1192                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1193                  }                  }
# Line 1044  Line 1195 
1195                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
1196          }          }
1197    
1198          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1199          {                  if ((pEnc->iFrameNum == 0)
1200                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
1201                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
1202                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1203                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
1204                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1205                          pFrame->intra = FrameCodeI(pEnc,                  }
1206                                                     &bs,          } else {
1207                                                     &bits);                  if (pFrame->intra == 1) {
1208                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1209                  else                  } else {
1210                  {                          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);  
1211                  }                  }
1212    
1213          }          }
# Line 1088  Line 1217 
1217          BitstreamPad(&bs);          BitstreamPad(&bs);
1218          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1219    
1220          if (pResult)          if (pResult) {
         {  
1221                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1222                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1223                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 1225 
1225                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1226          }          }
1227    
1228          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1229    
1230  #ifdef _DEBUG          if (pFrame->quant == 0) {
1231          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1232                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
1233                            pEnc->mbParam.edged_width,          }
1234                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
1235            psnr =
1236                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1237                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1238                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1239    
1240          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1241          DEBUG(temp);          DEBUG(temp);
1242  #endif  #endif
1243    
1244    #ifdef BFRAMES
1245            inc_frame_num(pEnc);
1246    #else
1247          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1248    #endif
1249    
1250    
1251          stop_global_timer();          stop_global_timer();
1252          write_timer();          write_timer();
1253    
1254          return XVID_ERR_OK;          return XVID_ERR_OK;
1255  }  }
 #endif  
1256    
1257    
1258  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1259    CodeIntraMB(Encoder * pEnc,
1260                            MACROBLOCK * pMB)
1261    {
1262    
1263          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1264    
# Line 1139  Line 1269 
1269          pMB->sad16 = 0;          pMB->sad16 = 0;
1270    
1271          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1272                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1273                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1274                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1275    
1276                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1277                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1278                            if (pEnc->current->quant < 1)
1279                                    pEnc->current->quant = 1;
1280                  }                  }
1281          }          }
1282    
# Line 1156  Line 1287 
1287  #define FCODEBITS       3  #define FCODEBITS       3
1288  #define MODEBITS        5  #define MODEBITS        5
1289    
1290  void HintedMESet(Encoder * pEnc, int * intra)  void
1291    HintedMESet(Encoder * pEnc,
1292                            int *intra)
1293  {  {
1294          HINTINFO * hint;          HINTINFO * hint;
1295          Bitstream bs;          Bitstream bs;
# Line 1165  Line 1298 
1298    
1299          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1300    
1301          if (hint->rawhints)          if (hint->rawhints) {
         {  
1302                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1303          }          } else {
         else  
         {  
1304                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1305                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1306          }          }
1307    
1308          if (*intra)          if (*intra) {
         {  
1309                  return;                  return;
1310          }          }
1311    
1312          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1313                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1314                                                                                                                                     FCODEBITS);
1315    
1316          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1317          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1318    
1319          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1320          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1321                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1322                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1323                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1324                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1325                          VECTOR pred[4];                          VECTOR pred;
1326                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1327                          int vec;                          int vec;
1328    
1329                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1330                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1331                                                                                                                                      MODEBITS);
1332    
1333                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1334                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1335    
1336                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1337                          {                                  tmp.x =
1338                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1339                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1340                                    tmp.y =
1341                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1342                                                                                                                                                      length);
1343                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1344                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1345    
1346                                  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);
1347    
1348                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1349                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1350                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1351                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1352                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1353                                  }                                  }
1354                          }                          } else if (pMB->mode == MODE_INTER4V) {
1355                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1356                          {                                          tmp.x =
1357                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1358                                  {                                                  x : BitstreamGetBits(&bs, length);
1359                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1360                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1361                                                    y : BitstreamGetBits(&bs, length);
1362                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1363                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1364    
1365                                          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);
1366    
1367                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1368                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1369                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1370                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
                                 }  
1371                          }                          }
1372                          else    // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1373                                  {                                  {
1374                                    for (vec = 0; vec < 4; ++vec) {
1375                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1376                                  }                                  }
1377                          }                          }
1378    
1379                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1380                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1381                          {                                  && pMB->dquant != NO_CHANGE) {
1382                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1383    
1384                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1385                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1386                                  }                                  }
1387                          }                          }
# Line 1258  Line 1390 
1390  }  }
1391    
1392    
1393  void HintedMEGet(Encoder * pEnc, int intra)  void
1394    HintedMEGet(Encoder * pEnc,
1395                            int intra)
1396  {  {
1397          HINTINFO * hint;          HINTINFO * hint;
1398          Bitstream bs;          Bitstream bs;
# Line 1267  Line 1401 
1401    
1402          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1403    
1404          if (hint->rawhints)          if (hint->rawhints) {
         {  
1405                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1406          }          } else {
         else  
         {  
1407                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1408                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1409          }          }
1410    
1411          if (intra)          if (intra) {
1412          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1413                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1414                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1415                  }                  }
# Line 1290  Line 1419 
1419          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1420          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1421    
1422          if (hint->rawhints)          if (hint->rawhints) {
         {  
1423                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1424          }          } else {
         else  
         {  
1425                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1426          }          }
1427    
1428          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1429          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1430                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1431                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1432                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1433                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1434                          VECTOR tmp;                          VECTOR tmp;
1435    
1436                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1437                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1438                          }                          } else {
                         else  
                         {  
1439                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1440                          }                          }
1441    
1442                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1443                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1444                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1445                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1446                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1447    
1448                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1449                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1450                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1451                                  }                                  } else {
                                 else  
                                 {  
1452                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1453                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1454                                  }                                  }
1455                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1456                                  int vec;                                  int vec;
1457    
1458                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1459                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1460                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1461                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1462                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1463    
1464                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1465                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1466                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1467                                          }                                          } else {
                                         else  
                                         {  
1468                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1469                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1470                                          }                                          }
# Line 1360  Line 1473 
1473                  }                  }
1474          }          }
1475    
1476          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1477                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1478                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1479          }          }
1480  }  }
1481    
1482    
1483  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1484    FrameCodeI(Encoder * pEnc,
1485                       Bitstream * bs,
1486                       uint32_t * pBits)
1487  {  {
1488    
1489          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 1497 
1497          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1498    
1499          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1500          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1501    #define DIVX501B481P "DivX501b481p"
1502            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1503                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1504            }
1505    #endif
1506            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1507    
1508          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1509    
# Line 1391  Line 1512 
1512          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1513    
1514          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1515                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1516                  {                          MACROBLOCK *pMB =
1517                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1518    
1519                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1520    
1521                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1522                                                              dct_codes, qcoeff);
1523    
1524                          start_timer();                          start_timer();
1525                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
# Line 1416  Line 1538 
1538          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1539          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1540    
1541          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1542                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1543          }          }
1544    
# Line 1427  Line 1548 
1548    
1549  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1550    
1551  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1552    FrameCodeP(Encoder * pEnc,
1553                       Bitstream * bs,
1554                       uint32_t * pBits,
1555                       bool force_inter,
1556                       bool vol_header)
1557  {  {
1558          float fSigma;          float fSigma;
1559    
# Line 1443  Line 1569 
1569          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1570    
1571          start_timer();          start_timer();
1572          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1573                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
1574                         pEnc->current->global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
1575          stop_edges_timer();          stop_edges_timer();
1576    
# Line 1456  Line 1579 
1579          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1580    
1581          if (!force_inter)          if (!force_inter)
1582                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1583                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1584                                       INTRA_THRESHOLD);
1585          else          else
1586                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1587    
1588          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1589                  start_timer();                  start_timer();
1590                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1591                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1592                                                      pEnc->mbParam.edged_height,
1593                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1594                  stop_inter_timer();                  stop_inter_timer();
1595          }          }
1596    
1597          start_timer();          start_timer();
1598          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1599                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1600          }          } else {
1601    
1602    #ifdef _SMP
1603            if (pEnc->mbParam.num_threads > 1)
1604                    bIntra =
1605                            SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1606                                                     &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1607                                                     iLimit);
1608          else          else
1609          {  #endif
1610                  bIntra = MotionEstimation(                  bIntra =
1611                          &pEnc->mbParam,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1612                          pEnc->current,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1613                          iLimit);                          iLimit);
1614    
1615          }          }
1616          stop_motion_timer();          stop_motion_timer();
1617    
1618          if (bIntra == 1)          if (bIntra == 1) {
         {  
1619                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1620          }          }
1621    
# Line 1496  Line 1624 
1624          if(vol_header)          if(vol_header)
1625                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1626    
1627          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1628    
1629          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1630    
# Line 1505  Line 1633 
1633          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1634          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1635    
1636          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1637          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1638                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1639                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1640    
1641                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1642    
1643                          if (!bIntra)                          if (!bIntra) {
                         {  
1644                                  start_timer();                                  start_timer();
1645                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1646                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1647                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1648                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1649                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1650                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1651                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1534  Line 1655 
1655                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1656                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1657                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1658                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1659                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1660                                                    else if (pEnc->current->quant < 1)
1661                                                            pEnc->current->quant = 1;
1662                                          }                                          }
1663                                  }                                  }
1664                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1665    
1666                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1667    
1668                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
1669                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1670                          else                                                                            dct_codes, qcoeff);
1671                          {                          } else {
1672                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1673                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1674                                                                      dct_codes, qcoeff);
1675                          }                          }
1676    
1677                          start_timer();                          start_timer();
1678                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1679                          stop_prediction_timer();                          stop_prediction_timer();
1680    
1681                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1682                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1683                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1684                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1685                                   pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
                                  pMB->mvs[1].x || pMB->mvs[1].y ||  
                                  pMB->mvs[2].x || pMB->mvs[2].y ||  
                                  pMB->mvs[3].x || pMB->mvs[3].y)  
                         {  
1686                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1687                          }                          } else {
                         else  
                         {  
1688                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1689                          }                          }
1690    
# Line 1579  Line 1696 
1696    
1697          emms();          emms();
1698    
1699          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1700                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1701          }          }
1702    
# Line 1596  Line 1712 
1712          {          {
1713                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1714                  iSearchRange *= 2;                  iSearchRange *= 2;
1715          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1716                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1717                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1718                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1608  Line 1723 
1723    
1724          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1725    
1726    #ifdef BFRAMES
1727            /* frame drop code */
1728            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1729            if (pEnc->sStat.kblks + pEnc->sStat.mblks <=
1730                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1731            {
1732                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1733                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1734    
1735                    BitstreamReset(bs);
1736                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1737    
1738                    // copy reference frame details into the current frame
1739                    pEnc->current->quant = pEnc->reference->quant;
1740                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1741                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1742                    pEnc->current->fcode = pEnc->reference->fcode;
1743                    pEnc->current->bcode = pEnc->reference->bcode;
1744                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1745                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1746    
1747            }
1748    #endif
1749    
1750          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1751    
1752    #ifdef BFRAMES
1753            pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase;
1754    
1755            pEnc->last_pframe = pEnc->mbParam.m_ticks;
1756    #endif
1757          return 0;                                        // inter          return 0;                                        // inter
1758  }  }
1759    
1760    
1761  #ifdef BFRAMES  #ifdef BFRAMES
1762  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void
1763    FrameCodeB(Encoder * pEnc,
1764                       FRAMEINFO * frame,
1765                       Bitstream * bs,
1766                       uint32_t * pBits)
1767  {  {
1768      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1769      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
# Line 1626  Line 1774 
1774      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1775          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1776    
1777    #ifdef BFRAMES_DEC_DEBUG
1778            FILE *fp;
1779            static char first=0;
1780    #define BFRAME_DEBUG    if (!first && fp){ \
1781                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1782            }
1783    
1784            if (!first){
1785                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1786            }
1787    #endif
1788    
1789          // forward          // forward
1790          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,
1791                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1792                                       pEnc->mbParam.height,
1793                                       frame->global_flags & XVID_INTERLACING);
1794          start_timer();          start_timer();
1795          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,
1796                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1797                                              0);
1798          stop_inter_timer();          stop_inter_timer();
1799    
1800          // backward          // backward
1801          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,
1802                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1803                                       pEnc->mbParam.height,
1804                                       frame->global_flags & XVID_INTERLACING);
1805      start_timer();      start_timer();
1806          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1807                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1808                                              0);
1809          stop_inter_timer();          stop_inter_timer();
1810    
1811          start_timer();          start_timer();
1812          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1813                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,            ((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase,
1814                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                  pEnc->time_pp,
1815                                                    pEnc->reference->mbs, f_ref,
1816                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1817                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1818                                                     &pEnc->vInterV, &pEnc->vInterHV);
1819    
1820    
1821          stop_motion_timer();          stop_motion_timer();
# Line 1654  Line 1826 
1826          }*/          }*/
1827    
1828      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1829      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1830    
1831      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1832    
# Line 1664  Line 1836 
1836          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1837    
1838    
1839      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
         {  
1840                  // reset prediction                  // reset prediction
1841    
1842                  forward.x = 0;                  forward.x = 0;
# Line 1673  Line 1844 
1844                  backward.x = 0;                  backward.x = 0;
1845                  backward.y = 0;                  backward.y = 0;
1846    
1847                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1848                  {                          MACROBLOCK *f_mb =
1849                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1850                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *b_mb =
1851                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1852                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1853    
1854                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1855                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
                         {  
1856                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1857                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1858    
1859                                    mb->cbp = 0;
1860    #ifdef BFRAMES_DEC_DEBUG
1861            BFRAME_DEBUG
1862    #endif
1863                                  continue;                                  continue;
1864                          }                          }
1865    
1866                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1867                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1868                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1869                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1870                                          dct_codes);                                          dct_codes);
1871    
1872                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1873                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);                          mb->cbp =
1874                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1875                                                                      qcoeff);
1876                          //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);
1877    
1878    
1879                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1880                                  mb->cbp == 0 &&                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
1881                                  mb->mvs[0].x == 0 &&                                  mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
1882                          }                          }
1883    
1884                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
                         {  
1885                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1886                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1887                                  forward.x = mb->mvs[0].x;                                  forward.x = mb->mvs[0].x;
1888                                  forward.y = mb->mvs[0].y;                                  forward.y = mb->mvs[0].y;
1889                          }                          }
1890    
1891                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
                         {  
1892                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1893                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1894                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1895                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1896                          }                          }
1897    //                      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);
1898    
1899  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  #ifdef BFRAMES_DEC_DEBUG
1900            BFRAME_DEBUG
1901    #endif
1902                          start_timer();                          start_timer();
1903                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1904                                                     &pEnc->sStat);
1905                          stop_coding_timer();                          stop_coding_timer();
1906                  }                  }
1907          }          }
# Line 1734  Line 1911 
1911          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1912    
1913          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1914    
1915    #ifdef BFRAMES_DEC_DEBUG
1916            if (!first){
1917                    first=1;
1918                    if (fp)
1919                            fclose(fp);
1920            }
1921    #endif
1922  }  }
1923  #endif  #endif

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

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