[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

trunk/xvidcore/src/encoder.c revision 189, Fri Jun 7 10:36:08 2002 UTC branches/dev-api-3/xvidcore/src/encoder.c revision 573, Mon Sep 30 14:16:02 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;   *  08.05.2002 fix some problem in DEBUG mode;
39   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
40   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
41   *   *
42   *  $Id: encoder.c,v 1.38 2002-06-07 10:36:08 edgomez Exp $   *  $Id: encoder.c,v 1.76.2.8 2002-09-30 14:16:02 chl Exp $
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 61  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 100  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 122  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 141  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 170  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 181  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 223  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                     pEnc->mbParam.edged_height) < 0)
287                    goto xvid_err_memory3;
288  #endif  #endif
 #ifdef BFRAMES  
                 image_create(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
 #endif  
                 image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  
         {  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
 #ifdef BFRAMES  
                 image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
                 image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
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    
 // ==============================================================================  
 #ifdef BFRAMES  
332    
333          // TODO: handle malloc() == NULL  
334            /* B Frames specific init */
335    
336            pEnc->global = pParam->global;
337          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
338          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
339          if (pEnc->mbParam.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->mbParam.max_bframes * sizeof(FRAMEINFO *));                  pEnc->bframes =
346                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
347                                                    CACHE_LINE);
348    
349                    if (pEnc->bframes == NULL)
350                            goto xvid_err_memory3;
351    
352                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)
353                  {                          pEnc->bframes[n] = NULL;
354                          pEnc->bframes[n] = malloc(sizeof(FRAMEINFO));  
355                          pEnc->bframes[n]->mbs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);  
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            pEnc->mbParam.m_stamp = 0;
414    
415            pEnc->m_framenum = 0;
416            pEnc->current->stamp = 0;
417            pEnc->reference->stamp = 0;
418    
419          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
420    
421          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
422          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
423                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
424                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
425                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
426                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
427          }          }
428    
429          init_timer();          init_timer();
430    
431          return XVID_ERR_OK;          return XVID_ERR_OK;
432    
433            /*
434             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
435             */
436    
437      xvid_err_memory5:
438    
439    
440            if (pEnc->mbParam.max_bframes > 0) {
441    
442                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
443                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
444                                                      pEnc->mbParam.edged_height);
445  }  }
446                    xvid_free(pEnc->queue);
447            }
448    
449      xvid_err_memory4:
450    
451            if (pEnc->mbParam.max_bframes > 0) {
452    
453                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
454    
455                            if (pEnc->bframes[i] == NULL)
456                                    continue;
457    
458                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
459                                                      pEnc->mbParam.edged_height);
460    
461                            xvid_free(pEnc->bframes[i]->mbs);
462    
463                            xvid_free(pEnc->bframes[i]);
464    
465                    }
466    
467                    xvid_free(pEnc->bframes);
468            }
469    
470      xvid_err_memory3:
471    #ifdef _DEBUG_PSNR
472            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
473                                      pEnc->mbParam.edged_height);
474    #endif
475    
476  int encoder_destroy(Encoder * pEnc)          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
477                                      pEnc->mbParam.edged_height);
478            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
479                                      pEnc->mbParam.edged_height);
480            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482    
483            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
484                                      pEnc->mbParam.edged_height);
485            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
486                                      pEnc->mbParam.edged_height);
487            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
488                                      pEnc->mbParam.edged_height);
489            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
490                                      pEnc->mbParam.edged_height);
491            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
492                                      pEnc->mbParam.edged_height);
493            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
494                                      pEnc->mbParam.edged_height);
495            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
496                                      pEnc->mbParam.edged_height);
497    
498      xvid_err_memory2:
499            xvid_free(pEnc->current->mbs);
500            xvid_free(pEnc->reference->mbs);
501    
502      xvid_err_memory1:
503            xvid_free(pEnc->current);
504            xvid_free(pEnc->reference);
505            xvid_free(pEnc);
506    
507            pParam->handle = NULL;
508    
509            return XVID_ERR_MEMORY;
510    }
511    
512    /*****************************************************************************
513     * Encoder destruction
514     *
515     * This function destroy the entire encoder structure created by a previous
516     * successful encoder_create call.
517     *
518     * Returned values (for now only one returned value) :
519     *    - XVID_ERR_OK     - no errors
520     *
521     ****************************************************************************/
522    
523    int
524    encoder_destroy(Encoder * pEnc)
525  {  {
526            int i;
527    
528          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
529    
530  // =================================================================          /* B Frames specific */
531  #ifdef BFRAMES          if (pEnc->mbParam.max_bframes > 0) {
532          if (pEnc->mbParam.max_bframes > 0)  
533          {                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
534                  int n;  
535                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
536                  {                                            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]);  
537                  }                  }
538                  free(pEnc->bframes);                  xvid_free(pEnc->queue);
539          }          }
 #endif  
 //====================================================================  
540    
541          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
542          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (pEnc->mbParam.max_bframes > 0) {
543          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
544          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
545          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
546          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          if (pEnc->bframes[i] == NULL)
547          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                  continue;
548  #ifdef BFRAMES  
549          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
550          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
551          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
552  #endif                          xvid_free(pEnc->bframes[i]->mbs);
553  #ifdef _DEBUG  
554                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          xvid_free(pEnc->bframes[i]);
555                    }
556    
557                    xvid_free(pEnc->bframes);
558    
559            }
560    
561            /* All images, reference, current etc ... */
562    
563            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
564                                      pEnc->mbParam.edged_height);
565            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
566                                      pEnc->mbParam.edged_height);
567            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
568                                      pEnc->mbParam.edged_height);
569            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
570                                      pEnc->mbParam.edged_height);
571            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
572                                      pEnc->mbParam.edged_height);
573            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
574                                      pEnc->mbParam.edged_height);
575            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
576                                      pEnc->mbParam.edged_height);
577    
578            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
579                                      pEnc->mbParam.edged_height);
580            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
581                                      pEnc->mbParam.edged_height);
582            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
583                                      pEnc->mbParam.edged_height);
584    
585    #ifdef _DEBUG_PSNR
586            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
587                                      pEnc->mbParam.edged_height);
588  #endif  #endif
589    
590            /* Encoder structure */
591    
592          xvid_free(pEnc->current->mbs);          xvid_free(pEnc->current->mbs);
593          xvid_free(pEnc->current);          xvid_free(pEnc->current);
594    
# Line 359  Line 596 
596          xvid_free(pEnc->reference);          xvid_free(pEnc->reference);
597    
598          xvid_free(pEnc);          xvid_free(pEnc);
599    
600          return XVID_ERR_OK;          return XVID_ERR_OK;
601  }  }
602    
603    
604    static __inline void inc_frame_num(Encoder * pEnc)
605    {
606            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
607            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
608    }
609    
610  // ==================================================================  
611  #ifdef BFRAMES  static __inline void
612  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
613    {
614            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
615            {
616                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
617                    return;
618            }
619    
620            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
621                                    pEnc->bframenum_head, pEnc->bframenum_tail,
622                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
623    
624    
625            start_timer();
626            if (image_input
627                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
628                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
629                    return;
630            stop_conv_timer();
631    
632            pEnc->queue_size++;
633            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
634    }
635    
636    static __inline void
637    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
638    {
639    
640                    pCur->ticks = (int32_t)pCur->stamp % time_base;
641                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
642    
643                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
644    
645    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
646                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
647                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
648                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
649                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
650                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
651    
652    */
653    }
654    
655    
656    
657    
658    
659    /*****************************************************************************
660     * IPB frame encoder entry point
661     *
662     * Returned values :
663     *    - XVID_ERR_OK     - no errors
664     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
665     *                        format
666     ****************************************************************************/
667    
668    int
669    encoder_encode_bframes(Encoder * pEnc,
670                               XVID_ENC_FRAME * pFrame,
671                               XVID_ENC_STATS * pResult)
672  {  {
673          uint16_t x, y;          uint16_t x, y;
674          Bitstream bs;          Bitstream bs;
675          uint32_t bits;          uint32_t bits, mode;
676    
677  #ifdef _DEBUG          int input_valid = 1;
678    
679    #ifdef _DEBUG_PSNR
680          float psnr;          float psnr;
681          char temp[128];          char temp[128];
682  #endif  #endif
683    
684          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
685          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
686            ENC_CHECK(pFrame->image);
687    
688          start_global_timer();          start_global_timer();
689    
690          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
691    
692  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  ipvop_loop:
 // bframe "flush" code  
 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
693    
694          if ((pFrame->image == NULL || pEnc->flush_bframes) &&          /*
695                  pEnc->bframenum_head < pEnc->bframenum_tail)           * bframe "flush" code
696          {           */
697    
698            if ((pFrame->image == NULL || pEnc->flush_bframes)
699                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
700    
701                    if (pEnc->flush_bframes == 0) {
702                            /*
703                             * we have reached the end of stream without getting
704                             * a future reference frame... so encode last final
705                             * frame as a pframe
706                             */
707    
708                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
709                                    pEnc->bframenum_head, pEnc->bframenum_tail,
710                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
711    
                 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) --- ");  
712                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
713                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
714    
# Line 406  Line 718 
718    
719                          BitstreamPad(&bs);                          BitstreamPad(&bs);
720                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
721                          pFrame->intra = 0;                          pFrame->intra = 0;
722    
723                          return XVID_ERR_OK;                          return XVID_ERR_OK;
724                  }                  }
725    
726                  dprintf("--- BFRAME (flush) --- ");  
727                  FrameCodeB(pEnc,                  DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
728                          pEnc->bframes[pEnc->bframenum_head],                                  pEnc->bframenum_head, pEnc->bframenum_tail,
729                           &bs, &bits);                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
730    
731                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
732                  pEnc->bframenum_head++;                  pEnc->bframenum_head++;
733    
734                    BitstreamPad(&bs);
735                    pFrame->length = BitstreamLength(&bs);
736                    pFrame->intra = 0;
737    
738                    if (input_valid)
739                            queue_image(pEnc, pFrame);
740    
741                    return XVID_ERR_OK;
742            }
743    
744            if (pEnc->bframenum_head > 0) {
745                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
746    
747                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
748    
749                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
750                                    pEnc->bframenum_head, pEnc->bframenum_tail,
751                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
752    
753                            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
754                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
755                  BitstreamPad(&bs);                  BitstreamPad(&bs);
756                            BitstreamPutBits(&bs, 0x7f, 8);
757    
758                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
759                  pFrame->intra = 0;                  pFrame->intra = 0;
760    
761                            if (input_valid)
762                                    queue_image(pEnc, pFrame);
763    
764                  return XVID_ERR_OK;                  return XVID_ERR_OK;
765          }          }
766            }
767    
768    
769    bvop_loop:
770    
771          if (pFrame->image == NULL)          if (pEnc->bframenum_dx50bvop != -1)
772          {          {
773                  pFrame->length = 0;  
774                  pFrame->input_consumed = 1;                  SWAP(pEnc->current, pEnc->reference);
775                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
776    
777                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
778                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
779                    }
780    
781                    if (input_valid)
782                    {
783                            queue_image(pEnc, pFrame);
784                            input_valid = 0;
785                    }
786    
787            } else if (input_valid) {
788    
789                    SWAP(pEnc->current, pEnc->reference);
790    
791                    start_timer();
792                    if (image_input
793                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
794                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
795                            return XVID_ERR_FORMAT;
796                    stop_conv_timer();
797    
798                    // queue input frame, and dequue next image
799                    if (pEnc->queue_size > 0)
800                    {
801                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
802                            if (pEnc->queue_head != pEnc->queue_tail)
803                            {
804                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
805                            }
806                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
807                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
808                    }
809    
810            } else if (pEnc->queue_size > 0) {
811    
812                    SWAP(pEnc->current, pEnc->reference);
813    
814                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
815                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
816                    pEnc->queue_size--;
817    
818            } else if (BitstreamPos(&bs) == 0) {
819    
820                    DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
821                                    pEnc->bframenum_head, pEnc->bframenum_tail,
822                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
823    
824                  pFrame->intra = 0;                  pFrame->intra = 0;
825    
826                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
827                    BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP
828                    BitstreamPad(&bs);
829                    pFrame->length = BitstreamLength(&bs);
830    
831                    return XVID_ERR_OK;
832    
833            } else {
834    
835                    pFrame->length = BitstreamLength(&bs);
836                  return XVID_ERR_OK;                  return XVID_ERR_OK;
837          }          }
838    
839          if (pEnc->bframenum_head > 0)          pEnc->flush_bframes = 0;
840    
841            emms();
842    
843            // only inc frame num, adapt quant, etc. if we havent seen it before
844            if (pEnc->bframenum_dx50bvop < 0 )
845          {          {
846                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                  if (pFrame->quant == 0)
847                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
848                    else
849                            pEnc->current->quant = pFrame->quant;
850    
851    /*              if (pEnc->current->quant < 1)
852                            pEnc->current->quant = 1;
853    
854                    if (pEnc->current->quant > 31)
855                            pEnc->current->quant = 31;
856    */
857                    pEnc->current->global_flags = pFrame->general;
858                    pEnc->current->motion_flags = pFrame->motion;
859    
860                    /* ToDo : dynamic fcode (in both directions) */
861                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
862                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
863    
864                    inc_frame_num(pEnc);
865    
866    #ifdef _DEBUG_PSNR
867                    image_copy(&pEnc->sOriginal, &pEnc->current->image,
868                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
869    #endif
870    
871                    emms();
872    
873                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
874                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
875                                    "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
876          }          }
877    
878          pEnc->flush_bframes = 0;          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
879             * Luminance masking
880             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
881    
882                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
883                            int *temp_dquants =
884                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
885                                                                    pEnc->mbParam.mb_height * sizeof(int),
886                                                                    CACHE_LINE);
887    
888                            pEnc->current->quant =
889                                    adaptive_quantization(pEnc->current->image.y,
890                                                                      pEnc->mbParam.edged_width, temp_dquants,
891                                                                      pEnc->current->quant, pEnc->current->quant,
892                                                                      2 * pEnc->current->quant,
893                                                                      pEnc->mbParam.mb_width,
894                                                                      pEnc->mbParam.mb_height);
895    
896                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
897    
898    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
899    
900                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
901                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
902    
903                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
904                                    }
905    
906    #undef OFFSET
907                            }
908    
909                            xvid_free(temp_dquants);
910                    }
911    
912  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          }
913    
914          SWAP(pEnc->current, pEnc->reference);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915             * ivop/pvop/bvop selection
916             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
917    
         pEnc->current->quant = (pFrame->quant == 0) ? RateControlGetQ(&pEnc->rate_control, 0) : pFrame->quant;  
918    
919          if(pEnc->current->quant < 1)          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 ||
920                  pEnc->current->quant = 1;                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
921                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
922                    || /*image_mad(&pEnc->reference->image, &pEnc->current->image,
923                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
924                                             pEnc->mbParam.height) > 30) {*/
925                            2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image,
926                                             &pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) {
927    
928          if(pEnc->current->quant > 31)                  /*
929                  pEnc->current->quant = 31;                   * This will be coded as an Intra Frame
930                     */
931    
932          pEnc->current->global_flags = pFrame->general;                  DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
933          pEnc->current->motion_flags = pFrame->motion;                                  pEnc->bframenum_head, pEnc->bframenum_tail,
934          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;  
935    
936          start_timer();                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
937          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;  
938          }          }
         stop_conv_timer();  
939    
940  #ifdef _DEBUG                  // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
         image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);  
 #endif  
941    
942                    if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
943    
944  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                          pEnc->bframenum_tail--;
945  // 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);  
946    
947                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
948                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
949                          {                                  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);  
950          }          }
951                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
952    
953                            pFrame->intra = 0;
954    
955  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                  } 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 ---");  
956    
957                  FrameCodeI(pEnc, &bs, &bits);                  FrameCodeI(pEnc, &bs, &bits);
   
958                  pFrame->intra = 1;                  pFrame->intra = 1;
959    
960                            pEnc->bframenum_dx50bvop = -1;
961                    }
962    
963                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
964    
965                  /* note: sequences like "IIBB" decode fine with msfdam but,                  if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
966                     go screwy with divx5.00 */                          BitstreamPad(&bs);
967                            input_valid = 0;
968                            goto ipvop_loop;
969                    }
970    
971                    /*
972                     * NB : sequences like "IIBB" decode fine with msfdam but,
973                     *      go screwy with divx 5.00
974                     */
975            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) {
976                    /*
977                     * This will be coded as a Predicted Frame
978                     */
979    
980                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
981                                    pEnc->bframenum_head, pEnc->bframenum_tail,
982                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
983    
984                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
985                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
986          }          }
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
                 dprintf("--- PFRAME ---");  
987    
988                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
989                  pFrame->intra = 0;                  pFrame->intra = 0;
990                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
991    
992                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
993                            BitstreamPad(&bs);
994                            input_valid = 0;
995                            goto ipvop_loop;
996          }          }
         else  
         {  
                 dprintf("--- BFRAME (store) ---  head=%i tail=%i", pEnc->bframenum_head, pEnc->bframenum_tail);  
997    
998                  if (pFrame->bquant < 1)          } else {
999                  {                  /*
1000                          pEnc->current->quant = ((pEnc->reference->quant + pEnc->current->quant) * pEnc->bquant_ratio) / 200;                   * This will be coded as a Bidirectional Frame
1001                     */
1002    
1003                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
1004                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1005                  }                  }
1006                  else  
1007                  {                  if (pFrame->bquant < 1) {
1008                            pEnc->current->quant =
1009                                    ((pEnc->reference->quant +
1010                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
1011                    } else {
1012                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
1013                  }                  }
1014                    if (pEnc->current->quant < 1)
1015                            pEnc->current->quant = 1;
1016    
1017                    if (pEnc->current->quant > 31)
1018                            pEnc->current->quant = 31;
1019    
1020                  // store frame into bframe buffer & swap ref back to current  
1021                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1022                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1023                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1024    
1025    
1026    
1027                    /* store frame into bframe buffer & swap ref back to current */
1028                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);                  SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1029                  SWAP(pEnc->current, pEnc->reference);                  SWAP(pEnc->current, pEnc->reference);
1030    
# Line 548  Line 1032 
1032    
1033                  pFrame->intra = 0;                  pFrame->intra = 0;
1034                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
1035    
1036                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  input_valid = 0;
1037                  if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)                  goto bvop_loop;
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
                 return XVID_ERR_OK;  
1038          }          }
1039    
1040            pEnc->iFrameNum++;
1041    
1042          BitstreamPad(&bs);          BitstreamPad(&bs);
1043          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1044    
1045          if (pResult)          if (pResult) {
         {  
1046                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1047                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1048                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 571  Line 1050 
1050                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1051          }          }
1052    
1053  #ifdef _DEBUG          emms();
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
1054    
1055          sprintf(temp, "PSNR: %f\n", psnr);  #ifdef _DEBUG_PSNR
1056            psnr =
1057                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1058                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1059                                       pEnc->mbParam.height);
1060    
1061            snprintf(temp, 127, "PSNR: %f\n", psnr);
1062          DEBUG(temp);          DEBUG(temp);
1063  #endif  #endif
1064    
1065          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1066          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1067                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
   
         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;  
1068          }          }
         pFrame->input_consumed = 1;  
1069    
1070          stop_global_timer();          stop_global_timer();
1071          write_timer();          write_timer();
1072    
1073          return XVID_ERR_OK;          return XVID_ERR_OK;
1074  }  }
1075  // ==================================================================  
1076  #else  
1077  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
1078    /*****************************************************************************
1079     * "original" IP frame encoder entry point
1080     *
1081     * Returned values :
1082     *    - XVID_ERR_OK     - no errors
1083     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1084     *                        format
1085     ****************************************************************************/
1086    
1087    int
1088    encoder_encode(Encoder * pEnc,
1089                               XVID_ENC_FRAME * pFrame,
1090                               XVID_ENC_STATS * pResult)
1091  {  {
1092          uint16_t x, y;          uint16_t x, y;
1093          Bitstream bs;          Bitstream bs;
1094          uint32_t bits;          uint32_t bits;
1095          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1096  #ifdef _DEBUG  
1097    #ifdef _DEBUG_PSNR
1098          float psnr;          float psnr;
1099          uint8_t temp[100];          uint8_t temp[128];
1100  #endif  #endif
1101    
1102          start_global_timer();          start_global_timer();
# Line 627  Line 1112 
1112          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1113          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1114    
1115          start_timer();          inc_frame_num(pEnc);
1116          if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  
1117                          pFrame->image, pFrame->colorspace))          /* disable alternate scan flag if interlacing is not enabled */
1118            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1119                    !(pEnc->current->global_flags & XVID_INTERLACING))
1120          {          {
1121                  return XVID_ERR_FORMAT;                  pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1122          }          }
1123    
1124            start_timer();
1125            if (image_input
1126                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1127                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
1128                    return XVID_ERR_FORMAT;
1129          stop_conv_timer();          stop_conv_timer();
1130    
1131  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1132          image_copy(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1133                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
1134  #endif  #endif
1135    
1136          EMMS();          emms();
1137    
1138          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1139    
1140          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1141                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1142          }          } else {
         else  
         {  
1143                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1144          }          }
1145    
1146          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1147          {                  int *temp_dquants =
1148                  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 *
1149                                                                    pEnc->mbParam.mb_height * sizeof(int),
1150                  pEnc->current->quant = adaptive_quantization(pEnc->current->image.y,                                                                  CACHE_LINE);
1151                                                              pEnc->mbParam.edged_width,  // stride  
1152                                                              temp_dquants,                  pEnc->current->quant =
1153                                                              pEnc->current->quant,                          adaptive_quantization(pEnc->current->image.y,
1154                                                              pEnc->current->quant,       // min_quant                                                                    pEnc->mbParam.edged_width, temp_dquants,
1155                                                              2*pEnc->current->quant,     // max_quant                                                                    pEnc->current->quant, pEnc->current->quant,
1156                                                                      2 * pEnc->current->quant,
1157                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1158                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1159    
1160                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1161                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1162                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1163                                  MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1164                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1165    
1166    
1167                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1168    
1169                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1170                            }
1171    
1172    #undef OFFSET
1173                          }                          }
1174    
1175                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1176          }          }
1177    
# Line 678  Line 1179 
1179                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1180                          write_vol_header = 1;                          write_vol_header = 1;
1181                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1182          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1183          else if(pEnc->current->global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1184    
1185                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1186    
1187                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)                  if(pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1188                          write_vol_header = 1;                          write_vol_header = 1;
# Line 691  Line 1191 
1191    
1192                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1193                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1194                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1195                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1196                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1197                  }                  } else {
1198                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1199                          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());  
1200                  }                  }
1201                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1202                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1203          }          }
1204    
1205          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1206          {                  if ((pEnc->iFrameNum == 0)
1207                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
1208                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
1209                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1210                  else                  } else {
1211                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1212          }          }
1213          else          } else {
1214          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1215                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1216                  else                  } else {
1217                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1218          }          }
1219    
1220            }
1221    
1222          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1223          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
1224          BitstreamPad(&bs);          BitstreamPad(&bs);
1225          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1226    
1227          if (pResult)          if (pResult) {
         {  
1228                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1229                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1230                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 734  Line 1232 
1232                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1233          }          }
1234    
1235          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1236    
1237  #ifdef _DEBUG          if (pFrame->quant == 0) {
1238          psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1239                                  pEnc->mbParam.width, pEnc->mbParam.height);                                                    pFrame->length, pFrame->intra);
1240            }
1241    #ifdef _DEBUG_PSNR
1242            psnr =
1243                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1244                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1245                                       pEnc->mbParam.height);
1246    
1247          sprintf(temp, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1248          DEBUG(temp);          DEBUG(temp);
1249  #endif  #endif
1250    
# Line 759  Line 1255 
1255    
1256          return XVID_ERR_OK;          return XVID_ERR_OK;
1257  }  }
 #endif  
1258    
1259    
1260  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1261    CodeIntraMB(Encoder * pEnc,
1262                            MACROBLOCK * pMB)
1263    {
1264    
1265          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1266    
# Line 773  Line 1271 
1271          pMB->sad16 = 0;          pMB->sad16 = 0;
1272    
1273          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1274                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1275                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1276                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1277    
1278                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1279                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1280                            if (pEnc->current->quant < 1)
1281                                    pEnc->current->quant = 1;
1282                  }                  }
1283          }          }
1284    
# Line 790  Line 1289 
1289  #define FCODEBITS       3  #define FCODEBITS       3
1290  #define MODEBITS        5  #define MODEBITS        5
1291    
1292  void HintedMESet(Encoder * pEnc, int * intra)  void
1293    HintedMESet(Encoder * pEnc,
1294                            int *intra)
1295  {  {
1296          HINTINFO * hint;          HINTINFO * hint;
1297          Bitstream bs;          Bitstream bs;
# Line 799  Line 1300 
1300    
1301          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1302    
1303          if (hint->rawhints)          if (hint->rawhints) {
         {  
1304                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1305          }          } else {
         else  
         {  
1306                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1307                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1308          }          }
1309    
1310          if (*intra)          if (*intra) {
         {  
1311                  return;                  return;
1312          }          }
1313    
1314          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1315                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1316                                                                                                                                     FCODEBITS);
1317    
1318          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1319          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1320    
1321          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1322          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1323                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1324                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1325                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1326                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1327                          VECTOR pred[4];                          VECTOR pred;
1328                          VECTOR tmp;                          VECTOR tmp;
                         int32_t dummy[4];  
1329                          int vec;                          int vec;
1330    
1331                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1332                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1333                                                                                                                                      MODEBITS);
1334    
1335                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1336                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1337    
1338                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1339                          {                                  tmp.x =
1340                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1341                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1342                                    tmp.y =
1343                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1344                                                                                                                                                      length);
1345                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1346                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1347    
1348                                  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);
1349    
1350                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1351                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1352                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1353                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1354                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
1355                          }                          }
1356                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
1357                          {                                  for (vec = 0; vec < 4; ++vec) {
1358                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
1359                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
1360                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
1361                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
1362                                                    (hint->rawhints) ? bhint->mvs[vec].
1363                                                    y : BitstreamGetBits(&bs, length);
1364                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1365                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1366    
1367                                          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);
1368    
1369                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1370                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1371                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1372                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1373                                  }                                  }
1374                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1375                                  {                                  {
1376                                    for (vec = 0; vec < 4; ++vec) {
1377                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1378                                  }                                  }
1379                          }                          }
1380    
1381                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1382                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1383                          {                                  && pMB->dquant != NO_CHANGE) {
1384                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1385    
1386                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1387                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1388                                  }                                  }
1389                          }                          }
# Line 892  Line 1392 
1392  }  }
1393    
1394    
1395  void HintedMEGet(Encoder * pEnc, int intra)  void
1396    HintedMEGet(Encoder * pEnc,
1397                            int intra)
1398  {  {
1399          HINTINFO * hint;          HINTINFO * hint;
1400          Bitstream bs;          Bitstream bs;
# Line 901  Line 1403 
1403    
1404          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1405    
1406          if (hint->rawhints)          if (hint->rawhints) {
         {  
1407                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1408          }          } else {
         else  
         {  
1409                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1410                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1411          }          }
1412    
1413          if (intra)          if (intra) {
1414          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1415                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1416                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1417                  }                  }
# Line 924  Line 1421 
1421          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1422          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1423    
1424          if (hint->rawhints)          if (hint->rawhints) {
         {  
1425                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1426          }          } else {
         else  
         {  
1427                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1428          }          }
1429    
1430          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1431          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1432                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1433                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1434                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1435                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1436                          VECTOR tmp;                          VECTOR tmp;
1437    
1438                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1439                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1440                          }                          } else {
                         else  
                         {  
1441                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1442                          }                          }
1443    
1444                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1445                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1446                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1447                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1448                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1449    
1450                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1451                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1452                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1453                                  }                                  } else {
                                 else  
                                 {  
1454                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1455                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1456                                  }                                  }
1457                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1458                                  int vec;                                  int vec;
1459    
1460                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1461                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1462                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1463                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1464                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1465    
1466                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1467                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1468                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1469                                          }                                          } else {
                                         else  
                                         {  
1470                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1471                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1472                                          }                                          }
# Line 994  Line 1475 
1475                  }                  }
1476          }          }
1477    
1478          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1479                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1480                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1481          }          }
1482  }  }
1483    
1484    
1485  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1486    FrameCodeI(Encoder * pEnc,
1487                       Bitstream * bs,
1488                       uint32_t * pBits)
1489  {  {
1490    
1491          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 1016  Line 1499 
1499          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1500    
1501          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1502          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1503    #define DIVX501B481P "DivX501b481p"
1504            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1505                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1506            }
1507    
1508            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1509            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1510    
1511          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1512    
# Line 1025  Line 1515 
1515          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1516    
1517          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1518                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1519                  {                          MACROBLOCK *pMB =
1520                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1521    
1522                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1523    
1524                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1525                                                              dct_codes, qcoeff);
1526    
1527                          start_timer();                          start_timer();
1528                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1529                          stop_prediction_timer();                          stop_prediction_timer();
1530    
1531                          start_timer();                          start_timer();
1532                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1533                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1534                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1535                                    qcoeff[5*64+0]=0;
1536                            }
1537                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1538                          stop_coding_timer();                          stop_coding_timer();
1539                  }                  }
# Line 1050  Line 1546 
1546          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1547          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1548    
1549          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1550                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1551          }          }
1552    
# Line 1060  Line 1555 
1555    
1556    
1557  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1558    #define BFRAME_SKIP_THRESHHOLD 30
1559    
1560  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1561    FrameCodeP(Encoder * pEnc,
1562                       Bitstream * bs,
1563                       uint32_t * pBits,
1564                       bool force_inter,
1565                       bool vol_header)
1566  {  {
1567          float fSigma;          float fSigma;
1568    
# Line 1069  Line 1570 
1570          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1571    
1572          int iLimit;          int iLimit;
1573          uint32_t x, y;          int x, y, k;
1574          int iSearchRange;          int iSearchRange;
1575          int bIntra;          int bIntra;
1576    
# Line 1077  Line 1578 
1578          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1579    
1580          start_timer();          start_timer();
1581          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1582                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->current->global_flags & XVID_INTERLACING);  
1583          stop_edges_timer();          stop_edges_timer();
1584    
1585          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
# Line 1090  Line 1587 
1587          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1588    
1589          if (!force_inter)          if (!force_inter)
1590                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1591                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1592                                       INTRA_THRESHOLD);
1593          else          else
1594                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1595    
1596          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1597                  start_timer();                  start_timer();
1598                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1599                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1600                                                      pEnc->mbParam.edged_height,
1601                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1602                  stop_inter_timer();                  stop_inter_timer();
1603          }          }
1604    
1605          start_timer();          start_timer();
1606          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1607                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1608          }                  if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference,
1609          else                                                                                          &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);
1610          {  
1611                  bIntra = MotionEstimation(          } else {
1612                          &pEnc->mbParam,  
1613                          pEnc->current,          bIntra =
1614                          pEnc->reference,                  MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1615                          &pEnc->vInterH,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1616                          iLimit);                          iLimit);
1617    
1618          }          }
1619          stop_motion_timer();          stop_motion_timer();
1620    
1621          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
         {  
                 return FrameCodeI(pEnc, bs, pBits);  
         }  
1622    
1623          pEnc->current->coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1624    
1625          if(vol_header)          if(vol_header)
1626                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1627    
1628          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  
1629            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1630            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1631    
1632          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1633    
1634          pEnc->sStat.iTextBits = 0;          pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount =
         pEnc->sStat.iMvSum = 0;  
         pEnc->sStat.iMvCount = 0;  
1635          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1636    
1637          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1638          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1639                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1640                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1641    
1642                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1643    
1644                          if (!bIntra)                          if (!bIntra) {
                         {  
1645                                  start_timer();                                  start_timer();
1646                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1647                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1648                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1649                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1650                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1651                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1652                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1168  Line 1656 
1656                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1657                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1658                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1659                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1660                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1661                                                    else if (pEnc->current->quant < 1)
1662                                                            pEnc->current->quant = 1;
1663                                          }                                          }
1664                                  }                                  }
1665                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1666    
1667                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1668    
1669                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  if (pMB->mode != MODE_NOT_CODED)
1670                          }                                          pMB->cbp =
1671                          else                                                  MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1672                          {                                                                                    dct_codes, qcoeff);
1673                            } else {
1674                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1675                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1676                                                                      dct_codes, qcoeff);
1677                          }                          }
1678    
1679                          start_timer();                          start_timer();
1680                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1681                          stop_prediction_timer();                          stop_prediction_timer();
1682    
1683                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1684                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1685                            } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1686                                               pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1687                                               pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1688                                    pEnc->sStat.mblks++;
1689                            }  else {
1690                                    pEnc->sStat.ublks++;
1691                          }                          }
1692                          else if (pMB->cbp ||  
1693                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          start_timer();
1694                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1695                                   pMB->mvs[2].x || pMB->mvs[2].y ||                          /* Finished processing the MB, now check if to CODE or SKIP */
1696                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1697                            if ((pMB->mode == MODE_NOT_CODED) ||
1698                                    (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1699                                    pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) {
1700    
1701    /* This is a candidate for SKIPping, but check intermediate B-frames first */
1702    
1703                                    int bSkip = 1;
1704                                    pMB->mode = MODE_NOT_CODED;
1705    
1706                                    for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1707                          {                          {
1708                                  pEnc->sStat.mblks++;                                          int iSAD;
1709                                            iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1710                                                                    pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1711                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1712                                            if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1713                                            {       bSkip = 0;
1714                                                    break;
1715                          }                          }
1716                          else                                  }
1717                                    if (!bSkip)
1718                          {                          {
1719                                  pEnc->sStat.ublks++;                                          VECTOR predMV;
1720                                            predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1721                                            pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y;
1722                                            pMB->mode = MODE_INTER;
1723                                            pMB->cbp = 0;
1724                                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1725                          }                          }
1726                                    else MBSkip(bs);
1727    
1728                          start_timer();                          } else {
1729                                    if (pEnc->current->global_flags & XVID_GREYSCALE)
1730                                    {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1731                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1732                                            qcoeff[5*64+0]=0;
1733                                    }
1734                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1735                            }
1736    
1737                          stop_coding_timer();                          stop_coding_timer();
1738                  }                  }
1739          }          }
1740    
1741          emms();          emms();
1742    
1743          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1744                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1745          }          }
1746    
# Line 1230  Line 1756 
1756          {          {
1757                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1758                  iSearchRange *= 2;                  iSearchRange *= 2;
1759          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1760                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1761                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1762                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1242  Line 1767 
1767    
1768          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->sStat.fMvPrevSigma = fSigma;
1769    
1770    #ifdef FRAMEDROP
1771            /* frame drop code */
1772            // DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks);
1773            if (pEnc->sStat.kblks + pEnc->sStat.mblks <
1774                    (pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100)
1775            {
1776                    pEnc->sStat.kblks = pEnc->sStat.mblks = 0;
1777                    pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;
1778    
1779                    BitstreamReset(bs);
1780    
1781                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1782                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
1783    
1784                    // copy reference frame details into the current frame
1785                    pEnc->current->quant = pEnc->reference->quant;
1786                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
1787                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
1788                    pEnc->current->fcode = pEnc->reference->fcode;
1789                    pEnc->current->bcode = pEnc->reference->bcode;
1790                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
1791                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);
1792    
1793            }
1794    #endif
1795    
1796          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1797    
1798          return 0;                                        // inter          return 0;                                        // inter
1799  }  }
1800    
1801    
1802  #ifdef BFRAMES  static __inline void
1803  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  FrameCodeB(Encoder * pEnc,
1804                       FRAMEINFO * frame,
1805                       Bitstream * bs,
1806                       uint32_t * pBits)
1807  {  {
1808      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1809      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
1810      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
1811    
1812      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
1813          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1814    
1815    #ifdef BFRAMES_DEC_DEBUG
1816            FILE *fp;
1817            static char first=0;
1818    #define BFRAME_DEBUG    if (!first && fp){ \
1819                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1820            }
1821    
1822            if (!first){
1823                    fp=fopen("C:\\XVIDDBGE.TXT","w");
1824            }
1825    #endif
1826    
1827          // forward          // forward
1828          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,
1829                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1830                                       pEnc->mbParam.height);
1831          start_timer();          start_timer();
1832          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,
1833                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1834                                              0);
1835          stop_inter_timer();          stop_inter_timer();
1836    
1837          // backward          // backward
1838          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,
1839                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1840                                       pEnc->mbParam.height);
1841      start_timer();      start_timer();
1842          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1843                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1844                                              0);
1845          stop_inter_timer();          stop_inter_timer();
1846    
1847          start_timer();          start_timer();
1848    
1849          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
1850                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
1851                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
1852                            pEnc->reference->mbs, f_ref,
1853                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1854                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1855                                                     &pEnc->vInterV, &pEnc->vInterHV);
1856    
1857    
1858          stop_motion_timer();          stop_motion_timer();
# Line 1288  Line 1863 
1863          }*/          }*/
1864    
1865      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1866      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);  
1867            set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
1868            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1869    
1870      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1871    
# Line 1298  Line 1875 
1875          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1876    
1877    
1878      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1879          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1880                  // reset prediction                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1881                            int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0;
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
   
                 for (x = 0; x < pEnc->mbParam.mb_width; x++)  
                 {  
                         MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
1882    
1883                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1884                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
1885                          {                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
1886                                  continue;                                  continue;
1887                          }                          }
1888    
1889                            if (mb->mode != MODE_DIRECT_NONE_MV) {
1890                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1891                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1892                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1893                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1894                                          dct_codes);                                          dct_codes);
1895    
1896                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
1897                          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);  
1898    
1899                                    mb->cbp =
1900                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
1901    
1902                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
1903                                  mb->cbp == 0 &&                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
1904                                  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;  
1905                          }                          }
   
                         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;  
1906                          }                          }
1907    
1908  //                      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
1909            BFRAME_DEBUG
1910    #endif
1911                          start_timer();                          start_timer();
1912                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1913                                                     &pEnc->sStat, direction);
1914                          stop_coding_timer();                          stop_coding_timer();
1915                  }                  }
1916          }          }
# Line 1368  Line 1920 
1920          // TODO: dynamic fcode/bcode ???          // TODO: dynamic fcode/bcode ???
1921    
1922          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1923    
1924    #ifdef BFRAMES_DEC_DEBUG
1925            if (!first){
1926                    first=1;
1927                    if (fp)
1928                            fclose(fp);
1929  }  }
1930  #endif  #endif
1931    }
1932    
1933    
1934    /*      in case internal output is needed somewhere... */
1935    /*      {
1936            FILE *filehandle;
1937            filehandle=fopen("last-b.pgm","wb");
1938            if (filehandle)
1939            {
1940                    fprintf(filehandle,"P5\n\n");           //
1941                    fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height);
1942                    fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle);
1943                    fclose(filehandle);
1944                    }
1945            }
1946    */

Legend:
Removed from v.189  
changed lines
  Added in v.573

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