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

Legend:
Removed from v.1.34  
changed lines
  Added in v.1.76.2.1

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