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

Diff of /xvidcore/src/encoder.c

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

revision 1.39, Sun Jun 9 13:16:26 2002 UTC revision 1.76.2.35, Sat Jan 11 14:59:23 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Encoder main module  -   *  -  Encoder main module  -
# Line 26  Line 26 
26   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
27   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   ***************************************************************************/   ****************************************************************************/
30    
31  /****************************************************************************  /*****************************************************************************
32   *   *
33   *  History   *  History
34   *   *
35     *  10.07.2002  added BFRAMES_DEC_DEBUG support
36     *              MinChen <chenm001@163.com>
37     *  20.06.2002 bframe patch
38   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id$   *  $Id$
43   *   *
44   ***************************************************************************/   ****************************************************************************/
45    
46  #include <stdlib.h>  #include <stdlib.h>
47  #include <stdio.h>  #include <stdio.h>
48  #include <math.h>  #include <math.h>
49    #include <string.h>
50    
51  #include "encoder.h"  #include "encoder.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "global.h"  #include "global.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "image/image.h"  #include "image/image.h"
56    #include "image/font.h"
57    #include "motion/sad.h"
58  #include "motion/motion.h"  #include "motion/motion.h"
59  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
60  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 61  Line 67 
67  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
68  #include "utils/mem_align.h"  #include "utils/mem_align.h"
69    
70    /*****************************************************************************
71     * Local macros
72     ****************************************************************************/
73    
74  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
75  #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }  #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
76    
77    /*****************************************************************************
78     * Local function prototypes
79     ****************************************************************************/
80    
81  static int FrameCodeI(Encoder * pEnc,  static int FrameCodeI(Encoder * pEnc,
82                        Bitstream * bs,                        Bitstream * bs,
# Line 74  Line 88 
88                        bool force_inter,                        bool force_inter,
89                        bool vol_header);                        bool vol_header);
90    
 #ifdef BFRAMES  
91  static void FrameCodeB(Encoder * pEnc,  static void FrameCodeB(Encoder * pEnc,
92                         FRAMEINFO * frame,                         FRAMEINFO * frame,
93                         Bitstream * bs,                         Bitstream * bs,
94                         uint32_t *pBits);                         uint32_t *pBits);
 #endif  
95    
96  static int DQtab[4] =  /*****************************************************************************
97  {   * Local data
98     ****************************************************************************/
99    
100    static int DQtab[4] = {
101          -1, -2, 1, 2          -1, -2, 1, 2
102  };  };
103    
104  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
105          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
106  };  };
107    
108    
109  void static image_null(IMAGE * image)  /*****************************************************************************
110  {   * Encoder creation
111          image->y = image->u = image->v = NULL;   *
112  }   * This function creates an Encoder instance, it allocates all necessary
113     * image buffers (reference, current and bframes) and initialize the internal
114     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
115     *
116     * The code seems to be very long but is very basic, mainly memory allocation
117     * and cleaning code.
118     *
119     * Returned values :
120     *    - XVID_ERR_OK     - no errors
121     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
122     *                        cleans the structure before exiting.
123     *                        pParam->handle is also set to NULL.
124     *
125     ****************************************************************************/
126    
127  int encoder_create(XVID_ENC_PARAM * pParam)  int
128    encoder_create(XVID_ENC_PARAM * pParam)
129  {  {
130          Encoder *pEnc;          Encoder *pEnc;
131          uint32_t i;          int i;
132    
133          pParam->handle = NULL;          pParam->handle = NULL;
134    
# Line 114  Line 141 
141    
142          /* Fps */          /* Fps */
143    
144          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
145                  pParam->fincr = 1;                  pParam->fincr = 1;
146                  pParam->fbase = 25;                  pParam->fbase = 25;
147          }          }
# Line 126  Line 152 
152           */           */
153    
154          i = pParam->fincr;          i = pParam->fincr;
155          while (i > 1)          while (i > 1) {
156          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
157                          pParam->fincr /= i;                          pParam->fincr /= i;
158                          pParam->fbase /= i;                          pParam->fbase /= i;
159                          i = pParam->fincr;                          i = pParam->fincr;
# Line 138  Line 162 
162                  i--;                  i--;
163          }          }
164    
165          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
166                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
167    
168                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
169                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
170          }          }
# Line 172  Line 196 
196    
197          /* 1 keyframe each 10 seconds */          /* 1 keyframe each 10 seconds */
198    
199          if (pParam->max_key_interval == 0)          if (pParam->max_key_interval <= 0)
200                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;                  pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
201    
   
202          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);          pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
203          if (pEnc == NULL)          if (pEnc == NULL)
204                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
205    
206            /* Zero the Encoder Structure */
207    
208            memset(pEnc, 0, sizeof(Encoder));
209    
210          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
211    
212          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 194  Line 221 
221          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
222          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
223    
224          pEnc->sStat.fMvPrevSigma = -1;          pEnc->mbParam.m_quant_type = H263_QUANT;
225    
226            pEnc->fMvPrevSigma = -1;
227    
228          /* Fill rate control parameters */          /* Fill rate control parameters */
229    
230          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
231    
232          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
233          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
234    
235          /* try to allocate frame memory */          /* try to allocate frame memory */
236    
# Line 213  Line 242 
242    
243          /* try to allocate mb memory */          /* try to allocate mb memory */
244    
245          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
246                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
247                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
248                                           CACHE_LINE);          pEnc->reference->mbs =
249          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
250                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
251    
252          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
253                  goto xvid_err_memory2;                  goto xvid_err_memory2;
254    
255          /* try to allocate image memory */          /* try to allocate image memory */
256    
257  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
258          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
259  #endif  #endif
260  #ifdef BFRAMES  
261          image_null(&pEnc->f_refh);          image_null(&pEnc->f_refh);
262          image_null(&pEnc->f_refv);          image_null(&pEnc->f_refv);
263          image_null(&pEnc->f_refhv);          image_null(&pEnc->f_refhv);
264  #endif  
265          image_null(&pEnc->current->image);          image_null(&pEnc->current->image);
266          image_null(&pEnc->reference->image);          image_null(&pEnc->reference->image);
267          image_null(&pEnc->vInterH);          image_null(&pEnc->vInterH);
# Line 243  Line 270 
270          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
271          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
272    
273  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
274          if (image_create(&pEnc->sOriginal,          if (image_create
275                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
276                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
277                  goto xvid_err_memory3;                  goto xvid_err_memory3;
278  #endif  #endif
279  #ifdef BFRAMES  
280          if (image_create(&pEnc->f_refh,          if (image_create
281                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
282                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
283                  goto xvid_err_memory3;                  goto xvid_err_memory3;
284          if (image_create(&pEnc->f_refv,          if (image_create
285                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
286                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
287                  goto xvid_err_memory3;                  goto xvid_err_memory3;
288          if (image_create(&pEnc->f_refhv,          if (image_create
289                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
290                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
291                  goto xvid_err_memory3;                  goto xvid_err_memory3;
292  #endif  
293          if (image_create(&pEnc->current->image,          if (image_create
294                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
295                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
296                  goto xvid_err_memory3;                  goto xvid_err_memory3;
297          if (image_create(&pEnc->reference->image,          if (image_create
298                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
299                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
300                  goto xvid_err_memory3;                  goto xvid_err_memory3;
301          if (image_create(&pEnc->vInterH,          if (image_create
302                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
303                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
304                  goto xvid_err_memory3;                  goto xvid_err_memory3;
305          if (image_create(&pEnc->vInterV,          if (image_create
306                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, 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->vInterVf,          if (image_create
310                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, 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->vInterHV,          if (image_create
314                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, 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->vInterHVf,          if (image_create
318                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, 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    
322    /* Create full bitplane for GMC, this might be wasteful */
323            if (image_create
324                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
325                     pEnc->mbParam.edged_height) < 0)
326                    goto xvid_err_memory3;
327    
328    
329          /* B Frames specific init */          /* B Frames specific init */
 #ifdef BFRAMES  
330    
331            pEnc->mbParam.global = pParam->global;
332          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
333          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
334            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
335            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
336          pEnc->bframes = NULL;          pEnc->bframes = NULL;
337    
338          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
339                  int n;                  int n;
340    
341                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
342                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
343                                              CACHE_LINE);                                              CACHE_LINE);
344    
345                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 316  Line 349 
349                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
350    
351    
352                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
353                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
354    
355                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
356                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
357    
358                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
359                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
360                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
361    
362                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
363                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
364    
365                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
366    
367                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
368                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
369                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
370                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
371    
# Line 345  Line 375 
375          pEnc->bframenum_head = 0;          pEnc->bframenum_head = 0;
376          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
377          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
378            pEnc->bframenum_dx50bvop = -1;
379    
380          pEnc->mbParam.m_seconds = 0;          pEnc->queue = NULL;
381          pEnc->mbParam.m_ticks = 0;  
382  #endif  
383            if (pEnc->mbParam.max_bframes > 0) {
384                    int n;
385    
386                    pEnc->queue =
387                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
388                                                    CACHE_LINE);
389    
390                    if (pEnc->queue == NULL)
391                            goto xvid_err_memory4;
392    
393                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
394                            image_null(&pEnc->queue[n]);
395    
396                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
397                            if (image_create
398                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
399                                     pEnc->mbParam.edged_height) < 0)
400                                    goto xvid_err_memory5;
401    
402                    }
403            }
404    
405            pEnc->queue_head = 0;
406            pEnc->queue_tail = 0;
407            pEnc->queue_size = 0;
408    
409            pEnc->mbParam.m_stamp = 0;
410    
411            pEnc->m_framenum = 0;
412            pEnc->current->stamp = 0;
413            pEnc->reference->stamp = 0;
414    
415          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
416    
417          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
418          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
419                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
420                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
421                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
422                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
423          }          }
424    
425          init_timer();          init_timer();
# Line 370  Line 428 
428    
429          /*          /*
430           * 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  
431           */           */
432  #ifdef BFRAMES  
433      xvid_err_memory5:
434    
435    
436            if (pEnc->mbParam.max_bframes > 0) {
437    
438                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
439                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
440                                                      pEnc->mbParam.edged_height);
441                    }
442                    xvid_free(pEnc->queue);
443            }
444    
445   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
446    
447                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
448    
449                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
450                                pEnc->mbParam.edged_width,  
451                            if (pEnc->bframes[i] == NULL)
452                                    continue;
453    
454                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
455                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
456    
457                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 392  Line 461 
461          }          }
462    
463          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
464            }
 #endif  
465    
466   xvid_err_memory3:   xvid_err_memory3:
467  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
468          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
469                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
470  #endif  #endif
471    
472  #ifdef BFRAMES          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
         image_destroy(&pEnc->f_refh,  
                       pEnc->mbParam.edged_width,  
473                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
474          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
475                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
476          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
477                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
 #endif  
478    
479          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
480                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
481          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
482                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
483          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
484                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
485          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
486                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
487          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
488                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
489          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
490                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
491          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
492                        pEnc->mbParam.edged_width,                                    pEnc->mbParam.edged_height);
493    
494    /* destroy GMC image */
495            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
496                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
497    
498    
499   xvid_err_memory2:   xvid_err_memory2:
500          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
501          xvid_free(pEnc->reference->mbs);          xvid_free(pEnc->reference->mbs);
# Line 445  Line 505 
505          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
506          xvid_free(pEnc);          xvid_free(pEnc);
507    
508            pParam->handle = NULL;
509    
510          return XVID_ERR_MEMORY;          return XVID_ERR_MEMORY;
511  }  }
512    
513  int encoder_destroy(Encoder * pEnc)  /*****************************************************************************
514     * Encoder destruction
515     *
516     * This function destroy the entire encoder structure created by a previous
517     * successful encoder_create call.
518     *
519     * Returned values (for now only one returned value) :
520     *    - XVID_ERR_OK     - no errors
521     *
522     ****************************************************************************/
523    
524    int
525    encoder_destroy(Encoder * pEnc)
526  {  {
527            int i;
528    
529          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
530    
531          /* B Frames specific */          /* B Frames specific */
532  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
         int i;  
533    
534          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
         {  
535    
536                  if (pEnc->bframes[i] == NULL) continue;                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
537                                              pEnc->mbParam.edged_height);
538                    }
539                    xvid_free(pEnc->queue);
540            }
541    
542                  image_destroy(&pEnc->bframes[i]->image,  
543                                pEnc->mbParam.edged_width,          if (pEnc->mbParam.max_bframes > 0) {
544    
545                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
546    
547                            if (pEnc->bframes[i] == NULL)
548                                    continue;
549    
550                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
551                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
552    
553                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
554    
555                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
556          }          }
557    
558          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
559    
560  #endif          }
561    
562          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
563    
564          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
565                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
566          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
567                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
568          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
569                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
570          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
571                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
572          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
573                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
574          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
575                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
576          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
577                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
578  #ifdef BFRAMES  
579          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
580                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
581          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
582                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
583          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
584                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
585  #endif  
586  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
587          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
588                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
589  #endif  #endif
590    
# Line 529  Line 602 
602  }  }
603    
604    
605    static __inline void inc_frame_num(Encoder * pEnc)
606    {
607            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
608            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
609    }
610    
611  // ==================================================================  
612  #ifdef BFRAMES  static __inline void
613  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
614    {
615            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
616            {
617                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
618                    return;
619            }
620    
621            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
622                                    pEnc->bframenum_head, pEnc->bframenum_tail,
623                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
624    
625    
626            start_timer();
627            if (image_input
628                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
629                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
630                    return;
631            stop_conv_timer();
632    
633            pEnc->queue_size++;
634            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
635    }
636    
637    static __inline void
638    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
639    {
640    
641                    pCur->ticks = (int32_t)pCur->stamp % time_base;
642                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
643    
644                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
645    
646    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
647                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
648                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
649                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
650                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
651                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
652    
653    */
654    }
655    
656    
657    
658    /* convert pFrame->intra to coding_type */
659    static int intra2coding_type(int intra)
660    {
661            if (intra < 0)          return -1;
662            if (intra == 1)         return I_VOP;
663            if (intra == 2)         return B_VOP;
664    
665            return P_VOP;
666    }
667    
668    
669    
670    /*****************************************************************************
671     * IPB frame encoder entry point
672     *
673     * Returned values :
674     *    - XVID_ERR_OK     - no errors
675     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
676     *                        format
677     ****************************************************************************/
678    
679    int
680    encoder_encode_bframes(Encoder * pEnc,
681                               XVID_ENC_FRAME * pFrame,
682                               XVID_ENC_STATS * pResult)
683  {  {
684          uint16_t x, y;          uint16_t x, y;
685          Bitstream bs;          Bitstream bs;
686          uint32_t bits;          uint32_t bits;
687            int mode;
688    
689            int input_valid = 1;
690            int bframes_count = 0;
691    
692  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
693          float psnr;          float psnr;
694          char temp[128];          char temp[128];
695  #endif  #endif
696    
697          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
698          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
699            ENC_CHECK(pFrame->image);
700    
701          start_global_timer();          start_global_timer();
702    
703          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
704    
705  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ipvop_loop:
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
706    
707          if ((pFrame->image == NULL || pEnc->flush_bframes) &&          /*
708                  pEnc->bframenum_head < pEnc->bframenum_tail)           * bframe "flush" code
709          {           */
710    
711            if ((pFrame->image == NULL || pEnc->flush_bframes)
712                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
713    
714                    if (pEnc->flush_bframes == 0) {
715                            /*
716                             * we have reached the end of stream without getting
717                             * a future reference frame... so encode last final
718                             * frame as a pframe
719                             */
720    
721                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
722                                    pEnc->bframenum_head, pEnc->bframenum_tail,
723                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
724    
                 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) --- ");  
725                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
726                          SWAP(pEnc->current, pEnc->reference);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
727    
728                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
729    
730                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
731                            bframes_count = 0;
732    
733                          BitstreamPad(&bs);                          BitstreamPad(&bs);
734                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
735                          pFrame->intra = 0;                          pFrame->intra = 0;
736    
737                            emms();
738    
739                          return XVID_ERR_OK;                          return XVID_ERR_OK;
740                  }                  }
741    
742                  //dprintf("--- BFRAME (flush) --- ");  
743                  FrameCodeB(pEnc,                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
744                          pEnc->bframes[pEnc->bframenum_head],                                  pEnc->bframenum_head, pEnc->bframenum_tail,
745                           &bs, &bits);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
746    
747                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
748                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
749    
750                    BitstreamPad(&bs);
751                    pFrame->length = BitstreamLength(&bs);
752                    pFrame->intra = 2;
753    
754                    if (input_valid)
755                            queue_image(pEnc, pFrame);
756    
757                    emms();
758    
759                    return XVID_ERR_OK;
760            }
761    
762            if (pEnc->bframenum_head > 0) {
763                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
764    
765                    /* write an empty marker to the bitstream.
766    
767                       for divx5 decoder compatibility, this marker must consist
768                       of a not-coded p-vop, with a time_base of zero, and time_increment
769                       indentical to the future-referece frame.
770                    */
771    
772                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
773                            int tmp;
774    
775                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
776                                    pEnc->bframenum_head, pEnc->bframenum_tail,
777                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
778    
779                  BitstreamPad(&bs);                  BitstreamPad(&bs);
780    
781                            tmp = pEnc->current->seconds;
782                            pEnc->current->seconds = 0; /* force time_base = 0 */
783                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
784                            pEnc->current->seconds = tmp;
785    
786                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
787                  pFrame->input_consumed = 0;                          pFrame->intra = 4;
788                  pFrame->intra = 0;  
789                            if (input_valid)
790                                    queue_image(pEnc, pFrame);
791    
792                            emms();
793    
794                  return XVID_ERR_OK;                  return XVID_ERR_OK;
795          }          }
796            }
797    
798          if (pFrame->image == NULL)  
799    bvop_loop:
800    
801            if (pEnc->bframenum_dx50bvop != -1)
802          {          {
803                  pFrame->length = 0;  
804                  pFrame->input_consumed = 1;                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
805                  pFrame->intra = 0;                  SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
806                  return XVID_ERR_OK;  
807                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
808                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
809          }          }
810    
811          if (pEnc->bframenum_head > 0)                  if (input_valid)
812          {          {
813                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                          queue_image(pEnc, pFrame);
814                            input_valid = 0;
815          }          }
816    
817          pEnc->flush_bframes = 0;          } else if (input_valid) {
818    
819                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
820    
821                    start_timer();
822                    if (image_input
823                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
824                            pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
825                    {
826                            emms();
827                            return XVID_ERR_FORMAT;
828                    }
829                    stop_conv_timer();
830    
831                    // queue input frame, and dequue next image
832                    if (pEnc->queue_size > 0)
833                    {
834                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
835                            if (pEnc->queue_head != pEnc->queue_tail)
836                            {
837                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
838                            }
839                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
840                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
841                    }
842    
843  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          } else if (pEnc->queue_size > 0) {
844    
845          SWAP(pEnc->current, pEnc->reference);                  SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
846    
847          pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant;                  image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
848                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
849                    pEnc->queue_size--;
850    
851          if(pEnc->current->quant < 1)          } else {
852    
853                    /* if nothing was encoded, write an 'ignore this frame' flag
854                       to the bitstream */
855    
856                    if (BitstreamPos(&bs) == 0) {
857    
858                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
859                                    pEnc->bframenum_head, pEnc->bframenum_tail,
860                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
861    
862                            BitstreamPutBits(&bs, 0x7f, 8);
863                            pFrame->intra = 5;
864                    }
865    
866                    pFrame->length = BitstreamLength(&bs);
867                    emms();
868                    return XVID_ERR_OK;
869            }
870    
871            pEnc->flush_bframes = 0;
872    
873            emms();
874    
875            // only inc frame num, adapt quant, etc. if we havent seen it before
876            if (pEnc->bframenum_dx50bvop < 0 )
877            {
878                    mode = intra2coding_type(pFrame->intra);
879                    if (pFrame->quant == 0)
880                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
881                    else
882                            pEnc->current->quant = pFrame->quant;
883    
884    /*              if (pEnc->current->quant < 1)
885                  pEnc->current->quant = 1;                  pEnc->current->quant = 1;
886    
887          if(pEnc->current->quant > 31)          if(pEnc->current->quant > 31)
888                  pEnc->current->quant = 31;                  pEnc->current->quant = 31;
889    */
890          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
891          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
892          pEnc->current->seconds = pEnc->mbParam.m_seconds;  
893          pEnc->current->ticks = pEnc->mbParam.m_ticks;                  /* ToDo : dynamic fcode (in both directions) */
         //@@@ TODO: dyanmic fcode (in both directions)  
894          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
895          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
896    
897          start_timer();                  inc_frame_num(pEnc);
         if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  
                         pFrame->image, pFrame->colorspace))  
         {  
                 return XVID_ERR_FORMAT;  
         }  
         stop_conv_timer();  
898    
899  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
900          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);                  image_copy(&pEnc->sOriginal, &pEnc->current->image,
901                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
902  #endif  #endif
903    
904                    emms();
905    
906                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
907                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
908                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
909                    }
910    
911            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912             * Luminance masking
913             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
914    
915  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
916  // lumi masking                          int *temp_dquants =
917  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                                  (int *) xvid_malloc(pEnc->mbParam.mb_width *
918                                                                    pEnc->mbParam.mb_height * sizeof(int),
919          if ((pEnc->current->global_flags & XVID_LUMIMASKING))                                                                  CACHE_LINE);
920          {  
921                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);                          pEnc->current->quant =
922                                    adaptive_quantization(pEnc->current->image.y,
923                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                                                                    pEnc->mbParam.edged_width, temp_dquants,
924                                                              pEnc->mbParam.edged_width,  // stride                                                                    pEnc->current->quant, pEnc->current->quant,
925                                                              temp_dquants,                                                                    2 * pEnc->current->quant,
                                                             pEnc->current->quant,  
                                                             pEnc->current->quant,       // min_quant  
                                                             2*pEnc->current->quant,     // max_quant  
926                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
927                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
928    
929                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
930                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
931                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
932                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
933                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
934                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
935    
936                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
937                                    }
938    
939    #undef OFFSET
940                          }                          }
941    
942                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
943          }          }
944    
945            }
946    
947  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
948  // ivop/pvop/bvop selection           * ivop/pvop/bvop selection
949  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
950            pEnc->iFrameNum++;
         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 ---");  
951    
952                  FrameCodeI(pEnc, &bs, &bits);          if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
953                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
954                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
955            {
956                    mode = I_VOP;
957            }else{
958                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
959                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
960                                            (mode < 0) ? pEnc->iFrameNum : 0,
961                                            bframes_count++);
962            }
963    
964            if (mode == I_VOP) {
965                    /*
966                     * This will be coded as an Intra Frame
967                     */
968                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
969                            pEnc->mbParam.m_quarterpel = 1;
970                    else
971                            pEnc->mbParam.m_quarterpel = 0;
972    
973                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
974    
975                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
976                            if (pFrame->quant_intra_matrix != NULL)
977                                    set_intra_matrix(pFrame->quant_intra_matrix);
978                            if (pFrame->quant_inter_matrix != NULL)
979                                    set_inter_matrix(pFrame->quant_inter_matrix);
980                    }
981    
982    
983                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
984                                    pEnc->bframenum_head, pEnc->bframenum_tail,
985                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
986    
987                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
988                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
989                    }
990    
991                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
992    
993                    if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
994    
995                            pEnc->bframenum_tail--;
996                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
997    
998                            SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
999                            if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1000                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1001                            }
1002                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
1003                            bframes_count = 0;
1004    
1005                            pFrame->intra = 0;
1006    
1007                    } else {
1008    
1009                            FrameCodeI(pEnc, &bs, &bits);
1010                            bframes_count = 0;
1011                  pFrame->intra = 1;                  pFrame->intra = 1;
1012    
1013                            pEnc->bframenum_dx50bvop = -1;
1014                    }
1015    
1016                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1017    
1018                  /* note: sequences like "IIBB" decode fine with msfdam but,                  if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1019                     go screwy with divx5.00 */                          BitstreamPadAlways(&bs);
1020                            input_valid = 0;
1021                            goto ipvop_loop;
1022                    }
1023    
1024                    /*
1025                     * NB : sequences like "IIBB" decode fine with msfdam but,
1026                     *      go screwy with divx 5.00
1027                     */
1028            } else if (mode == P_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1029                    /*
1030                     * This will be coded as a Predicted Frame
1031                     */
1032    
1033                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1034                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1035                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1036    
1037                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1038                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1039          }          }
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 //dprintf("--- PFRAME ---");  
1040    
1041                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
1042                    bframes_count = 0;
1043                  pFrame->intra = 0;                  pFrame->intra = 0;
1044                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
1045    
1046                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1047                            BitstreamPadAlways(&bs);
1048                            input_valid = 0;
1049                            goto ipvop_loop;
1050          }          }
         else  
         {  
                 //dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
1051    
1052                  if (pFrame->bquant < 1)          } else {        /* mode == B_VOP */
1053                  {                  /*
1054                          pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;                   * This will be coded as a Bidirectional Frame
1055                     */
1056    
1057                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1058                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1059                  }                  }
1060                  else  
1061                  {                  if (pFrame->bquant < 1) {
1062                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1063                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1064    
1065                    } else {
1066                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1067                  }                  }
1068    
1069                  // store frame into bframe buffer & swap ref back to current                  if (pEnc->current->quant < 1)
1070                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                          pEnc->current->quant = 1;
1071                  SWAP(pEnc->current, pEnc->reference);                  else if (pEnc->current->quant > 31)
1072                pEnc->current->quant = 31;
1073    
1074                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1075                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1076                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1077    
1078                    /* store frame into bframe buffer & swap ref back to current */
1079                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1080                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1081    
1082                  pEnc->bframenum_tail++;                  pEnc->bframenum_tail++;
1083    
1084                  pFrame->intra = 0;  // bframe report by koepi
1085                    pFrame->intra = 2;
1086                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1087    
1088                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1089                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
                 return XVID_ERR_OK;  
1090          }          }
1091    
1092          BitstreamPad(&bs);          BitstreamPad(&bs);
1093          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1094    
1095          if (pResult)          if (pResult) {
         {  
1096                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1097                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1098                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1099                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1100                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1101          }          }
1102    
1103  #ifdef _DEBUG          emms();
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
1104    
1105          sprintf(temp, "PSNR: %f\n", psnr);  #ifdef _DEBUG_PSNR
1106          DEBUG(temp);          psnr =
1107  #endif                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1108                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1109                                       pEnc->mbParam.height);
1110    
1111          if (pFrame->quant == 0)          printf("PSNR: %f\n", psnr);
1112          {  //      DEBUG(temp);
1113                  RateControlUpdate(&pEnc->rate_control,  #endif
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1114    
1115          pEnc->iFrameNum++;          if (pFrame->quant == 0) {
1116          pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1117          if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                                                    pFrame->length, pFrame->intra);
         {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
1118          }          }
         pFrame->input_consumed = 1;  
1119    
1120          stop_global_timer();          stop_global_timer();
1121          write_timer();          write_timer();
1122    
1123            emms();
1124          return XVID_ERR_OK;          return XVID_ERR_OK;
1125  }  }
1126  // ==================================================================  
1127  #else  
1128  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1129    /*****************************************************************************
1130     * "original" IP frame encoder entry point
1131     *
1132     * Returned values :
1133     *    - XVID_ERR_OK     - no errors
1134     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1135     *                        format
1136     ****************************************************************************/
1137    
1138    int
1139    encoder_encode(Encoder * pEnc,
1140                               XVID_ENC_FRAME * pFrame,
1141                               XVID_ENC_STATS * pResult)
1142  {  {
1143          uint16_t x, y;          uint16_t x, y;
1144          Bitstream bs;          Bitstream bs;
1145          uint32_t bits;          uint32_t bits;
1146          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1147  #ifdef _DEBUG  
1148    #ifdef _DEBUG_PSNR
1149          float psnr;          float psnr;
1150          uint8_t temp[100];          uint8_t temp[128];
1151  #endif  #endif
1152    
1153          start_global_timer();          start_global_timer();
# Line 787  Line 1157 
1157          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1158          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1159    
1160          SWAP(pEnc->current, pEnc->reference);          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1161    
1162          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1163          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1164          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1165    
1166          start_timer();          inc_frame_num(pEnc);
1167          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  
1168                          pFrame->image, pFrame->colorspace))          /* disable alternate scan flag if interlacing is not enabled */
1169            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1170                    !(pEnc->current->global_flags & XVID_INTERLACING))
1171          {          {
1172                  return XVID_ERR_FORMAT;                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1173          }          }
1174    
1175            start_timer();
1176            if (image_input
1177                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1178                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1179                    return XVID_ERR_FORMAT;
1180          stop_conv_timer();          stop_conv_timer();
1181    
1182  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1183          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1184                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1185  #endif  #endif
1186    
1187          EMMS();          emms();
1188    
1189          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1190    
1191          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1192                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1193          }          } else {
         else  
         {  
1194                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1195          }          }
1196    
1197          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1198          {                  pEnc->mbParam.m_quarterpel = 1;
1199                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);          else
1200                    pEnc->mbParam.m_quarterpel = 0;
1201    
1202            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1203                    int *temp_dquants =
1204                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
1205                                                                    pEnc->mbParam.mb_height * sizeof(int),
1206                                                                    CACHE_LINE);
1207    
1208                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                  pEnc->current->quant =
1209                                                              pEnc->mbParam.edged_width,  // stride                          adaptive_quantization(pEnc->current->image.y,
1210                                                              temp_dquants,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1211                                                              pEnc->current->quant,                                                                    pEnc->current->quant, pEnc->current->quant,
1212                                                              pEnc->current->quant,       // min_quant                                                                    2 * pEnc->current->quant,
                                                             2*pEnc->current->quant,     // max_quant  
1213                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1214                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1215    
1216                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1217                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1218                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1219                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1220                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1221    
1222    
1223                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1224    
1225                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1226                          }                          }
1227    
1228    #undef OFFSET
1229                    }
1230    
1231                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1232          }          }
1233    
# Line 844  Line 1235 
1235                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1236                          write_vol_header = 1;                          write_vol_header = 1;
1237                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1238          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1239          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1240    
1241                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1242    
1243                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1244                          write_vol_header = 1;                          write_vol_header = 1;
# Line 857  Line 1247 
1247    
1248                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1249                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1250                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1251                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1252                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1253                  }                  } else {
1254                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1255                          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());  
1256                  }                  }
1257                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1258                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1259          }          }
1260    
1261          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1262          {                  if ((pEnc->iFrameNum == 0)
1263                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1264                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
   
1265                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1266                  else                  } else {
1267                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1268          }          }
1269          else          } else {
1270          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1271                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1272                  else                  } else {
1273                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1274          }          }
1275    
1276            }
1277    
1278          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1279          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1280          BitstreamPad(&bs);          BitstreamPad(&bs);
1281          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1282    
1283          if (pResult)          if (pResult) {
         {  
1284                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1285                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1286                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->current->sStat.kblks;
1287                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->current->sStat.mblks;
1288                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->current->sStat.ublks;
1289          }          }
1290    
1291          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1292    
1293  #ifdef _DEBUG          if (pFrame->quant == 0) {
1294          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1295                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
1296            }
1297    #ifdef _DEBUG_PSNR
1298            psnr =
1299                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1300                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1301                                       pEnc->mbParam.height);
1302    
1303          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1304          DEBUG(temp);  //      DEBUG(temp);
1305  #endif  #endif
1306    
1307          pEnc->iFrameNum++;          pEnc->iFrameNum++;
# Line 925  Line 1311 
1311    
1312          return XVID_ERR_OK;          return XVID_ERR_OK;
1313  }  }
 #endif  
1314    
1315    
1316  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1317    CodeIntraMB(Encoder * pEnc,
1318                            MACROBLOCK * pMB)
1319    {
1320    
1321          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1322    
# Line 939  Line 1327 
1327          pMB->sad16 = 0;          pMB->sad16 = 0;
1328    
1329          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1330                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1331                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1332                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1333    
1334                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1335                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1336                            if (pEnc->current->quant < 1)
1337                                    pEnc->current->quant = 1;
1338                  }                  }
1339          }          }
1340    
# Line 956  Line 1345 
1345  #define FCODEBITS       3  #define FCODEBITS       3
1346  #define MODEBITS        5  #define MODEBITS        5
1347    
1348  void HintedMESet(Encoder * pEnc, int * intra)  void
1349    HintedMESet(Encoder * pEnc,
1350                            int *intra)
1351  {  {
1352          HINTINFO * hint;          HINTINFO * hint;
1353          Bitstream bs;          Bitstream bs;
# Line 965  Line 1356 
1356    
1357          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1358    
1359          if (hint->rawhints)          if (hint->rawhints) {
         {  
1360                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1361          }          } else {
         else  
         {  
1362                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1363                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1364          }          }
1365    
1366          if (*intra)          if (*intra) {
         {  
1367                  return;                  return;
1368          }          }
1369    
1370          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1371                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1372                                                                                                                                     FCODEBITS);
1373    
1374          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1375          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1376    
1377          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1378          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1379                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1380                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1381                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1382                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1383                          VECTOR pred[4];                          VECTOR pred;
1384                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1385                          int vec;                          int vec;
1386    
1387                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1388                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1389                                                                                                                                      MODEBITS);
1390    
1391                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1392                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1393    
1394                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1395                          {                                  tmp.x =
1396                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1397                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1398                                    tmp.y =
1399                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1400                                                                                                                                                      length);
1401                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1402                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1403    
1404                                  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);
1405    
1406                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1407                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1408                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1409                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1410                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
1411                                  }                                  }
1412                          }                          } else if (pMB->mode == MODE_INTER4V) {
1413                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1414                          {                                          tmp.x =
1415                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1416                                  {                                                  x : BitstreamGetBits(&bs, length);
1417                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1418                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1419                                                    y : BitstreamGetBits(&bs, length);
1420                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1421                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1422    
1423                                          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);
1424    
1425                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1426                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1427                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1428                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
                                 }  
1429                          }                          }
1430                          else    // intra / stuffing / not_coded                          } else                          // intra / stuffing / not_coded
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1431                                  {                                  {
1432                                    for (vec = 0; vec < 4; ++vec) {
1433                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1434                                  }                                  }
1435                          }                          }
1436    
1437                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1438                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1439                          {                                  && pMB->dquant != NO_CHANGE) {
1440                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1441    
1442                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1443                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1444                                  }                                  }
1445                          }                          }
# Line 1058  Line 1448 
1448  }  }
1449    
1450    
1451  void HintedMEGet(Encoder * pEnc, int intra)  void
1452    HintedMEGet(Encoder * pEnc,
1453                            int intra)
1454  {  {
1455          HINTINFO * hint;          HINTINFO * hint;
1456          Bitstream bs;          Bitstream bs;
# Line 1067  Line 1459 
1459    
1460          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1461    
1462          if (hint->rawhints)          if (hint->rawhints) {
         {  
1463                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1464          }          } else {
         else  
         {  
1465                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1466                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1467          }          }
1468    
1469          if (intra)          if (intra) {
1470          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1471                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1472                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1473                  }                  }
# Line 1090  Line 1477 
1477          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1478          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1479    
1480          if (hint->rawhints)          if (hint->rawhints) {
         {  
1481                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1482          }          } else {
         else  
         {  
1483                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1484          }          }
1485    
1486          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1487          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1488                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1489                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1490                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1491                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1492                          VECTOR tmp;                          VECTOR tmp;
1493    
1494                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1495                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1496                          }                          } else {
                         else  
                         {  
1497                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1498                          }                          }
1499    
1500                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1501                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1502                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1503                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1504                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1505    
1506                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1507                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1508                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1509                                  }                                  } else {
                                 else  
                                 {  
1510                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1511                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1512                                  }                                  }
1513                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1514                                  int vec;                                  int vec;
1515    
1516                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1517                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1518                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1519                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1520                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1521    
1522                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1523                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1524                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1525                                          }                                          } else {
                                         else  
                                         {  
1526                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1527                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1528                                          }                                          }
# Line 1160  Line 1531 
1531                  }                  }
1532          }          }
1533    
1534          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1535                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1536                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1537          }          }
1538  }  }
1539    
1540    
1541  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1542    FrameCodeI(Encoder * pEnc,
1543                       Bitstream * bs,
1544                       uint32_t * pBits)
1545  {  {
1546            int mb_width = pEnc->mbParam.mb_width;
1547            int mb_height = pEnc->mbParam.mb_height;
1548    
1549          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1550          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1551    
1552          uint16_t x, y;          uint16_t x, y;
1553    
1554            if ((pEnc->current->global_flags & XVID_REDUCED))
1555            {
1556                    mb_width = (pEnc->mbParam.width + 31) / 32;
1557                    mb_height = (pEnc->mbParam.height + 31) / 32;
1558    
1559                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1560                    /* XXX: setedges is overkill */
1561                    start_timer();
1562                    image_setedges(&pEnc->current->image,
1563                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1564                            pEnc->mbParam.width, pEnc->mbParam.height);
1565                    stop_edges_timer();
1566            }
1567    
1568          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1569          pEnc->mbParam.m_rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1570          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1571            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1572          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1573    
1574          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1575    
1576          *pBits = BitstreamPos(bs);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1577            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1578    
1579          pEnc->sStat.iTextBits = 0;          *pBits = BitstreamPos(bs);
         pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;  
         pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1580    
1581          for (y = 0; y < pEnc->mbParam.mb_height; y++)          pEnc->current->sStat.iTextBits = 0;
1582                  for (x = 0; x < pEnc->mbParam.mb_width; x++)          pEnc->current->sStat.kblks = mb_width * mb_height;
1583                  {          pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1584                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1585            for (y = 0; y < mb_height; y++)
1586                    for (x = 0; x < mb_width; x++) {
1587                            MACROBLOCK *pMB =
1588                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1589    
1590                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1591    
1592                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1593                                                              dct_codes, qcoeff);
1594    
1595                          start_timer();                          start_timer();
1596                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1597                          stop_prediction_timer();                          stop_prediction_timer();
1598    
1599                          start_timer();                          start_timer();
1600                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1601                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1602                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1603                                    qcoeff[5*64+0]=0;
1604                            }
1605                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1606                          stop_coding_timer();                          stop_coding_timer();
1607                  }                  }
1608    
1609            if ((pEnc->current->global_flags & XVID_REDUCED))
1610            {
1611                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1612                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1613                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1614            }
1615          emms();          emms();
1616    
1617          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1618          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1619          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1620    
1621          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1622                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1623          }          }
1624    
# Line 1226  Line 1627 
1627    
1628    
1629  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1630    #define BFRAME_SKIP_THRESHHOLD 30
1631    
1632  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  
1633    /* FrameCodeP also handles S(GMC)-VOPs */
1634    static int
1635    FrameCodeP(Encoder * pEnc,
1636                       Bitstream * bs,
1637                       uint32_t * pBits,
1638                       bool force_inter,
1639                       bool vol_header)
1640  {  {
1641          float fSigma;          float fSigma;
1642    
1643          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1644          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1645    
1646            int mb_width = pEnc->mbParam.mb_width;
1647            int mb_height = pEnc->mbParam.mb_height;
1648    
1649          int iLimit;          int iLimit;
1650          uint32_t x, y;          int x, y, k;
1651          int iSearchRange;          int iSearchRange;
1652          int bIntra;          int bIntra, skip_possible;
1653    
1654          /* IMAGE *pCurrent = &pEnc->current->image; */          /* IMAGE *pCurrent = &pEnc->current->image; */
1655          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1656    
1657            if ((pEnc->current->global_flags & XVID_REDUCED))
1658            {
1659                    mb_width = (pEnc->mbParam.width + 31) / 32;
1660                    mb_height = (pEnc->mbParam.height + 31) / 32;
1661            }
1662    
1663    
1664          start_timer();          start_timer();
1665          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1666                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->current->global_flags & XVID_INTERLACING);  
1667          stop_edges_timer();          stop_edges_timer();
1668    
1669          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1670          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1671            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1672          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1673    
1674          if (!force_inter)          if (!force_inter)
1675                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1676          else          else
1677                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1678    
1679          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1680                  start_timer();                  start_timer();
1681                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1682                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1683                                                      pEnc->mbParam.edged_height,
1684                                                      pEnc->mbParam.m_quarterpel,
1685                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1686                  stop_inter_timer();                  stop_inter_timer();
1687          }          }
1688    
1689            pEnc->current->coding_type = P_VOP;
1690    
1691          start_timer();          start_timer();
1692          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
         {  
1693                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
         }  
1694          else          else
1695          {                  bIntra =
1696                  bIntra = MotionEstimation(                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1697                          &pEnc->mbParam,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         pEnc->current,  
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1698                          iLimit);                          iLimit);
1699          }  
1700          stop_motion_timer();          stop_motion_timer();
1701    
1702          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1703    
1704            if ( ( pEnc->current->global_flags & XVID_GMC )
1705                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1706          {          {
1707                  return FrameCodeI(pEnc, bs, pBits);                  pEnc->current->coding_type = S_VOP;
         }  
1708    
1709          pEnc->current->coding_type = P_VOP;                  generate_GMCparameters( 2, 16, &pEnc->current->warp,
1710                                            pEnc->mbParam.width, pEnc->mbParam.height,
1711                                            &pEnc->current->gmc_data);
1712    
1713                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1714                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1715                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1716                                    pEnc->mbParam.m_fcode, 0, 0,
1717                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1718    
1719            }
1720    
1721          if(vol_header)          if(vol_header)
1722                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1723    
1724          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1725            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1726    
1727          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1728    
1729          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1730          pEnc->sStat.iMvSum = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1731          pEnc->sStat.iMvCount = 0;  
1732          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1733            for (y = 0; y < mb_height; y++) {
1734                    for (x = 0; x < mb_width; x++) {
1735                            MACROBLOCK *pMB =
1736                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1737    
1738    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1739    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1740    
         for(y = 0; y < pEnc->mbParam.mb_height; y++)  
         {  
                 for(x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1741    
1742                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1743    
1744                          if (!bIntra)                          if (bIntra) {
1745                                    CodeIntraMB(pEnc, pMB);
1746                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1747                                                                      dct_codes, qcoeff);
1748    
1749                                    start_timer();
1750                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1751                                    stop_prediction_timer();
1752    
1753                                    pEnc->current->sStat.kblks++;
1754    
1755                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1756                                    continue;
1757                            }
1758    
1759                            if (pEnc->current->coding_type == S_VOP) {
1760    
1761                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1762                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1763                                            pEnc->mbParam.edged_width,65536);
1764    
1765                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1766                                            if (pEnc->mbParam.m_quarterpel)
1767                                                    pMB->qmvs[0] = pMB->qmvs[1] =pMB->qmvs[2] =pMB->qmvs[3] = pMB->amv;
1768                                            else
1769                                                    pMB->mvs[0] = pMB->mvs[1] =pMB->mvs[2] =pMB->mvs[3] = pMB->amv;
1770    
1771                                            pMB->mode = MODE_INTER;
1772                                            pMB->mcsel = 1;
1773                                            pMB->sad16 = iSAD;
1774                                    } else {
1775                                            pMB->mcsel = 0;
1776                                    }
1777                            } else
1778                          {                          {
1779                                    pMB->mcsel = 0; /* just a precaution */
1780                            }
1781    
1782                                  start_timer();                                  start_timer();
1783                                  MBMotionCompensation(pMB,                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1784                                                       x, y,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1785                                                       &pEnc->reference->image,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
                                                      &pEnc->vInterH,  
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
1786                                                       &pEnc->current->image,                                                       &pEnc->current->image,
1787                                                       dct_codes,                                                                   dct_codes, pEnc->mbParam.width,
                                                      pEnc->mbParam.width,  
1788                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1789                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1790                                                                     pEnc->mbParam.m_quarterpel,
1791                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1792                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
1793    
1794                                  stop_comp_timer();                                  stop_comp_timer();
1795    
1796                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1797                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1798                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1799                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1800                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                          if (pEnc->current->quant > 31)
1801                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                  pEnc->current->quant = 31;
1802                                            else if (pEnc->current->quant < 1)
1803                                                    pEnc->current->quant = 1;
1804                                          }                                          }
1805                                  }                                  }
1806                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1807    
1808                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1809    
1810                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          if (pMB->mode != MODE_NOT_CODED)
1811                            {       pMB->cbp =
1812                                            MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1813                                                                              dct_codes, qcoeff);
1814                          }                          }
1815                          else  
1816                          {  
1817                                  CodeIntraMB(pEnc, pMB);                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1818                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1819                                       pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1820                                    pEnc->current->sStat.mblks++;
1821                            }  else {
1822                                    pEnc->current->sStat.ublks++;
1823                          }                          }
1824    
1825                          start_timer();                          start_timer();
                         MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);  
                         stop_prediction_timer();  
1826    
1827                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          /* Finished processing the MB, now check if to CODE or SKIP */
1828                          {  
1829                                  pEnc->sStat.kblks++;                          skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) &
1830                                                            (pMB->dquant == NO_CHANGE);
1831    
1832                            if (pEnc->current->coding_type == S_VOP)
1833                                    skip_possible &= (pMB->mcsel == 1);
1834                            else if (pEnc->current->coding_type == P_VOP) {
1835                                    if (pEnc->mbParam.m_quarterpel)
1836                                            skip_possible &= (pMB->qmvs[0].x == 0) & (pMB->qmvs[0].y == 0);
1837                                    else
1838                                            skip_possible &= (pMB->mvs[0].x == 0) & (pMB->mvs[0].y == 0);
1839                          }                          }
1840                          else if (pMB->cbp ||  
1841                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1842                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1843                                   pMB->mvs[2].x || pMB->mvs[2].y ||  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1844                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1845                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1846                                    {
1847                                            int bSkip = 1;
1848    
1849                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1850                          {                          {
1851                                  pEnc->sStat.mblks++;                                                  int iSAD;
1852                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1853                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1854                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1855                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1856                                                    {       bSkip = 0;
1857                                                            break;
1858                          }                          }
1859                          else                                          }
1860    
1861                                            if (!bSkip) {   /* no SKIP, but trivial block */
1862                                                    if(pEnc->mbParam.m_quarterpel) {
1863                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1864                                                            pMB->pmvs[0].x = - predMV.x;
1865                                                            pMB->pmvs[0].y = - predMV.y;
1866                                                    }
1867                                                    else {
1868                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1869                                                            pMB->pmvs[0].x = - predMV.x;
1870                                                            pMB->pmvs[0].y = - predMV.y;
1871                                                    }
1872                                                    pMB->mode = MODE_INTER;
1873                                                    pMB->cbp = 0;
1874                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1875                                                    stop_coding_timer();
1876                                                    continue;       /* next MB */
1877                                            }
1878                                    }
1879                                    /* do SKIP */
1880    
1881                                    pMB->mode = MODE_NOT_CODED;
1882                                    MBSkip(bs);
1883                                    stop_coding_timer();
1884                                    continue;       /* next MB */
1885                            }
1886                            /* ordinary case: normal coded INTER/INTER4V block */
1887    
1888                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1889                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1890                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1891                                    qcoeff[5*64+0]=0;
1892                            }
1893    
1894                            if(pEnc->mbParam.m_quarterpel) {
1895                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1896                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1897                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1898                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1899                            } else {
1900                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1901                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1902                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1903                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1904                            }
1905    
1906    
1907                            if (pMB->mode == MODE_INTER4V)
1908                            {       int k;
1909                                    for (k=1;k<4;k++)
1910                          {                          {
1911                                  pEnc->sStat.ublks++;                                          if(pEnc->mbParam.m_quarterpel) {
1912                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1913                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1914                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1915                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1916                                            } else {
1917                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1918                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1919                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1920                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1921                          }                          }
1922    
1923                          start_timer();                                  }
1924                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          }
1925                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1926                          stop_coding_timer();                          stop_coding_timer();
1927                  }                  }
1928          }          }
1929    
1930            if ((pEnc->current->global_flags & XVID_REDUCED))
1931            {
1932                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1933                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1934                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1935            }
1936    
1937          emms();          emms();
1938    
1939          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1940                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1941          }          }
1942    
1943          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
1944                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
1945    
1946          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
1947    
1948          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1949    
1950          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1951              && (pEnc->mbParam.m_fcode <= 3))    // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
1952          {          {
1953                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1954                  iSearchRange *= 2;                  iSearchRange *= 2;
1955          }          } else if ((fSigma < iSearchRange / 6)
1956          else if ((fSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma >= 0)
1957                   && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
1958                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
                  && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16  
1959          {          {
1960                  pEnc->mbParam.m_fcode--;                  pEnc->mbParam.m_fcode--;
1961                  iSearchRange /= 2;                  iSearchRange /= 2;
1962          }          }
1963    
1964          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
1965    
1966            /* frame drop code */
1967            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
1968            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
1969                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
1970            {
1971                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
1972                    pEnc->current->sStat.ublks = mb_width * mb_height;
1973    
1974                    BitstreamReset(bs);
1975    
1976                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1977                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1978    
1979                    // copy reference frame details into the current frame
1980                    pEnc->current->quant = pEnc->reference->quant;
1981                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1982                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1983                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
1984                    pEnc->current->fcode = pEnc->reference->fcode;
1985                    pEnc->current->bcode = pEnc->reference->bcode;
1986                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1987                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
1988            }
1989    
1990            /* XXX: debug
1991            {
1992                    char s[100];
1993                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
1994                    image_dump_yuvpgm(&pEnc->current->image,
1995                            pEnc->mbParam.edged_width,
1996                            pEnc->mbParam.width, pEnc->mbParam.height, s);
1997    
1998                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
1999                    image_dump_yuvpgm(&pEnc->reference->image,
2000                            pEnc->mbParam.edged_width,
2001                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2002            }
2003            */
2004    
2005    
2006          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2007    
# Line 1414  Line 2009 
2009  }  }
2010    
2011    
2012  #ifdef BFRAMES  static void
2013  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  FrameCodeB(Encoder * pEnc,
2014                       FRAMEINFO * frame,
2015                       Bitstream * bs,
2016                       uint32_t * pBits)
2017  {  {
2018      int16_t dct_codes[6*64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2019      int16_t qcoeff[6*64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2020      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2021    
2022      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
2023          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2024    
2025    #ifdef BFRAMES_DEC_DEBUG
2026            FILE *fp;
2027            static char first=0;
2028    #define BFRAME_DEBUG    if (!first && fp){ \
2029                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2030            }
2031    
2032            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2033    
2034            if (!first){
2035                    fp=fopen("C:\\XVIDDBGE.TXT","w");
2036            }
2037    #endif
2038    
2039            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2040    
2041          // forward          // forward
2042          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,
2043                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
2044                                       pEnc->mbParam.height);
2045          start_timer();          start_timer();
2046          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,
2047                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2048                                              pEnc->mbParam.m_quarterpel, 0);
2049          stop_inter_timer();          stop_inter_timer();
2050    
2051          // backward          // backward
2052          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,
2053                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
2054                                       pEnc->mbParam.height);
2055      start_timer();      start_timer();
2056          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2057                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2058                                              pEnc->mbParam.m_quarterpel, 0);
2059          stop_inter_timer();          stop_inter_timer();
2060    
2061          start_timer();          start_timer();
2062    
2063          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2064                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
2065                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
2066                            pEnc->reference->mbs, f_ref,
2067                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2068                                                     pEnc->current, b_ref, &pEnc->vInterH,
2069                                                     &pEnc->vInterV, &pEnc->vInterHV);
2070    
2071    
2072          stop_motion_timer();          stop_motion_timer();
# Line 1454  Line 2077 
2077          }*/          }*/
2078    
2079      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
     BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
2080    
2081      *pBits = BitstreamPos(bs);          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2082            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
     pEnc->sStat.iTextBits = 0;  
     pEnc->sStat.iMvSum = 0;  
     pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
2083    
2084            *pBits = BitstreamPos(bs);
2085    
2086      for (y = 0; y < pEnc->mbParam.mb_height; y++)          frame->sStat.iTextBits = 0;
2087          {          frame->sStat.iMvSum = 0;
2088                  // reset prediction          frame->sStat.iMvCount = 0;
2089            frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2090    
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
2091    
2092                  for (x = 0; x < pEnc->mbParam.mb_width; x++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2093                  {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2094                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2095                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
2096    
2097                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2098                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
2099                          {                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
2100                                  continue;                                  continue;
2101                          }                          }
2102    
2103                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2104                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2105                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2106                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2107                                                                             &pEnc->vInterV, &pEnc->vInterHV,
2108                                          dct_codes);                                          dct_codes);
2109    
2110                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2111                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
2112    
2113                                    mb->cbp =
2114                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
2115    
2116                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2117                                  mb->cbp == 0 &&                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2118                                  mb->mvs[0].x == 0 &&                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)  
                         {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
2119                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)  
                         {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
2120                          }                          }
2121    
2122  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  #ifdef BFRAMES_DEC_DEBUG
2123            BFRAME_DEBUG
2124    #endif
2125                          start_timer();                          start_timer();
2126                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2127                                                     &frame->sStat, direction);
2128                          stop_coding_timer();                          stop_coding_timer();
2129                  }                  }
2130          }          }
# Line 1534  Line 2134 
2134          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
2135    
2136          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2137    
2138    #ifdef BFRAMES_DEC_DEBUG
2139            if (!first){
2140                    first=1;
2141                    if (fp)
2142                            fclose(fp);
2143  }  }
2144  #endif  #endif
2145    }

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

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