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

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.76.2.15

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