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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.76.2.2

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