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

Diff of /xvidcore/src/encoder.c

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

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

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

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