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

Diff of /trunk/xvidcore/src/encoder.c

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

revision 192, Sun Jun 9 13:16:26 2002 UTC revision 284, Wed Jul 10 19:16:32 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: encoder.c,v 1.39 2002-06-09 13:16:26 edgomez Exp $   *  $Id: encoder.c,v 1.53 2002-07-10 19:16:32 chl Exp $
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    #ifdef _SMP
70    #include "motion/smp_motion_est.h"
71    #endif
72    /*****************************************************************************
73     * Local macros
74     ****************************************************************************/
75    
76  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
77  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
78    
79    /*****************************************************************************
80     * Local function prototypes
81     ****************************************************************************/
82    
83  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
84                        Bitstream * bs,                        Bitstream * bs,
85                        uint32_t *pBits);                        uint32_t *pBits);
# Line 81  Line 97 
97                         uint32_t *pBits);                         uint32_t *pBits);
98  #endif  #endif
99    
100  static int DQtab[4] =  /*****************************************************************************
101  {   * Local data
102     ****************************************************************************/
103    
104    static int DQtab[4] = {
105          -1, -2, 1, 2          -1, -2, 1, 2
106  };  };
107    
108  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
109          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
110  };  };
111    
112    
113  void static image_null(IMAGE * image)  static void __inline
114    image_null(IMAGE * image)
115  {  {
116          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
117  }  }
118    
119    
120  int encoder_create(XVID_ENC_PARAM * pParam)  /*****************************************************************************
121     * Encoder creation
122     *
123     * This function creates an Encoder instance, it allocates all necessary
124     * image buffers (reference, current and bframes) and initialize the internal
125     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
126     *
127     * The code seems to be very long but is very basic, mainly memory allocation
128     * and cleaning code.
129     *
130     * Returned values :
131     *    - XVID_ERR_OK     - no errors
132     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
133     *                        cleans the structure before exiting.
134     *                        pParam->handle is also set to NULL.
135     *
136     ****************************************************************************/
137    
138    int
139    encoder_create(XVID_ENC_PARAM * pParam)
140  {  {
141          Encoder *pEnc;          Encoder *pEnc;
142          uint32_t i;          int i;
143    
144          pParam->handle = NULL;          pParam->handle = NULL;
145    
# Line 114  Line 152 
152    
153          /* Fps */          /* Fps */
154    
155          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
156                  pParam->fincr = 1;                  pParam->fincr = 1;
157                  pParam->fbase = 25;                  pParam->fbase = 25;
158          }          }
# Line 126  Line 163 
163           */           */
164    
165          i = pParam->fincr;          i = pParam->fincr;
166          while (i > 1)          while (i > 1) {
167          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
168                          pParam->fincr /= i;                          pParam->fincr /= i;
169                          pParam->fbase /= i;                          pParam->fbase /= i;
170                          i = pParam->fincr;                          i = pParam->fincr;
# Line 138  Line 173 
173                  i--;                  i--;
174          }          }
175    
176          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
177                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
178    
179                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
180                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
181          }          }
# Line 180  Line 215 
215          if (pEnc == NULL)          if (pEnc == NULL)
216                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
217    
218            /* Zero the Encoder Structure */
219    
220            memset(pEnc, 0, sizeof(Encoder));
221    
222          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
223    
224          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 194  Line 233 
233          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
234          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
235    
236            pEnc->mbParam.m_quant_type = H263_QUANT;
237    
238          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
239    
240          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 213  Line 254 
254    
255          /* try to allocate mb memory */          /* try to allocate mb memory */
256    
257          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
258                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
259                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
260                                           CACHE_LINE);          pEnc->reference->mbs =
261          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
262                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
263    
264          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
265                  goto xvid_err_memory2;                  goto xvid_err_memory2;
266    
267          /* try to allocate image memory */          /* try to allocate image memory */
268    
269  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
270          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
271  #endif  #endif
272  #ifdef BFRAMES  #ifdef BFRAMES
# Line 243  Line 282 
282          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
283          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
284    
285  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
286          if (image_create(&pEnc->sOriginal,          if (image_create
287                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
288                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
289                  goto xvid_err_memory3;                  goto xvid_err_memory3;
290  #endif  #endif
291  #ifdef BFRAMES  #ifdef BFRAMES
292          if (image_create(&pEnc->f_refh,          if (image_create
293                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
294                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
295                  goto xvid_err_memory3;                  goto xvid_err_memory3;
296          if (image_create(&pEnc->f_refv,          if (image_create
297                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
298                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
299                  goto xvid_err_memory3;                  goto xvid_err_memory3;
300          if (image_create(&pEnc->f_refhv,          if (image_create
301                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
302                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
303                  goto xvid_err_memory3;                  goto xvid_err_memory3;
304  #endif  #endif
305          if (image_create(&pEnc->current->image,          if (image_create
306                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
307                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
308                  goto xvid_err_memory3;                  goto xvid_err_memory3;
309          if (image_create(&pEnc->reference->image,          if (image_create
310                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
311                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
312                  goto xvid_err_memory3;                  goto xvid_err_memory3;
313          if (image_create(&pEnc->vInterH,          if (image_create
314                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
315                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
316                  goto xvid_err_memory3;                  goto xvid_err_memory3;
317          if (image_create(&pEnc->vInterV,          if (image_create
318                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
319                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
320                  goto xvid_err_memory3;                  goto xvid_err_memory3;
321          if (image_create(&pEnc->vInterVf,          if (image_create
322                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
323                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
324                  goto xvid_err_memory3;                  goto xvid_err_memory3;
325          if (image_create(&pEnc->vInterHV,          if (image_create
326                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
327                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
328                  goto xvid_err_memory3;                  goto xvid_err_memory3;
329          if (image_create(&pEnc->vInterHVf,          if (image_create
330                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
331                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
332                  goto xvid_err_memory3;                  goto xvid_err_memory3;
333    
# Line 297  Line 336 
336          /* B Frames specific init */          /* B Frames specific init */
337  #ifdef BFRAMES  #ifdef BFRAMES
338    
339            pEnc->global = pParam->global;
340          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
341          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
342          pEnc->bframes = NULL;          pEnc->bframes = NULL;
343    
344          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
345                  int n;                  int n;
346    
347                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
348                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
349                                              CACHE_LINE);                                              CACHE_LINE);
350    
351                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 316  Line 355 
355                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
356    
357    
358                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
359                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
360    
361                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
362                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
363    
364                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
365                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
366                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
367    
368                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
369                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
370    
371                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
372    
373                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
374                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
375                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
376                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
377    
# Line 345  Line 381 
381          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
382          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
383          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
384            pEnc->bframenum_dx50bvop = -1;
385    
386            pEnc->queue = NULL;
387    
388    
389            if (pEnc->mbParam.max_bframes > 0) {
390                    int n;
391    
392                    pEnc->queue =
393                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
394                                                    CACHE_LINE);
395    
396                    if (pEnc->queue == NULL)
397                            goto xvid_err_memory4;
398    
399                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
400                            image_null(&pEnc->queue[n]);
401    
402                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
403                            if (image_create
404                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
405                                     pEnc->mbParam.edged_height) < 0)
406                                    goto xvid_err_memory5;
407    
408                    }
409            }
410    
411            pEnc->queue_head = 0;
412            pEnc->queue_tail = 0;
413            pEnc->queue_size = 0;
414    
415    
416          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
417          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
418            pEnc->m_framenum = 0;
419  #endif  #endif
420    
421          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
422    
423          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
424          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
425                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
426                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
427                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
428                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
429          }          }
430    
431          init_timer();          init_timer();
# Line 370  Line 434 
434    
435          /*          /*
436           * 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  
437           */           */
438  #ifdef BFRAMES  #ifdef BFRAMES
439      xvid_err_memory5:
440    
441    
442            if (pEnc->mbParam.max_bframes > 0) {
443    
444                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
445                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
446                                                      pEnc->mbParam.edged_height);
447                    }
448                    xvid_free(pEnc->queue);
449            }
450    
451   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
452    
453                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
454    
455                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
456                                pEnc->mbParam.edged_width,  
457                            if (pEnc->bframes[i] == NULL)
458                                    continue;
459    
460                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
461                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
462    
463                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 392  Line 467 
467          }          }
468    
469          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
470            }
471    
472  #endif  #endif
473    
474   xvid_err_memory3:   xvid_err_memory3:
475  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
476          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
477                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
478  #endif  #endif
479    
480  #ifdef BFRAMES  #ifdef BFRAMES
481          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
482                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
483          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
484                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
485          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
486                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
487  #endif  #endif
488    
489          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
490                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
491          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
492                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
493          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
494                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
495          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
496                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
497          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
498                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
499          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
500                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
501          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
502                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
503    
504   xvid_err_memory2:   xvid_err_memory2:
# Line 445  Line 510 
510          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
511          xvid_free(pEnc);          xvid_free(pEnc);
512    
513            pParam->handle = NULL;
514    
515          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
516  }  }
517    
518  int encoder_destroy(Encoder * pEnc)  /*****************************************************************************
519     * Encoder destruction
520     *
521     * This function destroy the entire encoder structure created by a previous
522     * successful encoder_create call.
523     *
524     * Returned values (for now only one returned value) :
525     *    - XVID_ERR_OK     - no errors
526     *
527     ****************************************************************************/
528    
529    int
530    encoder_destroy(Encoder * pEnc)
531  {  {
532    #ifdef BFRAMES
533            int i;
534    #endif
535    
536          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
537    
538          /* B Frames specific */          /* B Frames specific */
539  #ifdef BFRAMES  #ifdef BFRAMES
540          int i;          if (pEnc->mbParam.max_bframes > 0) {
541    
542          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
         {  
543    
544                  if (pEnc->bframes[i] == NULL) continue;                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
545                                              pEnc->mbParam.edged_height);
546                    }
547                    xvid_free(pEnc->queue);
548            }
549    
550                  image_destroy(&pEnc->bframes[i]->image,  
551                                pEnc->mbParam.edged_width,          if (pEnc->mbParam.max_bframes > 0) {
552    
553                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
554    
555                            if (pEnc->bframes[i] == NULL)
556                                    continue;
557    
558                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
559                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
560    
561                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
562    
563                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
564          }          }
565    
566          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
567    
568            }
569  #endif  #endif
570    
571          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
572    
573          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
574                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
575          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
576                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
577          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
578                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
579          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
580                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
582                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
584                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
585          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
586                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
587  #ifdef BFRAMES  #ifdef BFRAMES
588          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
589                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
590          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
591                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
592          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
593                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
594  #endif  #endif
595  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
596          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
597                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
598  #endif  #endif
599    
# Line 529  Line 611 
611  }  }
612    
613    
614    #ifdef BFRAMES
615    void inc_frame_num(Encoder * pEnc)
616    {
617            pEnc->iFrameNum++;
618            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
619    
620            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
621            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
622    }
623    #endif
624    
625    
 // ==================================================================  
626  #ifdef BFRAMES  #ifdef BFRAMES
627  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
628    {
629            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
630            {
631                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
632                    return;
633            }
634    
635            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
636                                    pEnc->bframenum_head, pEnc->bframenum_tail,
637                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
638    
639    
640            start_timer();
641            if (image_input
642                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
643                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
644                    return;
645            stop_conv_timer();
646    
647            pEnc->queue_size++;
648            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
649    }
650    #endif
651    
652    
653    #ifdef BFRAMES
654    /*****************************************************************************
655     * IPB frame encoder entry point
656     *
657     * Returned values :
658     *    - XVID_ERR_OK     - no errors
659     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
660     *                        format
661     ****************************************************************************/
662    
663    int
664    encoder_encode_bframes(Encoder * pEnc,
665                               XVID_ENC_FRAME * pFrame,
666                               XVID_ENC_STATS * pResult)
667  {  {
668          uint16_t x, y;          uint16_t x, y;
669          Bitstream bs;          Bitstream bs;
670          uint32_t bits;          uint32_t bits;
671    
672  #ifdef _DEBUG          int input_valid = 1;
673    
674    #ifdef _DEBUG_PSNR
675          float psnr;          float psnr;
676          char temp[128];          char temp[128];
677  #endif  #endif
678    
679          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
680          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
681            ENC_CHECK(pFrame->image);
682    
683          start_global_timer();          start_global_timer();
684    
685          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
686    
687  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ipvop_loop:
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
688    
689          if ((pFrame->image == NULL || pEnc->flush_bframes) &&          /*
690                  pEnc->bframenum_head < pEnc->bframenum_tail)           * bframe "flush" code
691          {           */
692    
693            if ((pFrame->image == NULL || pEnc->flush_bframes)
694                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
695    
696                    if (pEnc->flush_bframes == 0) {
697                            /*
698                             * we have reached the end of stream without getting
699                             * a future reference frame... so encode last final
700                             * frame as a pframe
701                             */
702    
703                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
704                                    pEnc->bframenum_head, pEnc->bframenum_tail,
705                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
706    
                 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) --- ");  
707                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
708                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
709    
# Line 572  Line 713 
713    
714                          BitstreamPad(&bs);                          BitstreamPad(&bs);
715                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
716                          pFrame->intra = 0;                          pFrame->intra = 0;
717    
718                          return XVID_ERR_OK;                          return XVID_ERR_OK;
719                  }                  }
720    
                 //dprintf("--- BFRAME (flush) --- ");  
                 FrameCodeB(pEnc,  
                         pEnc->bframes[pEnc->bframenum_head],  
                          &bs, &bits);  
                 pEnc->bframenum_head++;  
721    
722                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
723                                    pEnc->bframenum_head, pEnc->bframenum_tail,
724                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
725    
726                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
727                    pEnc->bframenum_head++;
728    
729                  BitstreamPad(&bs);                  BitstreamPad(&bs);
730                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
731                  pFrame->intra = 0;                  pFrame->intra = 0;
732    
733                    if (input_valid)
734                            queue_image(pEnc, pFrame);
735    
736                  return XVID_ERR_OK;                  return XVID_ERR_OK;
737          }          }
738    
739          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
740          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
741                  pFrame->length = 0;  
742                  pFrame->input_consumed = 1;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
743    
744                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
745                                    pEnc->bframenum_head, pEnc->bframenum_tail,
746                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
747    
748                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
749                            BitstreamPad(&bs);
750                            BitstreamPutBits(&bs, 0x7f, 8);
751    
752                            pFrame->length = BitstreamLength(&bs);
753                  pFrame->intra = 0;                  pFrame->intra = 0;
754    
755                            if (input_valid)
756                                    queue_image(pEnc, pFrame);
757    
758                  return XVID_ERR_OK;                  return XVID_ERR_OK;
759          }          }
760            }
761    
762    
763    bvop_loop:
764    
765            if (pEnc->bframenum_dx50bvop != -1)
766            {
767    
768                    SWAP(pEnc->current, pEnc->reference);
769                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
770    
771                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
772                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
773                    }
774    
775          if (pEnc->bframenum_head > 0)                  if (input_valid)
776          {          {
777                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                          queue_image(pEnc, pFrame);
778                            input_valid = 0;
779          }          }
780    
781          pEnc->flush_bframes = 0;          } else if (input_valid) {
782    
783  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  SWAP(pEnc->current, pEnc->reference);
784    
785                    start_timer();
786                    if (image_input
787                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
788                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
789                            return XVID_ERR_FORMAT;
790                    stop_conv_timer();
791    
792                    // queue input frame, and dequue next image
793                    if (pEnc->queue_size > 0)
794                    {
795                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
796                            if (pEnc->queue_head != pEnc->queue_tail)
797                            {
798                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
799                            }
800                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
801                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
802                    }
803    
804            } else if (pEnc->queue_size > 0) {
805    
806          SWAP(pEnc->current, pEnc->reference);          SWAP(pEnc->current, pEnc->reference);
807    
808          pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant;                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
809                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
810                    pEnc->queue_size--;
811    
812            } else if (BitstreamPos(&bs) == 0) {
813    
814                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
815                                    pEnc->bframenum_head, pEnc->bframenum_tail,
816                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
817    
818                    pFrame->intra = 0;
819    
820                    BitstreamPutBits(&bs, 0x7f, 8);
821                    BitstreamPad(&bs);
822                    pFrame->length = BitstreamLength(&bs);
823    
824                    return XVID_ERR_OK;
825    
826            } else {
827    
828                    pFrame->length = BitstreamLength(&bs);
829                    return XVID_ERR_OK;
830            }
831    
832            pEnc->flush_bframes = 0;
833    
834            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835             * Well there was a separation here so i put it in ANSI C
836             * comment style :-)
837             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
838    
839            emms();
840    
841            // only inc frame num, adapt quant, etc. if we havent seen it before
842            if (pEnc->bframenum_dx50bvop < 0 )
843            {
844                    if (pFrame->quant == 0)
845                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
846                    else
847                            pEnc->current->quant = pFrame->quant;
848    
849          if(pEnc->current->quant < 1)          if(pEnc->current->quant < 1)
850                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
# Line 620  Line 854 
854    
855          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
856          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
857          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
858          pEnc->current->ticks = pEnc->mbParam.m_ticks;                  /* ToDo : dynamic fcode (in both directions) */
         //@@@ TODO: dyanmic fcode (in both directions)  
859          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
860          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
861    
862          start_timer();                  pEnc->current->seconds = pEnc->mbParam.m_seconds;
863          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,                  pEnc->current->ticks = pEnc->mbParam.m_ticks;
                         pFrame->image, pFrame->colorspace))  
         {  
                 return XVID_ERR_FORMAT;  
         }  
         stop_conv_timer();  
864    
865  #ifdef _DEBUG                  inc_frame_num(pEnc);
866          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);  
867    #ifdef _DEBUG_PSNR
868                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
869                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
870  #endif  #endif
871    
872                    emms();
873    
874  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
875  // lumi masking                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
876  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                  "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
877                    }
878    
879          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880          {           * Luminance masking
881                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
882    
883                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
884                                                              pEnc->mbParam.edged_width,  // stride                          int *temp_dquants =
885                                                              temp_dquants,                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
886                                                              pEnc->current->quant,                                                                  pEnc->mbParam.mb_height * sizeof(int),
887                                                              pEnc->current->quant,       // min_quant                                                                  CACHE_LINE);
888                                                              2*pEnc->current->quant,     // max_quant  
889                            pEnc->current->quant =
890                                    adaptive_quantization(pEnc->current->image.y,
891                                                                      pEnc->mbParam.edged_width, temp_dquants,
892                                                                      pEnc->current->quant, pEnc->current->quant,
893                                                                      2 * pEnc->current->quant,
894                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
895                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
896    
897                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
898                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
899                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
900                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
901                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
902                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
903    
904                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
905                          }                          }
906    
907    #undef OFFSET
908                            }
909    
910                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
911          }          }
912    
913            }
914    
915  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
916  // ivop/pvop/bvop selection           * ivop/pvop/bvop selection
917  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
   
         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 ---");  
918    
                 FrameCodeI(pEnc, &bs, &bits);  
919    
920            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
921                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
922                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
923                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
924                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
925                                             pEnc->mbParam.height) > 30) {
926                    /*
927                     * This will be coded as an Intra Frame
928                     */
929    
930                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
931                                    pEnc->bframenum_head, pEnc->bframenum_tail,
932                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
933    
934                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
935                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
936                    }
937    
938                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
939    
940                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
941    
942                            pEnc->bframenum_tail--;
943                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
944    
945                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
946                            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
947                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
948                            }
949                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
950    
951                            pFrame->intra = 0;
952    
953                    } else {
954    
955                            FrameCodeI(pEnc, &bs, &bits);
956                  pFrame->intra = 1;                  pFrame->intra = 1;
957    
958                            pEnc->bframenum_dx50bvop = -1;
959                    }
960    
961                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
962    
963                  /* note: sequences like "IIBB" decode fine with msfdam but,                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
964                     go screwy with divx5.00 */                          BitstreamPad(&bs);
965                            input_valid = 0;
966                            goto ipvop_loop;
967                    }
968    
969                    /*
970                     * NB : sequences like "IIBB" decode fine with msfdam but,
971                     *      go screwy with divx 5.00
972                     */
973            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
974                    /*
975                     * This will be coded as a Predicted Frame
976                     */
977    
978                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
979                                    pEnc->bframenum_head, pEnc->bframenum_tail,
980                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
981    
982                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
983                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
984          }          }
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 //dprintf("--- PFRAME ---");  
985    
986                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
987                  pFrame->intra = 0;                  pFrame->intra = 0;
988                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
989    
990                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
991                            BitstreamPad(&bs);
992                            input_valid = 0;
993                            goto ipvop_loop;
994          }          }
         else  
         {  
                 //dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
995    
996                  if (pFrame->bquant < 1)          } else {
997                  {                  /*
998                          pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;                   * This will be coded as a Bidirectional Frame
999                     */
1000    
1001                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1002                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1003                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1004    
1005                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1006                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1007                  }                  }
1008                  else  
1009                  {                  if (pFrame->bquant < 1) {
1010                            pEnc->current->quant =
1011                                    ((pEnc->reference->quant +
1012                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1013                    } else {
1014                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1015                  }                  }
1016    
1017                  // store frame into bframe buffer & swap ref back to current                  /* store frame into bframe buffer & swap ref back to current */
1018                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1019                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
1020    
# Line 714  Line 1022 
1022    
1023                  pFrame->intra = 0;                  pFrame->intra = 0;
1024                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1025    
1026                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1027                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
                 return XVID_ERR_OK;  
1028          }          }
1029    
1030          BitstreamPad(&bs);          BitstreamPad(&bs);
1031          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1032    
1033          if (pResult)          if (pResult) {
         {  
1034                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1035                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1036                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 737  Line 1038 
1038                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1039          }          }
1040    
1041  #ifdef _DEBUG          emms();
1042          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
1043                                  pEnc->mbParam.width, pEnc->mbParam.height);  #ifdef _DEBUG_PSNR
1044            psnr =
1045                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1046                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1047                                       pEnc->mbParam.height);
1048    
1049          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1050          DEBUG(temp);          DEBUG(temp);
1051  #endif  #endif
1052    
1053          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1054          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1055                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
1056          }          }
1057    
         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;  
1058    
1059          stop_global_timer();          stop_global_timer();
1060          write_timer();          write_timer();
1061    
1062          return XVID_ERR_OK;          return XVID_ERR_OK;
1063  }  }
1064  // ==================================================================  
1065  #else  #endif
1066  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1067    
1068    
1069    /*****************************************************************************
1070     * "original" IP frame encoder entry point
1071     *
1072     * Returned values :
1073     *    - XVID_ERR_OK     - no errors
1074     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1075     *                        format
1076     ****************************************************************************/
1077    
1078    int
1079    encoder_encode(Encoder * pEnc,
1080                               XVID_ENC_FRAME * pFrame,
1081                               XVID_ENC_STATS * pResult)
1082  {  {
1083          uint16_t x, y;          uint16_t x, y;
1084          Bitstream bs;          Bitstream bs;
1085          uint32_t bits;          uint32_t bits;
1086          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1087  #ifdef _DEBUG  
1088    #ifdef _DEBUG_PSNR
1089          float psnr;          float psnr;
1090          uint8_t temp[100];          uint8_t temp[128];
1091  #endif  #endif
1092    
1093          start_global_timer();          start_global_timer();
# Line 791  Line 1101 
1101    
1102          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1103          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1104    #ifdef BFRAMES
1105            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1106            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1107    #endif
1108          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1109    
1110          start_timer();          start_timer();
1111          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
1112                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1113          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1114                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1115          stop_conv_timer();          stop_conv_timer();
1116    
1117  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1118          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1119                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1120  #endif  #endif
1121    
1122          EMMS();          emms();
1123    
1124          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1125    
1126          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1127                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1128          }          } else {
         else  
         {  
1129                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1130          }          }
1131    
1132          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1133          {                  int *temp_dquants =
1134                  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 *
1135                                                                    pEnc->mbParam.mb_height * sizeof(int),
1136                                                                    CACHE_LINE);
1137    
1138                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  pEnc->current->quant =
1139                                                              pEnc->mbParam.edged_width,  // stride                          adaptive_quantization(pEnc->current->image.y,
1140                                                              temp_dquants,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1141                                                              pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
1142                                                              pEnc->current->quant,       // min_quant                                                                    2 * pEnc->current->quant,
                                                             2*pEnc->current->quant,     // max_quant  
1143                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1144                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1145    
1146                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1147                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1148                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1149                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1150                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1151    
1152    
1153                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1154    
1155                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1156                          }                          }
1157    
1158    #undef OFFSET
1159                    }
1160    
1161                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1162          }          }
1163    
# Line 844  Line 1165 
1165                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1166                          write_vol_header = 1;                          write_vol_header = 1;
1167                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1168          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1169          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1170    
1171                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1172    
1173                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1174                          write_vol_header = 1;                          write_vol_header = 1;
# Line 857  Line 1177 
1177    
1178                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1179                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1180                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1181                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1182                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1183                  }                  } else {
1184                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1185                          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());  
1186                  }                  }
1187                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1188                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1189          }          }
1190    
1191          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1192          {                  if ((pEnc->iFrameNum == 0)
1193                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1194                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1195                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1196                  else                  } else {
1197                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1198          }          }
1199          else          } else {
1200          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1201                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1202                  else                  } else {
1203                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1204          }          }
1205    
1206            }
1207    
1208          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1209          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1210          BitstreamPad(&bs);          BitstreamPad(&bs);
1211          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1212    
1213          if (pResult)          if (pResult) {
         {  
1214                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1215                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1216                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 900  Line 1218 
1218                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1219          }          }
1220    
1221          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1222    
1223  #ifdef _DEBUG          if (pFrame->quant == 0) {
1224          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1225                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
1226            }
1227    #ifdef _DEBUG_PSNR
1228            psnr =
1229                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1230                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1231                                       pEnc->mbParam.height);
1232    
1233          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1234          DEBUG(temp);          DEBUG(temp);
1235  #endif  #endif
1236    
1237    #ifdef BFRAMES
1238            inc_frame_num(pEnc);
1239    #else
1240          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1241    #endif
1242    
1243    
1244          stop_global_timer();          stop_global_timer();
1245          write_timer();          write_timer();
1246    
1247          return XVID_ERR_OK;          return XVID_ERR_OK;
1248  }  }
 #endif  
1249    
1250    
1251  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1252    CodeIntraMB(Encoder * pEnc,
1253                            MACROBLOCK * pMB)
1254    {
1255    
1256          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1257    
# Line 939  Line 1262 
1262          pMB->sad16 = 0;          pMB->sad16 = 0;
1263    
1264          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1265                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1266                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1267                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1268    
1269                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1270                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1271                            if (pEnc->current->quant < 1)
1272                                    pEnc->current->quant = 1;
1273                  }                  }
1274          }          }
1275    
# Line 956  Line 1280 
1280  #define FCODEBITS       3  #define FCODEBITS       3
1281  #define MODEBITS        5  #define MODEBITS        5
1282    
1283  void HintedMESet(Encoder * pEnc, int * intra)  void
1284    HintedMESet(Encoder * pEnc,
1285                            int *intra)
1286  {  {
1287          HINTINFO * hint;          HINTINFO * hint;
1288          Bitstream bs;          Bitstream bs;
# Line 965  Line 1291 
1291    
1292          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1293    
1294          if (hint->rawhints)          if (hint->rawhints) {
         {  
1295                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1296          }          } else {
         else  
         {  
1297                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1298                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1299          }          }
1300    
1301          if (*intra)          if (*intra) {
         {  
1302                  return;                  return;
1303          }          }
1304    
1305          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1306                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1307                                                                                                                                     FCODEBITS);
1308    
1309          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1310          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1311    
1312          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1313          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1314                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1315                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1316                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1317                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1318                          VECTOR pred[4];                          VECTOR pred;
1319                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1320                          int vec;                          int vec;
1321    
1322                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1323                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1324                                                                                                                                      MODEBITS);
1325    
1326                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1327                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1328    
1329                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1330                          {                                  tmp.x =
1331                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1332                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1333                                    tmp.y =
1334                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1335                                                                                                                                                      length);
1336                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1337                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1338    
1339                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
1340    
1341                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1342                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1343                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1344                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1345                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
1346                          }                          }
1347                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
1348                          {                                  for (vec = 0; vec < 4; ++vec) {
1349                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
1350                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
1351                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
1352                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
1353                                                    (hint->rawhints) ? bhint->mvs[vec].
1354                                                    y : BitstreamGetBits(&bs, length);
1355                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1356                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1357    
1358                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
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.x;
1363                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.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    
1595    #ifdef _SMP
1596                    if (NUMTHREADS > 1)
1597                            bIntra =
1598                                    SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1599                                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1600                                                             iLimit);
1601          else          else
1602          {  #endif
1603                  bIntra = MotionEstimation(  
1604                          &pEnc->mbParam,                  bIntra =
1605                          pEnc->current,                           MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1606                          pEnc->reference,                                                      &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1607                          iLimit);                          iLimit);
1608    
1609    
1610          }          }
1611          stop_motion_timer();          stop_motion_timer();
1612    
1613          if (bIntra == 1)          if (bIntra == 1) {
         {  
1614                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1615          }          }
1616    
# Line 1296  Line 1619 
1619          if(vol_header)          if(vol_header)
1620                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1621    
1622          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1623    
1624          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1625    
# Line 1305  Line 1628 
1628          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1629          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1630    
1631          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1632          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1633                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1634                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1635    
1636                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1637    
1638                          if (!bIntra)                          if (!bIntra) {
                         {  
1639                                  start_timer();                                  start_timer();
1640                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1641                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1642                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1643                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1644                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1645                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1646                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1334  Line 1650 
1650                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1651                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1652                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1653                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1654                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1655                                                    else if (pEnc->current->quant < 1)
1656                                                            pEnc->current->quant = 1;
1657                                          }                                          }
1658                                  }                                  }
1659                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1660    
1661                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1662    
1663                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
1664                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1665                          else                                                                            dct_codes, qcoeff);
1666                          {                          } else {
1667                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1668                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1669                                                                      dct_codes, qcoeff);
1670                          }                          }
1671    
1672                          start_timer();                          start_timer();
1673                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1674                          stop_prediction_timer();                          stop_prediction_timer();
1675    
1676                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1677                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1678                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1679                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1680                                   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)  
                         {  
1681                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1682                          }                          } else {
                         else  
                         {  
1683                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1684                          }                          }
1685    
# Line 1379  Line 1691 
1691    
1692          emms();          emms();
1693    
1694          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1695                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1696          }          }
1697    
# Line 1396  Line 1707 
1707          {          {
1708                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1709                  iSearchRange *= 2;                  iSearchRange *= 2;
1710          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1711                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1712                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1713                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1415  Line 1725 
1725    
1726    
1727  #ifdef BFRAMES  #ifdef BFRAMES
1728  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void
1729    FrameCodeB(Encoder * pEnc,
1730                       FRAMEINFO * frame,
1731                       Bitstream * bs,
1732                       uint32_t * pBits)
1733  {  {
1734      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1735      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
# Line 1427  Line 1741 
1741          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1742    
1743          // forward          // forward
1744          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,
1745                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1746                                       pEnc->mbParam.height,
1747                                       frame->global_flags & XVID_INTERLACING);
1748          start_timer();          start_timer();
1749          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,
1750                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1751                                              0);
1752          stop_inter_timer();          stop_inter_timer();
1753    
1754          // backward          // backward
1755          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,
1756                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1757                                       pEnc->mbParam.height,
1758                                       frame->global_flags & XVID_INTERLACING);
1759      start_timer();      start_timer();
1760          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1761                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1762                                              0);
1763          stop_inter_timer();          stop_inter_timer();
1764    
1765          start_timer();          start_timer();
1766          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1767                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1768                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1769                                                     &pEnc->vInterV, &pEnc->vInterHV);
1770    
1771    
1772          stop_motion_timer();          stop_motion_timer();
# Line 1454  Line 1777 
1777          }*/          }*/
1778    
1779      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1780      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1781    
1782      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1783    
# Line 1464  Line 1787 
1787          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1788    
1789    
1790      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
         {  
1791                  // reset prediction                  // reset prediction
1792    
1793                  forward.x = 0;                  forward.x = 0;
# Line 1473  Line 1795 
1795                  backward.x = 0;                  backward.x = 0;
1796                  backward.y = 0;                  backward.y = 0;
1797    
1798                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1799                  {                          MACROBLOCK *f_mb =
1800                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1801                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *b_mb =
1802                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1803                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1804    
1805                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1806                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
                         {  
1807                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1808                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1809                                  continue;                                  continue;
1810                          }                          }
1811    
1812                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1813                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1814                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1815                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1816                                          dct_codes);                                          dct_codes);
1817    
1818                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1819                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);                          mb->cbp =
1820                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1821                                                                      qcoeff);
1822                          //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);
1823    
1824    
1825                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1826                                  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)  
                         {  
1827                                  mb->mode = 5;  // skipped                                  mb->mode = 5;  // skipped
1828                          }                          }
1829    
1830                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
                         {  
1831                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1832                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1833                                  forward.x = mb->mvs[0].x;                                  forward.x = mb->mvs[0].x;
1834                                  forward.y = mb->mvs[0].y;                                  forward.y = mb->mvs[0].y;
1835                          }                          }
1836    
1837                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
                         {  
1838                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1839                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1840                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1841                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1842                          }                          }
1843    //                      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);  
1844    
1845                          start_timer();                          start_timer();
1846                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1847                                                     &pEnc->sStat);
1848                          stop_coding_timer();                          stop_coding_timer();
1849                  }                  }
1850          }          }

Legend:
Removed from v.192  
changed lines
  Added in v.284

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