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

Legend:
Removed from v.1.37  
changed lines
  Added in v.1.76.2.9

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