[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.48, Mon Jun 24 09:53:17 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     *      20.06.2002 bframe patch
36   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
37   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
38   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
39   *   *
40   *  $Id$   *  $Id$
41   *   *
42   ***************************************************************************/   ****************************************************************************/
43    
44  #include <stdlib.h>  #include <stdlib.h>
45  #include <stdio.h>  #include <stdio.h>
46  #include <math.h>  #include <math.h>
47    #include <string.h>
48    
49  #include "encoder.h"  #include "encoder.h"
50  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
51  #include "global.h"  #include "global.h"
52  #include "utils/timer.h"  #include "utils/timer.h"
53  #include "image/image.h"  #include "image/image.h"
54    #ifdef BFRAMES
55    #include "image/font.h"
56    #endif
57  #include "motion/motion.h"  #include "motion/motion.h"
58  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
59  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 61  Line 66 
66  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
67  #include "utils/mem_align.h"  #include "utils/mem_align.h"
68    
69    /*****************************************************************************
70     * Local macros
71     ****************************************************************************/
72    
73  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
74  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
75    
76    /*****************************************************************************
77     * Local function prototypes
78     ****************************************************************************/
79    
80  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
81                        Bitstream * bs,                        Bitstream * bs,
82                        uint32_t *pBits);                        uint32_t *pBits);
# Line 81  Line 94 
94                         uint32_t *pBits);                         uint32_t *pBits);
95  #endif  #endif
96    
97  static int DQtab[4] =  /*****************************************************************************
98  {   * Local data
99     ****************************************************************************/
100    
101    static int DQtab[4] = {
102          -1, -2, 1, 2          -1, -2, 1, 2
103  };  };
104    
105  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
106          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
107  };  };
108    
109    
110  void static image_null(IMAGE * image)  static void __inline
111    image_null(IMAGE * image)
112  {  {
113          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
114  }  }
115    
116    
117  int encoder_create(XVID_ENC_PARAM * pParam)  /*****************************************************************************
118     * Encoder creation
119     *
120     * This function creates an Encoder instance, it allocates all necessary
121     * image buffers (reference, current and bframes) and initialize the internal
122     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
123     *
124     * The code seems to be very long but is very basic, mainly memory allocation
125     * and cleaning code.
126     *
127     * Returned values :
128     *    - XVID_ERR_OK     - no errors
129     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
130     *                        cleans the structure before exiting.
131     *                        pParam->handle is also set to NULL.
132     *
133     ****************************************************************************/
134    
135    int
136    encoder_create(XVID_ENC_PARAM * pParam)
137  {  {
138          Encoder *pEnc;          Encoder *pEnc;
139          uint32_t i;          int i;
140    
141          pParam->handle = NULL;          pParam->handle = NULL;
142    
# Line 114  Line 149 
149    
150          /* Fps */          /* Fps */
151    
152          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
153                  pParam->fincr = 1;                  pParam->fincr = 1;
154                  pParam->fbase = 25;                  pParam->fbase = 25;
155          }          }
# Line 126  Line 160 
160           */           */
161    
162          i = pParam->fincr;          i = pParam->fincr;
163          while (i > 1)          while (i > 1) {
164          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
165                          pParam->fincr /= i;                          pParam->fincr /= i;
166                          pParam->fbase /= i;                          pParam->fbase /= i;
167                          i = pParam->fincr;                          i = pParam->fincr;
# Line 138  Line 170 
170                  i--;                  i--;
171          }          }
172    
173          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
174                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
175    
176                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
177                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
178          }          }
# Line 180  Line 212 
212          if (pEnc == NULL)          if (pEnc == NULL)
213                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
214    
215            /* Zero the Encoder Structure */
216    
217            memset(pEnc, 0, sizeof(Encoder));
218    
219          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
220    
221          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 194  Line 230 
230          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
231          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
232    
233            pEnc->mbParam.m_quant_type = H263_QUANT;
234    
235          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
236    
237          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 213  Line 251 
251    
252          /* try to allocate mb memory */          /* try to allocate mb memory */
253    
254          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
255                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
256                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
257                                           CACHE_LINE);          pEnc->reference->mbs =
258          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
259                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
260    
261          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
262                  goto xvid_err_memory2;                  goto xvid_err_memory2;
263    
264          /* try to allocate image memory */          /* try to allocate image memory */
265    
266  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
267          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
268  #endif  #endif
269  #ifdef BFRAMES  #ifdef BFRAMES
# Line 243  Line 279 
279          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
280          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
281    
282  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
283          if (image_create(&pEnc->sOriginal,          if (image_create
284                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
285                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
286                  goto xvid_err_memory3;                  goto xvid_err_memory3;
287  #endif  #endif
288  #ifdef BFRAMES  #ifdef BFRAMES
289          if (image_create(&pEnc->f_refh,          if (image_create
290                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
291                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
292                  goto xvid_err_memory3;                  goto xvid_err_memory3;
293          if (image_create(&pEnc->f_refv,          if (image_create
294                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, 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          if (image_create(&pEnc->f_refhv,          if (image_create
298                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
299                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
300                  goto xvid_err_memory3;                  goto xvid_err_memory3;
301  #endif  #endif
302          if (image_create(&pEnc->current->image,          if (image_create
303                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
304                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
305                  goto xvid_err_memory3;                  goto xvid_err_memory3;
306          if (image_create(&pEnc->reference->image,          if (image_create
307                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
308                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
309                  goto xvid_err_memory3;                  goto xvid_err_memory3;
310          if (image_create(&pEnc->vInterH,          if (image_create
311                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
312                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
313                  goto xvid_err_memory3;                  goto xvid_err_memory3;
314          if (image_create(&pEnc->vInterV,          if (image_create
315                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
316                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
317                  goto xvid_err_memory3;                  goto xvid_err_memory3;
318          if (image_create(&pEnc->vInterVf,          if (image_create
319                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
320                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
321                  goto xvid_err_memory3;                  goto xvid_err_memory3;
322          if (image_create(&pEnc->vInterHV,          if (image_create
323                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
324                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
325                  goto xvid_err_memory3;                  goto xvid_err_memory3;
326          if (image_create(&pEnc->vInterHVf,          if (image_create
327                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
328                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
329                  goto xvid_err_memory3;                  goto xvid_err_memory3;
330    
# Line 297  Line 333 
333          /* B Frames specific init */          /* B Frames specific init */
334  #ifdef BFRAMES  #ifdef BFRAMES
335    
336            pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339          pEnc->bframes = NULL;          pEnc->bframes = NULL;
340    
341          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
342                  int n;                  int n;
343    
344                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
345                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
346                                              CACHE_LINE);                                              CACHE_LINE);
347    
348                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 316  Line 352 
352                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
353    
354    
355                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
356                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
357    
358                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
359                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
360    
361                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
362                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
363                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
364    
365                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
366                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
367    
368                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
369    
370                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
371                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
372                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
373                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
374    
# Line 345  Line 378 
378          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
379          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
380          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
381            pEnc->bframenum_dx50bvop = -1;
382    
383            pEnc->queue = NULL;
384    
385    
386            if (pEnc->mbParam.max_bframes > 0) {
387                    int n;
388    
389                    pEnc->queue =
390                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
391                                                    CACHE_LINE);
392    
393                    if (pEnc->queue == NULL)
394                            goto xvid_err_memory4;
395    
396                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
397                            image_null(&pEnc->queue[n]);
398    
399                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
400                            if (image_create
401                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
402                                     pEnc->mbParam.edged_height) < 0)
403                                    goto xvid_err_memory5;
404    
405                    }
406            }
407    
408            pEnc->queue_head = 0;
409            pEnc->queue_tail = 0;
410            pEnc->queue_size = 0;
411    
412    
413          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
414          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
415            pEnc->m_framenum = 0;
416  #endif  #endif
417    
418          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
419    
420          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
421          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
422                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
423                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
424                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
425                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
426          }          }
427    
428          init_timer();          init_timer();
# Line 370  Line 431 
431    
432          /*          /*
433           * 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  
434           */           */
435  #ifdef BFRAMES  #ifdef BFRAMES
436      xvid_err_memory5:
437    
438    
439            if (pEnc->mbParam.max_bframes > 0) {
440    
441                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
442                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
443                                                      pEnc->mbParam.edged_height);
444                    }
445                    xvid_free(pEnc->queue);
446            }
447    
448   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
449    
450                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
451    
452                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
453                                pEnc->mbParam.edged_width,  
454                            if (pEnc->bframes[i] == NULL)
455                                    continue;
456    
457                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
458                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
459    
460                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 392  Line 464 
464          }          }
465    
466          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
467            }
468    
469  #endif  #endif
470    
471   xvid_err_memory3:   xvid_err_memory3:
472  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
473          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
474                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
475  #endif  #endif
476    
477  #ifdef BFRAMES  #ifdef BFRAMES
478          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
479                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
480          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
481                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
482          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
483                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
484  #endif  #endif
485    
486          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
487                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
488          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
489                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
490          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
491                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
492          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
493                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
494          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
495                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
496          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
497                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
498          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
499                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
500    
501   xvid_err_memory2:   xvid_err_memory2:
# Line 445  Line 507 
507          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
508          xvid_free(pEnc);          xvid_free(pEnc);
509    
510            pParam->handle = NULL;
511    
512          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
513  }  }
514    
515  int encoder_destroy(Encoder * pEnc)  /*****************************************************************************
516     * Encoder destruction
517     *
518     * This function destroy the entire encoder structure created by a previous
519     * successful encoder_create call.
520     *
521     * Returned values (for now only one returned value) :
522     *    - XVID_ERR_OK     - no errors
523     *
524     ****************************************************************************/
525    
526    int
527    encoder_destroy(Encoder * pEnc)
528  {  {
529    #ifdef BFRAMES
530            int i;
531    #endif
532    
533          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
534    
535          /* B Frames specific */          /* B Frames specific */
536  #ifdef BFRAMES  #ifdef BFRAMES
537          int i;          if (pEnc->mbParam.max_bframes > 0) {
538    
539          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
         {  
540    
541                  if (pEnc->bframes[i] == NULL) continue;                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
542                                              pEnc->mbParam.edged_height);
543                    }
544                    xvid_free(pEnc->queue);
545            }
546    
547                  image_destroy(&pEnc->bframes[i]->image,  
548                                pEnc->mbParam.edged_width,          if (pEnc->mbParam.max_bframes > 0) {
549    
550                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
551    
552                            if (pEnc->bframes[i] == NULL)
553                                    continue;
554    
555                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
556                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
557    
558                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
559    
560                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
561          }          }
562    
563          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
564    
565            }
566  #endif  #endif
567    
568          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
569    
570          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
571                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
572          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
573                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
574          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
575                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
577                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
578          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
579                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
580          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
581                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
582          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
583                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
584  #ifdef BFRAMES  #ifdef BFRAMES
585          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
586                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
587          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
588                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
589          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
590                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
591  #endif  #endif
592  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
593          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
594                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
595  #endif  #endif
596    
# Line 529  Line 608 
608  }  }
609    
610    
611    #ifdef BFRAMES
612    void inc_frame_num(Encoder * pEnc)
613    {
614            pEnc->iFrameNum++;
615            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
616    
617            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
618            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
619    }
620    #endif
621    
622    
 // ==================================================================  
623  #ifdef BFRAMES  #ifdef BFRAMES
624  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
625    {
626            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
627            {
628                    DPRINTF("FATAL: QUEUE FULL");
629                    return;
630            }
631    
632            DPRINTF("*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
633                                    pEnc->bframenum_head, pEnc->bframenum_tail,
634                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
635    
636    
637            start_timer();
638            if (image_input
639                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
640                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
641                    return;
642            stop_conv_timer();
643    
644            pEnc->queue_size++;
645            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
646    }
647    #endif
648    
649    
650    #ifdef BFRAMES
651    /*****************************************************************************
652     * IPB frame encoder entry point
653     *
654     * Returned values :
655     *    - XVID_ERR_OK     - no errors
656     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
657     *                        format
658     ****************************************************************************/
659    
660    int
661    encoder_encode_bframes(Encoder * pEnc,
662                               XVID_ENC_FRAME * pFrame,
663                               XVID_ENC_STATS * pResult)
664  {  {
665          uint16_t x, y;          uint16_t x, y;
666          Bitstream bs;          Bitstream bs;
667          uint32_t bits;          uint32_t bits;
668    
669  #ifdef _DEBUG          int input_valid = 1;
670    
671    #ifdef _DEBUG_PSNR
672          float psnr;          float psnr;
673          char temp[128];          char temp[128];
674  #endif  #endif
675    
676          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
677          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
678            ENC_CHECK(pFrame->image);
679    
680          start_global_timer();          start_global_timer();
681    
682          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
683    
684  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ipvop_loop:
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
685    
686          if ((pFrame->image == NULL || pEnc->flush_bframes) &&          /*
687                  pEnc->bframenum_head < pEnc->bframenum_tail)           * bframe "flush" code
688          {           */
689    
690            if ((pFrame->image == NULL || pEnc->flush_bframes)
691                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
692    
693                    if (pEnc->flush_bframes == 0) {
694                            /*
695                             * we have reached the end of stream without getting
696                             * a future reference frame... so encode last final
697                             * frame as a pframe
698                             */
699    
700                            DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
701                                    pEnc->bframenum_head, pEnc->bframenum_tail,
702                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
703    
                 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) --- ");  
704                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
705                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
706    
# Line 572  Line 710 
710    
711                          BitstreamPad(&bs);                          BitstreamPad(&bs);
712                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
713                          pFrame->intra = 0;                          pFrame->intra = 0;
714    
715                          return XVID_ERR_OK;                          return XVID_ERR_OK;
716                  }                  }
717    
                 //dprintf("--- BFRAME (flush) --- ");  
                 FrameCodeB(pEnc,  
                         pEnc->bframes[pEnc->bframenum_head],  
                          &bs, &bits);  
                 pEnc->bframenum_head++;  
718    
719                    DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
720                                    pEnc->bframenum_head, pEnc->bframenum_tail,
721                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
722    
723                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
724                    pEnc->bframenum_head++;
725    
726                  BitstreamPad(&bs);                  BitstreamPad(&bs);
727                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
728                  pFrame->intra = 0;                  pFrame->intra = 0;
729    
730                    if (input_valid)
731                            queue_image(pEnc, pFrame);
732    
733                  return XVID_ERR_OK;                  return XVID_ERR_OK;
734          }          }
735    
736          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
737          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
738                  pFrame->length = 0;  
739                  pFrame->input_consumed = 1;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
740    
741                            DPRINTF("*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
742                                    pEnc->bframenum_head, pEnc->bframenum_tail,
743                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
744    
745    
746                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
747                            BitstreamPad(&bs);
748                            BitstreamPutBits(&bs, 0x7f, 8);
749    
750                            pFrame->length = BitstreamLength(&bs);
751                  pFrame->intra = 0;                  pFrame->intra = 0;
752    
753                            if (input_valid)
754                                    queue_image(pEnc, pFrame);
755    
756                  return XVID_ERR_OK;                  return XVID_ERR_OK;
757          }          }
758            }
759    
760    
761          if (pEnc->bframenum_head > 0)  bvop_loop:
762    
763            if (pEnc->bframenum_dx50bvop != -1)
764          {          {
765                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;  
766                    SWAP(pEnc->current, pEnc->reference);
767                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
768    
769                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
770                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
771          }          }
772    
773          pEnc->flush_bframes = 0;                  if (input_valid)
774                    {
775                            queue_image(pEnc, pFrame);
776                            input_valid = 0;
777                    }
778    
779  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (input_valid) {
780    
781          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
782    
783          pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant;                  start_timer();
784                    if (image_input
785                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
786                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
787                            return XVID_ERR_FORMAT;
788                    stop_conv_timer();
789    
790                    // queue input frame, and dequue next image
791                    if (pEnc->queue_size > 0)
792                    {
793                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
794                            if (pEnc->queue_head != pEnc->queue_tail)
795                            {
796                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
797                            }
798                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
799                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
800                    }
801    
802            } else if (pEnc->queue_size > 0) {
803    
804                    SWAP(pEnc->current, pEnc->reference);
805    
806                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
807                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
808                    pEnc->queue_size--;
809    
810            } else if (BitstreamPos(&bs) == 0) {
811    
812                    DPRINTF("*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
813                                    pEnc->bframenum_head, pEnc->bframenum_tail,
814                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
815    
816    
817                    pFrame->intra = 0;
818    
819                    BitstreamPutBits(&bs, 0x7f, 8);
820                    BitstreamPad(&bs);
821                    pFrame->length = BitstreamLength(&bs);
822    
823                    return XVID_ERR_OK;
824    
825            } else {
826    
827                    pFrame->length = BitstreamLength(&bs);
828                    return XVID_ERR_OK;
829            }
830    
831            pEnc->flush_bframes = 0;
832    
833            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
834             * Well there was a separation here so i put it in ANSI C
835             * comment style :-)
836             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
837    
838            emms();
839    
840            // only inc frame num, adapt quant, etc. if we havent seen it before
841            if (pEnc->bframenum_dx50bvop < 0 )
842            {
843                    if (pFrame->quant == 0)
844                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
845                    else
846                            pEnc->current->quant = pFrame->quant;
847    
848          if(pEnc->current->quant < 1)          if(pEnc->current->quant < 1)
849                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
# Line 620  Line 853 
853    
854          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
855          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
856          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
857          pEnc->current->ticks = pEnc->mbParam.m_ticks;                  /* ToDo : dynamic fcode (in both directions) */
         //@@@ TODO: dyanmic fcode (in both directions)  
858          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
859          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
860    
861          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
862          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
863                          pFrame->image, pFrame->colorspace))  
864          {                  inc_frame_num(pEnc);
                 return XVID_ERR_FORMAT;  
         }  
         stop_conv_timer();  
865    
866  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
867          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->sOriginal, &pEnc->current->image,
868                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
869  #endif  #endif
870    
871                    emms();
872    
873                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
874                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
875                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
876                    }
877    
878  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879  // lumi masking           * Luminance masking
880  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
881    
882          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
883          {                          int *temp_dquants =
884                  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 *
885                                                                    pEnc->mbParam.mb_height * sizeof(int),
886                                                                    CACHE_LINE);
887    
888                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                          pEnc->current->quant =
889                                                              pEnc->mbParam.edged_width,  // stride                                  adaptive_quantization(pEnc->current->image.y,
890                                                              temp_dquants,                                                                    pEnc->mbParam.edged_width, temp_dquants,
891                                                              pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
892                                                              pEnc->current->quant,       // min_quant                                                                    2 * pEnc->current->quant,
                                                             2*pEnc->current->quant,     // max_quant  
893                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
894                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
895    
896                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
897                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
898                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
899                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
900                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
901                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
902    
903                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
904                          }                          }
905    
906    #undef OFFSET
907                            }
908    
909                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
910          }          }
911    
912            }
913    
914  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915  // ivop/pvop/bvop selection           * ivop/pvop/bvop selection
916  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
   
         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 ---");  
917    
                 FrameCodeI(pEnc, &bs, &bits);  
918    
919            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop != -1 ||
920                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
921                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
922                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
923                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
924                                             pEnc->mbParam.height) > 30) {
925                    /*
926                     * This will be coded as an Intra Frame
927                     */
928    
929                    DPRINTF("*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
930                                    pEnc->bframenum_head, pEnc->bframenum_tail,
931                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
932    
933                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
934                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
935                    }
936    
937                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
938    
939                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
940    
941                            pEnc->bframenum_tail--;
942                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
943    
944                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
945                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
946                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
947                            }
948                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
949    
950                            pFrame->intra = 0;
951    
952                    } else {
953                            pEnc->bframenum_dx50bvop = -1;
954                            FrameCodeI(pEnc, &bs, &bits);
955                  pFrame->intra = 1;                  pFrame->intra = 1;
956                    }
957    
958                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
959    
960                  /* note: sequences like "IIBB" decode fine with msfdam but,                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
961                     go screwy with divx5.00 */                          BitstreamPad(&bs);
962                            input_valid = 0;
963                            goto ipvop_loop;
964                    }
965    
966                    /*
967                     * NB : sequences like "IIBB" decode fine with msfdam but,
968                     *      go screwy with divx 5.00
969                     */
970            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
971                    /*
972                     * This will be coded as a Predicted Frame
973                     */
974    
975                    DPRINTF("*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
976                                    pEnc->bframenum_head, pEnc->bframenum_tail,
977                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
978    
979                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
980                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
981          }          }
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 //dprintf("--- PFRAME ---");  
982    
983                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
984                  pFrame->intra = 0;                  pFrame->intra = 0;
985                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
986    
987                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
988                            BitstreamPad(&bs);
989                            input_valid = 0;
990                            goto ipvop_loop;
991          }          }
         else  
         {  
                 //dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
992    
993                  if (pFrame->bquant < 1)          } else {
994                  {                  /*
995                          pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;                   * This will be coded as a Bidirectional Frame
996                     */
997    
998                    DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
999                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1000                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1001    
1002                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1003                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1004                  }                  }
1005                  else  
1006                  {                  if (pFrame->bquant < 1) {
1007                            pEnc->current->quant =
1008                                    ((pEnc->reference->quant +
1009                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1010                    } else {
1011                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1012                  }                  }
1013    
1014                  // store frame into bframe buffer & swap ref back to current                  /* store frame into bframe buffer & swap ref back to current */
1015                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1016                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
1017    
# Line 714  Line 1019 
1019    
1020                  pFrame->intra = 0;                  pFrame->intra = 0;
1021                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1022    
1023                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1024                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
                 return XVID_ERR_OK;  
1025          }          }
1026    
1027          BitstreamPad(&bs);          BitstreamPad(&bs);
1028          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1029    
1030          if (pResult)          if (pResult) {
         {  
1031                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1032                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1033                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 737  Line 1035 
1035                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1036          }          }
1037    
1038  #ifdef _DEBUG          emms();
1039          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
1040                                  pEnc->mbParam.width, pEnc->mbParam.height);  #ifdef _DEBUG_PSNR
1041            psnr =
1042                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1043                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1044                                       pEnc->mbParam.height);
1045    
1046          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1047          DEBUG(temp);          DEBUG(temp);
1048  #endif  #endif
1049    
1050          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1051          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1052                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
1053          }          }
1054    
         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;  
1055    
1056          stop_global_timer();          stop_global_timer();
1057          write_timer();          write_timer();
1058    
1059          return XVID_ERR_OK;          return XVID_ERR_OK;
1060  }  }
1061  // ==================================================================  
1062  #else  #endif
1063  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1064    
1065    
1066    /*****************************************************************************
1067     * "original" IP frame encoder entry point
1068     *
1069     * Returned values :
1070     *    - XVID_ERR_OK     - no errors
1071     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1072     *                        format
1073     ****************************************************************************/
1074    
1075    int
1076    encoder_encode(Encoder * pEnc,
1077                               XVID_ENC_FRAME * pFrame,
1078                               XVID_ENC_STATS * pResult)
1079  {  {
1080          uint16_t x, y;          uint16_t x, y;
1081          Bitstream bs;          Bitstream bs;
1082          uint32_t bits;          uint32_t bits;
1083          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1084  #ifdef _DEBUG  
1085    #ifdef _DEBUG_PSNR
1086          float psnr;          float psnr;
1087          uint8_t temp[100];          uint8_t temp[128];
1088  #endif  #endif
1089    
1090          start_global_timer();          start_global_timer();
# Line 791  Line 1098 
1098    
1099          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1100          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1101    #ifdef BFRAMES
1102            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1103            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1104    #endif
1105          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1106    
1107          start_timer();          start_timer();
1108          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
1109                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1110          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1111                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1112          stop_conv_timer();          stop_conv_timer();
1113    
1114  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1115          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1116                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1117  #endif  #endif
1118    
1119          EMMS();          emms();
1120    
1121          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1122    
1123          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1124                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1125          }          } else {
         else  
         {  
1126                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1127          }          }
1128    
1129          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1130          {                  int *temp_dquants =
1131                  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 *
1132                                                                    pEnc->mbParam.mb_height * sizeof(int),
1133                                                                    CACHE_LINE);
1134    
1135                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  pEnc->current->quant =
1136                                                              pEnc->mbParam.edged_width,  // stride                          adaptive_quantization(pEnc->current->image.y,
1137                                                              temp_dquants,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1138                                                              pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
1139                                                              pEnc->current->quant,       // min_quant                                                                    2 * pEnc->current->quant,
                                                             2*pEnc->current->quant,     // max_quant  
1140                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1141                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1142    
1143                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1144                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1145                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1146                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1147                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1148    
1149    
1150                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1151    
1152                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1153                            }
1154    
1155    #undef OFFSET
1156                          }                          }
1157    
1158                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1159          }          }
1160    
# Line 844  Line 1162 
1162                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1163                          write_vol_header = 1;                          write_vol_header = 1;
1164                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1165          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1166          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1167    
1168                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1169    
1170                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1171                          write_vol_header = 1;                          write_vol_header = 1;
# Line 857  Line 1174 
1174    
1175                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1176                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1177                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1178                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1179                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1180                  }                  } else {
1181                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1182                          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());  
1183                  }                  }
1184                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1185                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1186          }          }
1187    
1188          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1189          {                  if ((pEnc->iFrameNum == 0)
1190                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1191                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1192                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1193                  else                  } else {
1194                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1195          }          }
1196          else          } else {
1197          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1198                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1199                  else                  } else {
1200                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1201          }          }
1202    
1203            }
1204    
1205          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1206          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1207          BitstreamPad(&bs);          BitstreamPad(&bs);
1208          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1209    
1210          if (pResult)          if (pResult) {
         {  
1211                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1212                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1213                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 900  Line 1215 
1215                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1216          }          }
1217    
1218          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1219    
1220  #ifdef _DEBUG          if (pFrame->quant == 0) {
1221          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1222                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
1223            }
1224    #ifdef _DEBUG_PSNR
1225            psnr =
1226                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1227                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1228                                       pEnc->mbParam.height);
1229    
1230          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1231          DEBUG(temp);          DEBUG(temp);
1232  #endif  #endif
1233    
1234    #ifdef BFRAMES
1235            inc_frame_num(pEnc);
1236    #else
1237          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1238    #endif
1239    
1240    
1241          stop_global_timer();          stop_global_timer();
1242          write_timer();          write_timer();
1243    
1244          return XVID_ERR_OK;          return XVID_ERR_OK;
1245  }  }
 #endif  
1246    
1247    
1248  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1249    CodeIntraMB(Encoder * pEnc,
1250                            MACROBLOCK * pMB)
1251    {
1252    
1253          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1254    
# Line 939  Line 1259 
1259          pMB->sad16 = 0;          pMB->sad16 = 0;
1260    
1261          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1262                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1263                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1264                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1265    
1266                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1267                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1268                            if (pEnc->current->quant < 1)
1269                                    pEnc->current->quant = 1;
1270                  }                  }
1271          }          }
1272    
# Line 956  Line 1277 
1277  #define FCODEBITS       3  #define FCODEBITS       3
1278  #define MODEBITS        5  #define MODEBITS        5
1279    
1280  void HintedMESet(Encoder * pEnc, int * intra)  void
1281    HintedMESet(Encoder * pEnc,
1282                            int *intra)
1283  {  {
1284          HINTINFO * hint;          HINTINFO * hint;
1285          Bitstream bs;          Bitstream bs;
# Line 965  Line 1288 
1288    
1289          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1290    
1291          if (hint->rawhints)          if (hint->rawhints) {
         {  
1292                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1293          }          } else {
         else  
         {  
1294                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1295                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1296          }          }
1297    
1298          if (*intra)          if (*intra) {
         {  
1299                  return;                  return;
1300          }          }
1301    
1302          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1303                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1304                                                                                                                                     FCODEBITS);
1305    
1306          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1307          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1308    
1309          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1310          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1311                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1312                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1313                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1314                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1315                          VECTOR pred[4];                          VECTOR pred[4];
1316                          VECTOR tmp;                          VECTOR tmp;
1317                          int32_t dummy[4];                          int32_t dummy[4];
1318                          int vec;                          int vec;
1319    
1320                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1321                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1322                                                                                                                                      MODEBITS);
1323    
1324                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1325                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1326    
1327                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1328                          {                                  tmp.x =
1329                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1330                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1331                                    tmp.y =
1332                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1333                                                                                                                                                      length);
1334                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1335                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1336    
1337                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,
1338                                                            0, pred, dummy);
1339    
1340                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1341                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1342                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1343                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;
1344                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
1345                                  }                                  }
1346                          }                          } else if (pMB->mode == MODE_INTER4V) {
1347                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1348                          {                                          tmp.x =
1349                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1350                                  {                                                  x : BitstreamGetBits(&bs, length);
1351                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1352                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1353                                                    y : BitstreamGetBits(&bs, length);
1354                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1355                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1356    
1357                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          get_pmvdata(pEnc->current->mbs, x, y,
1358                                                                    pEnc->mbParam.mb_width, vec, pred, dummy);
1359    
1360                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1361                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1362                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
1363                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
1364                                  }                                  }
1365                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1366                                  {                                  {
1367                                    for (vec = 0; vec < 4; ++vec) {
1368                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1369                                  }                                  }
1370                          }                          }
1371    
1372                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1373                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1374                          {                                  && pMB->dquant != NO_CHANGE) {
1375                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1376    
1377                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1378                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1379                                  }                                  }
1380                          }                          }
# Line 1058  Line 1383 
1383  }  }
1384    
1385    
1386  void HintedMEGet(Encoder * pEnc, int intra)  void
1387    HintedMEGet(Encoder * pEnc,
1388                            int intra)
1389  {  {
1390          HINTINFO * hint;          HINTINFO * hint;
1391          Bitstream bs;          Bitstream bs;
# Line 1067  Line 1394 
1394    
1395          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1396    
1397          if (hint->rawhints)          if (hint->rawhints) {
         {  
1398                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1399          }          } else {
         else  
         {  
1400                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1401                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1402          }          }
1403    
1404          if (intra)          if (intra) {
1405          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1406                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1407                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1408                  }                  }
# Line 1090  Line 1412 
1412          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1413          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1414    
1415          if (hint->rawhints)          if (hint->rawhints) {
         {  
1416                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1417          }          } else {
         else  
         {  
1418                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1419          }          }
1420    
1421          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1422          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1423                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1424                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1425                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1426                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1427                          VECTOR tmp;                          VECTOR tmp;
1428    
1429                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1430                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1431                          }                          } else {
                         else  
                         {  
1432                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1433                          }                          }
1434    
1435                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1436                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1437                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1438                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1439                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1440    
1441                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1442                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1443                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1444                                  }                                  } else {
                                 else  
                                 {  
1445                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1446                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1447                                  }                                  }
1448                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1449                                  int vec;                                  int vec;
1450    
1451                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1452                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1453                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].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[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1459                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].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                                          }                                          }
# Line 1160  Line 1466 
1466                  }                  }
1467          }          }
1468    
1469          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1470                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1471                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1472          }          }
1473  }  }
1474    
1475    
1476  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1477    FrameCodeI(Encoder * pEnc,
1478                       Bitstream * bs,
1479                       uint32_t * pBits)
1480  {  {
1481    
1482          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 1490 
1490          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1491    
1492          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1493          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1494    #define DIVX501B481P "DivX501b481p"
1495            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1496                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1497            }
1498    #endif
1499            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1500    
1501          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1502    
# Line 1191  Line 1505 
1505          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1506    
1507          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1508                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1509                  {                          MACROBLOCK *pMB =
1510                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1511    
1512                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1513    
1514                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1515                                                              dct_codes, qcoeff);
1516    
1517                          start_timer();                          start_timer();
1518                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
# Line 1216  Line 1531 
1531          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1532          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1533    
1534          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1535                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1536          }          }
1537    
# Line 1227  Line 1541 
1541    
1542  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1543    
1544  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1545    FrameCodeP(Encoder * pEnc,
1546                       Bitstream * bs,
1547                       uint32_t * pBits,
1548                       bool force_inter,
1549                       bool vol_header)
1550  {  {
1551          float fSigma;          float fSigma;
1552    
# Line 1243  Line 1562 
1562          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1563    
1564          start_timer();          start_timer();
1565          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1566                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
1567                         pEnc->current->global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
1568          stop_edges_timer();          stop_edges_timer();
1569    
# Line 1256  Line 1572 
1572          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1573    
1574          if (!force_inter)          if (!force_inter)
1575                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1576                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1577                                       INTRA_THRESHOLD);
1578          else          else
1579                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1580    
1581          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1582                  start_timer();                  start_timer();
1583                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1584                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1585                                                      pEnc->mbParam.edged_height,
1586                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1587                  stop_inter_timer();                  stop_inter_timer();
1588          }          }
1589    
1590          start_timer();          start_timer();
1591          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1592                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1593          }          } else {
1594          else                  bIntra =
1595          {                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1596                  bIntra = MotionEstimation(                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->mbParam,  
                         pEnc->current,  
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1597                          iLimit);                          iLimit);
1598          }          }
1599          stop_motion_timer();          stop_motion_timer();
1600    
1601          if (bIntra == 1)          if (bIntra == 1) {
         {  
1602                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1603          }          }
1604    
# Line 1296  Line 1607 
1607          if(vol_header)          if(vol_header)
1608                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1609    
1610          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1611    
1612          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1613    
# Line 1305  Line 1616 
1616          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1617          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1618    
1619          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1620          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1621                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1622                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1623    
1624                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1625    
1626                          if (!bIntra)                          if (!bIntra) {
                         {  
1627                                  start_timer();                                  start_timer();
1628                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1629                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1630                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1631                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1632                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1633                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1634                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1334  Line 1638 
1638                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1639                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1640                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1641                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1642                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1643                                                    else if (pEnc->current->quant < 1)
1644                                                            pEnc->current->quant = 1;
1645                                          }                                          }
1646                                  }                                  }
1647                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1648    
1649                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1650    
1651                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
1652                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1653                          else                                                                            dct_codes, qcoeff);
1654                          {                          } else {
1655                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1656                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1657                                                                      dct_codes, qcoeff);
1658                          }                          }
1659    
1660                          start_timer();                          start_timer();
1661                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1662                          stop_prediction_timer();                          stop_prediction_timer();
1663    
1664                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1665                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1666                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1667                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1668                                   pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
                                  pMB->mvs[1].x || pMB->mvs[1].y ||  
                                  pMB->mvs[2].x || pMB->mvs[2].y ||  
                                  pMB->mvs[3].x || pMB->mvs[3].y)  
                         {  
1669                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1670                          }                          } else {
                         else  
                         {  
1671                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1672                          }                          }
1673    
# Line 1379  Line 1679 
1679    
1680          emms();          emms();
1681    
1682          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1683                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1684          }          }
1685    
# Line 1396  Line 1695 
1695          {          {
1696                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1697                  iSearchRange *= 2;                  iSearchRange *= 2;
1698          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1699                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1700                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1701                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1415  Line 1713 
1713    
1714    
1715  #ifdef BFRAMES  #ifdef BFRAMES
1716  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void
1717    FrameCodeB(Encoder * pEnc,
1718                       FRAMEINFO * frame,
1719                       Bitstream * bs,
1720                       uint32_t * pBits)
1721  {  {
1722      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1723      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
# Line 1427  Line 1729 
1729          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1730    
1731          // forward          // forward
1732          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,
1733                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1734                                       pEnc->mbParam.height,
1735                                       frame->global_flags & XVID_INTERLACING);
1736          start_timer();          start_timer();
1737          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,
1738                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1739                                              0);
1740          stop_inter_timer();          stop_inter_timer();
1741    
1742          // backward          // backward
1743          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,
1744                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1745                                       pEnc->mbParam.height,
1746                                       frame->global_flags & XVID_INTERLACING);
1747      start_timer();      start_timer();
1748          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1749                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1750                                              0);
1751          stop_inter_timer();          stop_inter_timer();
1752    
1753          start_timer();          start_timer();
1754          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1755                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1756                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1757                                                     &pEnc->vInterV, &pEnc->vInterHV);
1758    
1759    
1760          stop_motion_timer();          stop_motion_timer();
# Line 1454  Line 1765 
1765          }*/          }*/
1766    
1767      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1768      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1769    
1770      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1771    
# Line 1464  Line 1775 
1775          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1776    
1777    
1778      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
         {  
1779                  // reset prediction                  // reset prediction
1780    
1781                  forward.x = 0;                  forward.x = 0;
# Line 1473  Line 1783 
1783                  backward.x = 0;                  backward.x = 0;
1784                  backward.y = 0;                  backward.y = 0;
1785    
1786                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1787                  {                          MACROBLOCK *f_mb =
1788                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1789                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *b_mb =
1790                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1791                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1792    
1793                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1794                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
                         {  
1795                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1796                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1797                                  continue;                                  continue;
1798                          }                          }
1799    
1800                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1801                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1802                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1803                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1804                                          dct_codes);                                          dct_codes);
1805    
1806                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1807                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);                          mb->cbp =
1808                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1809                                                                      qcoeff);
1810                          //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);
1811    
1812    
1813                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1814                                  mb->cbp == 0 &&                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
1815                                  mb->mode = 5;  // skipped                                  mb->mode = 5;  // skipped
1816                          }                          }
1817    
1818                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
                         {  
1819                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1820                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1821                                  forward.x = mb->mvs[0].x;                                  forward.x = mb->mvs[0].x;
1822                                  forward.y = mb->mvs[0].y;                                  forward.y = mb->mvs[0].y;
1823                          }                          }
1824    
1825                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
                         {  
1826                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1827                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1828                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1829                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1830                          }                          }
1831    //                      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);
 //                      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);  
1832    
1833                          start_timer();                          start_timer();
1834                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1835                                                     &pEnc->sStat);
1836                          stop_coding_timer();                          stop_coding_timer();
1837                  }                  }
1838          }          }

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

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