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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.71

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