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

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

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

revision 83, Fri Mar 29 04:25:16 2002 UTC revision 198, Thu Jun 13 11:42:15 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *  08.05.2002 fix some problem in DEBUG mode;
36     *             MinChen <chenm001@163.com>
37     *  14.04.2002 added FrameCodeB()
38     *
39     *  $Id: encoder.c,v 1.42 2002-06-13 11:42:15 edgomez Exp $
40     *
41     ****************************************************************************/
42    
43  #include <stdlib.h>  #include <stdlib.h>
44  #include <stdio.h>  #include <stdio.h>
45  #include <math.h>  #include <math.h>
46    #include <string.h>
47    
48  #include "encoder.h"  #include "encoder.h"
49  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
50  #include "global.h"  #include "global.h"
51  #include "utils/timer.h"  #include "utils/timer.h"
52  #include "image/image.h"  #include "image/image.h"
53    #include "motion/motion.h"
54  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
55  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
56  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 18  Line 62 
62  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
63  #include "utils/mem_align.h"  #include "utils/mem_align.h"
64    
65  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
66     * Local macros
67     ****************************************************************************/
68    
69    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
70    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
71    
72  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
73  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
74     ****************************************************************************/
75    
76    static int FrameCodeI(Encoder * pEnc,
77                                              Bitstream * bs,
78                                              uint32_t * pBits);
79    
80    static int FrameCodeP(Encoder * pEnc,
81                                              Bitstream * bs,
82                                              uint32_t * pBits,
83                                              bool force_inter,
84                                              bool vol_header);
85    
86    #ifdef BFRAMES
87    static void FrameCodeB(Encoder * pEnc,
88                                               FRAMEINFO * frame,
89                                               Bitstream * bs,
90                                               uint32_t * pBits);
91    #endif
92    
93    /*****************************************************************************
94     * Local data
95     ****************************************************************************/
96    
97  static int DQtab[4] =  static int DQtab[4] = {
 {  
98          -1, -2, 1, 2          -1, -2, 1, 2
99  };  };
100    
101  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
102          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
103  };  };
104    
105    
106  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
107    image_null(IMAGE * image)
108    {
109            image->y = image->u = image->v = NULL;
110    }
111    
112    
113    /*****************************************************************************
114     * Encoder creation
115     *
116     * This function creates an Encoder instance, it allocates all necessary
117     * image buffers (reference, current and bframes) and initialize the internal
118     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
119     *
120     * The code seems to be very long but is very basic, mainly memory allocation
121     * and cleaning code.
122     *
123     * Returned values :
124     *    - XVID_ERR_OK     - no errors
125     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
126     *                        cleans the structure before exiting.
127     *                        pParam->handle is also set to NULL.
128     *
129     ****************************************************************************/
130    
131    int
132    encoder_create(XVID_ENC_PARAM * pParam)
133  {  {
134          Encoder *pEnc;          Encoder *pEnc;
135          uint32_t i;          uint32_t i;
# Line 49  Line 143 
143          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
144          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
145    
146          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
147          {  
148            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
149                  pParam->fincr = 1;                  pParam->fincr = 1;
150                  pParam->fbase = 25;                  pParam->fbase = 25;
151          }          }
152    
153          // simplify the "fincr/fbase" fraction          /*
154          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
155             * (neccessary, since windows supplies us with huge numbers)
156             */
157    
158          i = pParam->fincr;          i = pParam->fincr;
159          while (i > 1)          while (i > 1) {
160          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
161                          pParam->fincr /= i;                          pParam->fincr /= i;
162                          pParam->fbase /= i;                          pParam->fbase /= i;
163                          i = pParam->fincr;                          i = pParam->fincr;
# Line 71  Line 166 
166                  i--;                  i--;
167          }          }
168    
169          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
170                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
171    
172                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
173                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
174          }          }
175    
176          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
                 pParam->bitrate = 900000;  
177    
178          if (pParam->rc_buffersize <= 0)          if (pParam->rc_bitrate <= 0)
179                  pParam->rc_buffersize = 16;                  pParam->rc_bitrate = 900000;
180    
181            if (pParam->rc_reaction_delay_factor <= 0)
182                    pParam->rc_reaction_delay_factor = 16;
183    
184            if (pParam->rc_averaging_period <= 0)
185                    pParam->rc_averaging_period = 100;
186    
187            if (pParam->rc_buffer <= 0)
188                    pParam->rc_buffer = 100;
189    
190            /* Max and min quantizers */
191    
192          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
193                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 90  Line 195 
195          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
196                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
197    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
198          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
199                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
200    
201          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
202    
203            if (pParam->max_key_interval == 0)
204                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
205    
206    
207            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
208            if (pEnc == NULL)
209                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
210    
211            /* Zero the Encoder Structure */
212    
213            memset(pEnc, 0, sizeof(Encoder));
214    
215          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
216    
217          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 110  Line 223 
223          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
224          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
225    
226            pEnc->mbParam.fbase = pParam->fbase;
227            pEnc->mbParam.fincr = pParam->fincr;
228    
229          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
230    
231          /* Fill rate control parameters */          /* Fill rate control parameters */
232    
233          pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
234    
235          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
236          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
237    
238          /* try to allocate memory */          /* try to allocate frame memory */
239    
240          pEnc->sCurrent.y        =       pEnc->sCurrent.u        =       pEnc->sCurrent.v        = NULL;          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
241          pEnc->sReference.y      =       pEnc->sReference.u      =       pEnc->sReference.v      = NULL;          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
         pEnc->vInterH.y         =       pEnc->vInterH.u         =       pEnc->vInterH.v         = NULL;  
         pEnc->vInterV.y         =       pEnc->vInterV.u         =       pEnc->vInterV.v         = NULL;  
         pEnc->vInterVf.y        =       pEnc->vInterVf.u        =       pEnc->vInterVf.v        = NULL;  
         pEnc->vInterHV.y        =       pEnc->vInterHV.u        =       pEnc->vInterHV.v        = NULL;  
         pEnc->vInterHVf.y       =       pEnc->vInterHVf.u       =       pEnc->vInterHVf.v       = NULL;  
   
         pEnc->pMBs = NULL;  
   
         if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
                 (pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL)  
         {  
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 if (pEnc)  
                 {  
                         xvid_free(pEnc);  
                 }  
                 return XVID_ERR_MEMORY;  
         }  
242    
243          // init macroblock array          if (pEnc->current == NULL || pEnc->reference == NULL)
244          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)                  goto xvid_err_memory1;
245          {  
246                  pEnc->pMBs[i].dquant = NO_CHANGE;          /* try to allocate mb memory */
247          }  
248            pEnc->current->mbs =
249                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
250                                            pEnc->mbParam.mb_height, CACHE_LINE);
251            pEnc->reference->mbs =
252                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
253                                            pEnc->mbParam.mb_height, CACHE_LINE);
254    
255            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
256                    goto xvid_err_memory2;
257    
258            /* try to allocate image memory */
259    
260    #ifdef _DEBUG
261            image_null(&pEnc->sOriginal);
262    #endif
263    #ifdef BFRAMES
264            image_null(&pEnc->f_refh);
265            image_null(&pEnc->f_refv);
266            image_null(&pEnc->f_refhv);
267    #endif
268            image_null(&pEnc->current->image);
269            image_null(&pEnc->reference->image);
270            image_null(&pEnc->vInterH);
271            image_null(&pEnc->vInterV);
272            image_null(&pEnc->vInterVf);
273            image_null(&pEnc->vInterHV);
274            image_null(&pEnc->vInterHVf);
275    
276    #ifdef _DEBUG
277            if (image_create
278                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
279                     pEnc->mbParam.edged_height) < 0)
280                    goto xvid_err_memory3;
281    #endif
282    #ifdef BFRAMES
283            if (image_create
284                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
285                     pEnc->mbParam.edged_height) < 0)
286                    goto xvid_err_memory3;
287            if (image_create
288                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
289                     pEnc->mbParam.edged_height) < 0)
290                    goto xvid_err_memory3;
291            if (image_create
292                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
293                     pEnc->mbParam.edged_height) < 0)
294                    goto xvid_err_memory3;
295    #endif
296            if (image_create
297                    (&pEnc->current->image, pEnc->mbParam.edged_width,
298                     pEnc->mbParam.edged_height) < 0)
299                    goto xvid_err_memory3;
300            if (image_create
301                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
302                     pEnc->mbParam.edged_height) < 0)
303                    goto xvid_err_memory3;
304            if (image_create
305                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
306                     pEnc->mbParam.edged_height) < 0)
307                    goto xvid_err_memory3;
308            if (image_create
309                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
310                     pEnc->mbParam.edged_height) < 0)
311                    goto xvid_err_memory3;
312            if (image_create
313                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
314                     pEnc->mbParam.edged_height) < 0)
315                    goto xvid_err_memory3;
316            if (image_create
317                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
318                     pEnc->mbParam.edged_height) < 0)
319                    goto xvid_err_memory3;
320            if (image_create
321                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
322                     pEnc->mbParam.edged_height) < 0)
323                    goto xvid_err_memory3;
324    
325    
326    
327            /* B Frames specific init */
328    #ifdef BFRAMES
329    
330            pEnc->mbParam.max_bframes = pParam->max_bframes;
331            pEnc->bquant_ratio = pParam->bquant_ratio;
332            pEnc->bframes = NULL;
333    
334            if (pEnc->mbParam.max_bframes > 0) {
335                    int n;
336    
337                    pEnc->bframes =
338                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
339                                                    CACHE_LINE);
340    
341                    if (pEnc->bframes == NULL)
342                            goto xvid_err_memory3;
343    
344                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
345                            pEnc->bframes[n] = NULL;
346    
347    
348                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
349                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
350    
351                            if (pEnc->bframes[n] == NULL)
352                                    goto xvid_err_memory4;
353    
354                            pEnc->bframes[n]->mbs =
355                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
356                                                            pEnc->mbParam.mb_height, CACHE_LINE);
357    
358                            if (pEnc->bframes[n]->mbs == NULL)
359                                    goto xvid_err_memory4;
360    
361                            image_null(&pEnc->bframes[n]->image);
362    
363                            if (image_create
364                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
365                                     pEnc->mbParam.edged_height) < 0)
366                                    goto xvid_err_memory4;
367    
368                    }
369            }
370    
371            pEnc->bframenum_head = 0;
372            pEnc->bframenum_tail = 0;
373            pEnc->flush_bframes = 0;
374    
375            pEnc->mbParam.m_seconds = 0;
376            pEnc->mbParam.m_ticks = 0;
377    #endif
378    
379          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
380    
381          if (pParam->bitrate)          if (pParam->rc_bitrate) {
382          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
383                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase * 100 / pParam->fincr,                                                  pParam->rc_reaction_delay_factor,
384                                                    pParam->rc_averaging_period, pParam->rc_buffer,
385                                                    pParam->fbase * 1000 / pParam->fincr,
386                                  pParam->max_quantizer, pParam->min_quantizer);                                  pParam->max_quantizer, pParam->min_quantizer);
387          }          }
388    
         create_vlc_tables();  
389          init_timer();          init_timer();
390    
391          return XVID_ERR_OK;          return XVID_ERR_OK;
392    
393            /*
394             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
395             */
396    #ifdef BFRAMES
397      xvid_err_memory4:
398            for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
399    
400                    if (pEnc->bframes[i] == NULL)
401                            continue;
402    
403                    image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
404                                              pEnc->mbParam.edged_height);
405    
406                    xvid_free(pEnc->bframes[i]->mbs);
407    
408                    xvid_free(pEnc->bframes[i]);
409    
410  }  }
411    
412            xvid_free(pEnc->bframes);
413    
414    #endif
415    
416      xvid_err_memory3:
417    #ifdef _DEBUG
418            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
419                                      pEnc->mbParam.edged_height);
420    #endif
421    
422    #ifdef BFRAMES
423            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
424                                      pEnc->mbParam.edged_height);
425            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
426                                      pEnc->mbParam.edged_height);
427            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
428                                      pEnc->mbParam.edged_height);
429    #endif
430    
431            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
432                                      pEnc->mbParam.edged_height);
433            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
434                                      pEnc->mbParam.edged_height);
435            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
436                                      pEnc->mbParam.edged_height);
437            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
438                                      pEnc->mbParam.edged_height);
439            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
440                                      pEnc->mbParam.edged_height);
441            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
442                                      pEnc->mbParam.edged_height);
443            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
444                                      pEnc->mbParam.edged_height);
445    
446      xvid_err_memory2:
447            xvid_free(pEnc->current->mbs);
448            xvid_free(pEnc->reference->mbs);
449    
450      xvid_err_memory1:
451            xvid_free(pEnc->current);
452            xvid_free(pEnc->reference);
453            xvid_free(pEnc);
454    
455            pParam->handle = NULL;
456    
457            return XVID_ERR_MEMORY;
458    }
459    
460  int encoder_destroy(Encoder * pEnc)  /*****************************************************************************
461     * Encoder destruction
462     *
463     * This function destroy the entire encoder structure created by a previous
464     * successful encoder_create call.
465     *
466     * Returned values (for now only one returned value) :
467     *    - XVID_ERR_OK     - no errors
468     *
469     ****************************************************************************/
470    
471    int
472    encoder_destroy(Encoder * pEnc)
473  {  {
474          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
475    
476          xvid_free(pEnc->pMBs);          /* B Frames specific */
477          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #ifdef BFRAMES
478          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          int i;
479          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
480          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
481          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
482                    if (pEnc->bframes[i] == NULL)
483                            continue;
484    
485                    image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
486                                              pEnc->mbParam.edged_height);
487    
488                    xvid_free(pEnc->bframes[i]->mbs);
489    
490                    xvid_free(pEnc->bframes[i]);
491    
492            }
493    
494            xvid_free(pEnc->bframes);
495    
496    #endif
497    
498            /* All images, reference, current etc ... */
499    
500            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
501                                      pEnc->mbParam.edged_height);
502            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
503                                      pEnc->mbParam.edged_height);
504            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
505                                      pEnc->mbParam.edged_height);
506            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
507                                      pEnc->mbParam.edged_height);
508            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
509                                      pEnc->mbParam.edged_height);
510            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
511                                      pEnc->mbParam.edged_height);
512            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
513                                      pEnc->mbParam.edged_height);
514    #ifdef BFRAMES
515            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
516                                      pEnc->mbParam.edged_height);
517            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
518                                      pEnc->mbParam.edged_height);
519            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
520                                      pEnc->mbParam.edged_height);
521    #endif
522    #ifdef _DEBUG
523            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
524                                      pEnc->mbParam.edged_height);
525    #endif
526    
527            /* Encoder structure */
528    
529            xvid_free(pEnc->current->mbs);
530            xvid_free(pEnc->current);
531    
532            xvid_free(pEnc->reference->mbs);
533            xvid_free(pEnc->reference);
534    
535          xvid_free(pEnc);          xvid_free(pEnc);
536    
537          destroy_vlc_tables();          return XVID_ERR_OK;
538    }
539    
540    /*****************************************************************************
541     * Frame encoder entry point
542     *
543     * At this moment 2 versions coexist : one for IPB compatible encoder,
544     *                                     another one for the old IP encoder.
545     *
546     * Returned values :
547     *    - XVID_ERR_OK     - no errors
548     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
549     *                        format
550     ****************************************************************************/
551    
552    
553    #ifdef BFRAMES
554    /*****************************************************************************
555     * Frame encoder entry point for IPB capable encoder
556     ****************************************************************************/
557    int
558    encoder_encode(Encoder * pEnc,
559                               XVID_ENC_FRAME * pFrame,
560                               XVID_ENC_STATS * pResult)
561    {
562            uint16_t x, y;
563            Bitstream bs;
564            uint32_t bits;
565    
566    #ifdef _DEBUG
567            float psnr;
568            char temp[128];
569    #endif
570    
571            ENC_CHECK(pEnc);
572            ENC_CHECK(pFrame);
573    
574            start_global_timer();
575    
576            BitstreamInit(&bs, pFrame->bitstream, 0);
577    
578            /*
579             * bframe "flush" code
580             */
581    
582            if ((pFrame->image == NULL || pEnc->flush_bframes)
583                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
584    
585                    if (pEnc->flush_bframes == 0) {
586                            /*
587                             * we have reached the end of stream without getting
588                             * a future reference frame... so encode last final
589                             * frame as a pframe
590                             */
591    
592                            /* ToDo : remove dprintf calls */
593                            /*
594                               dprintf("--- PFRAME (final frame correction) --- ");
595                             */
596                            pEnc->bframenum_tail--;
597                            SWAP(pEnc->current, pEnc->reference);
598    
599                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
600    
601                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
602    
603                            BitstreamPad(&bs);
604                            pFrame->length = BitstreamLength(&bs);
605                            pFrame->input_consumed = 0;
606                            pFrame->intra = 0;
607    
608          return XVID_ERR_OK;          return XVID_ERR_OK;
609  }  }
610    
611  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)                  /* ToDo : remove dprintf calls */
612                    /*
613                       dprintf("--- BFRAME (flush) --- ");
614                     */
615                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
616                    pEnc->bframenum_head++;
617    
618    
619                    BitstreamPad(&bs);
620                    pFrame->length = BitstreamLength(&bs);
621                    pFrame->input_consumed = 0;
622                    pFrame->intra = 0;
623    
624                    return XVID_ERR_OK;
625            }
626    
627            if (pFrame->image == NULL) {
628                    pFrame->length = 0;
629                    pFrame->input_consumed = 1;
630                    pFrame->intra = 0;
631    
632                    return XVID_ERR_OK;
633            }
634    
635            if (pEnc->bframenum_head > 0) {
636                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
637            }
638    
639            pEnc->flush_bframes = 0;
640    
641            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642             * Well there was a separation here so i put it in ANSI C
643             * comment style :-)
644             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
645    
646            SWAP(pEnc->current, pEnc->reference);
647    
648            emms();
649    
650            if (pFrame->quant == 0)
651                    pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
652            else
653                    pEnc->current->quant = pFrame->quant;
654    
655            if (pEnc->current->quant < 1)
656                    pEnc->current->quant = 1;
657    
658            if (pEnc->current->quant > 31)
659                    pEnc->current->quant = 31;
660    
661            pEnc->current->global_flags = pFrame->general;
662            pEnc->current->motion_flags = pFrame->motion;
663            pEnc->current->seconds = pEnc->mbParam.m_seconds;
664            pEnc->current->ticks = pEnc->mbParam.m_ticks;
665            /* ToDo : dynamic fcode (in both directions) */
666            pEnc->current->fcode = pEnc->mbParam.m_fcode;
667            pEnc->current->bcode = pEnc->mbParam.m_fcode;
668    
669            start_timer();
670            if (image_input
671                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
672                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
673                    return XVID_ERR_FORMAT;
674            stop_conv_timer();
675    
676    #ifdef _DEBUG
677            image_copy(&pEnc->sOriginal, &pEnc->current->image,
678                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
679    #endif
680    
681            emms();
682    
683            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684             * Luminance masking
685             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
686    
687            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
688                    int *temp_dquants =
689                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
690                                                                    pEnc->mbParam.mb_height * sizeof(int),
691                                                                    CACHE_LINE);
692    
693                    pEnc->current->quant =
694                            adaptive_quantization(pEnc->current->image.y,
695                                                                      pEnc->mbParam.edged_width, temp_dquants,
696                                                                      pEnc->current->quant, pEnc->current->quant,
697                                                                      2 * pEnc->current->quant,
698                                                                      pEnc->mbParam.mb_width,
699                                                                      pEnc->mbParam.mb_height);
700    
701                    for (y = 0; y < pEnc->mbParam.mb_height; y++) {
702    
703    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
704    
705                            for (x = 0; x < pEnc->mbParam.mb_width; x++) {
706                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
707    
708                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
709                            }
710    
711    #undef OFFSET
712    
713                    }
714    
715                    xvid_free(temp_dquants);
716            }
717    
718            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719             * ivop/pvop/bvop selection
720             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
721    
722    
723            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||
724                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
725                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
726                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
727                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
728                                             pEnc->mbParam.height) > 30) {
729                    /*
730                     * This will be coded as an Intra Frame
731                     */
732    
733                    /* ToDo : Remove dprintf calls */
734                    /*
735                       dprintf("--- IFRAME ---");
736                     */
737    
738                    FrameCodeI(pEnc, &bs, &bits);
739    
740                    pFrame->intra = 1;
741                    pEnc->flush_bframes = 1;
742    
743                    /*
744                     * NB : sequences like "IIBB" decode fine with msfdam but,
745                     *      go screwy with divx 5.00
746                     */
747            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
748                    /*
749                     * This will be coded as a Predicted Frame
750                     */
751    
752                    /* ToDo : Remove dprintf calls */
753                    /*
754                       dprintf("--- PFRAME ---");
755                     */
756    
757                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
758                    pFrame->intra = 0;
759                    pEnc->flush_bframes = 1;
760            } else {
761                    /*
762                     * This will be coded as a Bidirectional Frame
763                     */
764    
765                    /* ToDo : Remove dprintf calls */
766                    /*
767                       dprintf("--- BFRAME (store) ---  head=%i tail=%i",
768                       pEnc->bframenum_head,
769                       pEnc->bframenum_tail);
770                     */
771    
772                    if (pFrame->bquant < 1) {
773                            pEnc->current->quant =
774                                    ((pEnc->reference->quant +
775                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
776                    } else {
777                            pEnc->current->quant = pFrame->bquant;
778                    }
779    
780                    /* store frame into bframe buffer & swap ref back to current */
781                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
782                    SWAP(pEnc->current, pEnc->reference);
783    
784                    pEnc->bframenum_tail++;
785    
786                    pFrame->intra = 0;
787                    pFrame->length = 0;
788                    pFrame->input_consumed = 1;
789    
790                    pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
791                    if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {
792                            pEnc->mbParam.m_seconds++;
793                            pEnc->mbParam.m_ticks = 0;
794                    }
795    
796                    return XVID_ERR_OK;
797            }
798    
799            BitstreamPad(&bs);
800            pFrame->length = BitstreamLength(&bs);
801    
802            if (pResult) {
803                    pResult->quant = pEnc->current->quant;
804                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
805                    pResult->kblks = pEnc->sStat.kblks;
806                    pResult->mblks = pEnc->sStat.mblks;
807                    pResult->ublks = pEnc->sStat.ublks;
808            }
809    
810            emms();
811    
812    #ifdef _DEBUG
813            psnr =
814                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
815                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
816                                       pEnc->mbParam.height);
817    
818            snprintf(temp, 127, "PSNR: %f\n", psnr);
819            DEBUG(temp);
820    #endif
821    
822            if (pFrame->quant == 0) {
823                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
824                                                      pFrame->length, pFrame->intra);
825            }
826    
827            pEnc->iFrameNum++;
828            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
829            if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {
830                    pEnc->mbParam.m_seconds++;
831                    pEnc->mbParam.m_ticks = 0;
832            }
833            pFrame->input_consumed = 1;
834    
835            stop_global_timer();
836            write_timer();
837    
838            return XVID_ERR_OK;
839    }
840    #else
841    /*****************************************************************************
842     * Frame encoder entry point for IP capable encoder
843     ****************************************************************************/
844    int
845    encoder_encode(Encoder * pEnc,
846                               XVID_ENC_FRAME * pFrame,
847                               XVID_ENC_STATS * pResult)
848  {  {
849          uint16_t x, y;          uint16_t x, y;
850          Bitstream bs;          Bitstream bs;
851          uint32_t bits;          uint32_t bits;
852          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
853    
854    #ifdef _DEBUG
855            float psnr;
856            uint8_t temp[128];
857    #endif
858    
859          start_global_timer();          start_global_timer();
860    
861          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 210  Line 863 
863          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
864          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
865    
866          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
867          pEnc->mbParam.motion_flags = pFrame->motion;  
868            pEnc->current->global_flags = pFrame->general;
869            pEnc->current->motion_flags = pFrame->motion;
870            pEnc->mbParam.hint = &pFrame->hint;
871    
872          start_timer();          start_timer();
873          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
874                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
875          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
876                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
877          stop_conv_timer();          stop_conv_timer();
878    
879          EMMS();  #ifdef _DEBUG
880            image_copy(&pEnc->sOriginal, &pEnc->current->image,
881                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
882    #endif
883    
884          BitstreamInit(&bs, pFrame->bitstream, 0);          emms();
   
         if (pFrame->quant == 0)  
         {  
                 pEnc->mbParam.quant = RateControlGetQ(0);  
         }  
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
         }  
885    
886          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          BitstreamInit(&bs, pFrame->bitstream, 0);
         {  
                 int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
887    
888                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y,          if (pFrame->quant == 0) {
889                                                              pEnc->mbParam.width,                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
890                                                              temp_dquants,          } else {
891                                                              pFrame->quant,                  pEnc->current->quant = pFrame->quant;
892                                                              pFrame->quant,          }
893                                                              2*pFrame->quant,  
894            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
895                    int *temp_dquants =
896                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
897                                                                    pEnc->mbParam.mb_height * sizeof(int),
898                                                                    CACHE_LINE);
899    
900                    pEnc->current->quant =
901                            adaptive_quantization(pEnc->current->image.y,
902                                                                      pEnc->mbParam.edged_width, temp_dquants,
903                                                                      pEnc->current->quant, pEnc->current->quant,
904                                                                      2 * pEnc->current->quant,
905                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
906                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
907    
908                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
909                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
910                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
911                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
912                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
913    
914    
915                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
916    
917                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
918                          }                          }
919    
920    #undef OFFSET
921                    }
922    
923                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
924          }          }
925    
926          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
927                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
928                          write_vol_header = 1;                          write_vol_header = 1;
929                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
930          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
931          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
932    
933                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
934    
935                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
936                          write_vol_header = 1;                          write_vol_header = 1;
937    
938                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
939    
940                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
941                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
942                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
943                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
944                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
945                  }                  } else {
946                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
947                          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());  
948                  }                  }
949                  if(write_vol_header == 0)                  if(write_vol_header == 0)
950                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
951          }          }
952    
953          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
954          {                  if ((pEnc->iFrameNum == 0)
955                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
956                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
957                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
958                  else                  } else {
959                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
960          }          }
961          else          } else {
962          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
963                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
964                  else                  } else {
965                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
966          }          }
967    
968            }
969    
970          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
971          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
972          BitstreamPad(&bs);          BitstreamPad(&bs);
973          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
974    
975          if (pResult)          if (pResult) {
976          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
977                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
978                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
979                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
980                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
981          }          }
982    
983          EMMS();          emms();
984    
985          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
986          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
987                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
988          }          }
989    #ifdef _DEBUG
990            psnr =
991                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
992                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
993                                       pEnc->mbParam.height);
994    
995            snprintf(temp, 127, "PSNR: %f\n", psnr);
996            DEBUG(temp);
997    #endif
998    
999          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1000    
1001          stop_global_timer();          stop_global_timer();
1002          write_timer();          write_timer();
1003    
1004          return XVID_ERR_OK;          return XVID_ERR_OK;
1005  }  }
1006    #endif
1007    
1008    
1009  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1010    CodeIntraMB(Encoder * pEnc,
1011                            MACROBLOCK * pMB)
1012    {
1013    
1014          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1015    
1016          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1017                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1018                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1019            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1020            pMB->sad16 = 0;
1021    
1022            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1023                    if (pMB->dquant != NO_CHANGE) {
1024                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1025                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1026    
1027                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
1028                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
1029                            if (pEnc->current->quant < 1)
1030                                    pEnc->current->quant = 1;
1031                    }
1032                  }                  }
1033    
1034            pMB->quant = pEnc->current->quant;
1035          }          }
1036    
1037          pMB->quant = pEnc->mbParam.quant;  
1038    #define FCODEBITS       3
1039    #define MODEBITS        5
1040    
1041    void
1042    HintedMESet(Encoder * pEnc,
1043                            int *intra)
1044    {
1045            HINTINFO *hint;
1046            Bitstream bs;
1047            int length, high;
1048            uint32_t x, y;
1049    
1050            hint = pEnc->mbParam.hint;
1051    
1052            if (hint->rawhints) {
1053                    *intra = hint->mvhint.intra;
1054            } else {
1055                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1056                    *intra = BitstreamGetBit(&bs);
1057            }
1058    
1059            if (*intra) {
1060                    return;
1061            }
1062    
1063            pEnc->current->fcode =
1064                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1065                                                                                                                                     FCODEBITS);
1066    
1067            length = pEnc->current->fcode + 5;
1068            high = 1 << (length - 1);
1069    
1070            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1071                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1072                            MACROBLOCK *pMB =
1073                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1074                            MVBLOCKHINT *bhint =
1075                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1076                            VECTOR pred[4];
1077                            VECTOR tmp;
1078                            int32_t dummy[4];
1079                            int vec;
1080    
1081                            pMB->mode =
1082                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1083                                                                                                                                      MODEBITS);
1084    
1085                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1086                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1087    
1088                            if (pMB->mode == MODE_INTER) {
1089                                    tmp.x =
1090                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1091                                                                                                                                                      length);
1092                                    tmp.y =
1093                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1094                                                                                                                                                      length);
1095                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1096                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1097    
1098                                    get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,
1099                                                            0, pred, dummy);
1100    
1101                                    for (vec = 0; vec < 4; ++vec) {
1102                                            pMB->mvs[vec].x = tmp.x;
1103                                            pMB->mvs[vec].y = tmp.y;
1104                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;
1105                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
1106                                    }
1107                            } else if (pMB->mode == MODE_INTER4V) {
1108                                    for (vec = 0; vec < 4; ++vec) {
1109                                            tmp.x =
1110                                                    (hint->rawhints) ? bhint->mvs[vec].
1111                                                    x : BitstreamGetBits(&bs, length);
1112                                            tmp.y =
1113                                                    (hint->rawhints) ? bhint->mvs[vec].
1114                                                    y : BitstreamGetBits(&bs, length);
1115                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1116                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1117    
1118                                            get_pmvdata(pEnc->current->mbs, x, y,
1119                                                                    pEnc->mbParam.mb_width, vec, pred, dummy);
1120    
1121                                            pMB->mvs[vec].x = tmp.x;
1122                                            pMB->mvs[vec].y = tmp.y;
1123                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
1124                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
1125                                    }
1126                            } else                          // intra / stuffing / not_coded
1127                            {
1128                                    for (vec = 0; vec < 4; ++vec) {
1129                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1130                                    }
1131                            }
1132    
1133                            if (pMB->mode == MODE_INTER4V &&
1134                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1135                                    && pMB->dquant != NO_CHANGE) {
1136                                    pMB->mode = MODE_INTRA;
1137    
1138                                    for (vec = 0; vec < 4; ++vec) {
1139                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1140                                    }
1141                            }
1142                    }
1143            }
1144  }  }
1145    
1146    
1147  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  void
1148    HintedMEGet(Encoder * pEnc,
1149                            int intra)
1150    {
1151            HINTINFO *hint;
1152            Bitstream bs;
1153            uint32_t x, y;
1154            int length, high;
1155    
1156            hint = pEnc->mbParam.hint;
1157    
1158            if (hint->rawhints) {
1159                    hint->mvhint.intra = intra;
1160            } else {
1161                    BitstreamInit(&bs, hint->hintstream, 0);
1162                    BitstreamPutBit(&bs, intra);
1163            }
1164    
1165            if (intra) {
1166                    if (!hint->rawhints) {
1167                            BitstreamPad(&bs);
1168                            hint->hintlength = BitstreamLength(&bs);
1169                    }
1170                    return;
1171            }
1172    
1173            length = pEnc->current->fcode + 5;
1174            high = 1 << (length - 1);
1175    
1176            if (hint->rawhints) {
1177                    hint->mvhint.fcode = pEnc->current->fcode;
1178            } else {
1179                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1180            }
1181    
1182            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1183                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1184                            MACROBLOCK *pMB =
1185                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1186                            MVBLOCKHINT *bhint =
1187                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1188                            VECTOR tmp;
1189    
1190                            if (hint->rawhints) {
1191                                    bhint->mode = pMB->mode;
1192                            } else {
1193                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1194                            }
1195    
1196                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1197                                    tmp.x = pMB->mvs[0].x;
1198                                    tmp.y = pMB->mvs[0].y;
1199                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1200                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1201    
1202                                    if (hint->rawhints) {
1203                                            bhint->mvs[0].x = tmp.x;
1204                                            bhint->mvs[0].y = tmp.y;
1205                                    } else {
1206                                            BitstreamPutBits(&bs, tmp.x, length);
1207                                            BitstreamPutBits(&bs, tmp.y, length);
1208                                    }
1209                            } else if (pMB->mode == MODE_INTER4V) {
1210                                    int vec;
1211    
1212                                    for (vec = 0; vec < 4; ++vec) {
1213                                            tmp.x = pMB->mvs[vec].x;
1214                                            tmp.y = pMB->mvs[vec].y;
1215                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1216                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1217    
1218                                            if (hint->rawhints) {
1219                                                    bhint->mvs[vec].x = tmp.x;
1220                                                    bhint->mvs[vec].y = tmp.y;
1221                                            } else {
1222                                                    BitstreamPutBits(&bs, tmp.x, length);
1223                                                    BitstreamPutBits(&bs, tmp.y, length);
1224                                            }
1225                                    }
1226                            }
1227                    }
1228            }
1229    
1230            if (!hint->rawhints) {
1231                    BitstreamPad(&bs);
1232                    hint->hintlength = BitstreamLength(&bs);
1233            }
1234    }
1235    
1236    
1237    static int
1238    FrameCodeI(Encoder * pEnc,
1239                       Bitstream * bs,
1240                       uint32_t * pBits)
1241  {  {
1242    
1243          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 361  Line 1246 
1246          uint16_t x, y;          uint16_t x, y;
1247    
1248          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1249          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1250          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1251            pEnc->current->coding_type = I_VOP;
1252    
1253          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1254          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
1255    
1256          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1257    
# Line 374  Line 1260 
1260          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1261    
1262          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1263                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1264                  {                          MACROBLOCK *pMB =
1265                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1266    
1267                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1268    
1269                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1270                                                              dct_codes, qcoeff);
1271    
1272                          start_timer();                          start_timer();
1273                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1274                          stop_prediction_timer();                          stop_prediction_timer();
1275    
1276                          start_timer();                          start_timer();
1277                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1278                          stop_coding_timer();                          stop_coding_timer();
1279                  }                  }
1280    
# Line 397  Line 1284 
1284          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1285          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1286          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1287          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1288    
1289            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1290                    HintedMEGet(pEnc, 1);
1291            }
1292    
1293          return 1;                                        // intra          return 1;                                        // intra
1294  }  }
# Line 405  Line 1296 
1296    
1297  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1298    
1299  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1300    FrameCodeP(Encoder * pEnc,
1301                       Bitstream * bs,
1302                       uint32_t * pBits,
1303                       bool force_inter,
1304                       bool vol_header)
1305  {  {
1306          float fSigma;          float fSigma;
1307    
# Line 415  Line 1311 
1311          int iLimit;          int iLimit;
1312          uint32_t x, y;          uint32_t x, y;
1313          int iSearchRange;          int iSearchRange;
1314          bool bIntra;          int bIntra;
1315    
1316          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1317          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1318    
1319          start_timer();          start_timer();
1320          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1321                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
1322                         pEnc->mbParam.edged_height,                                     pEnc->current->global_flags & XVID_INTERLACING);
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->mbParam.global_flags & XVID_INTERLACING);  
1323          stop_edges_timer();          stop_edges_timer();
1324    
1325          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1326            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1327            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1328    
1329          if (!force_inter)          if (!force_inter)
1330                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1331                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1332                                       INTRA_THRESHOLD);
1333          else          else
1334                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1335    
1336          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1337                  start_timer();                  start_timer();
1338                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1339                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1340                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1341                                                      pEnc->current->rounding_type);
1342                  stop_inter_timer();                  stop_inter_timer();
1343          }          }
1344    
1345          start_timer();          start_timer();
1346          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1347                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1348                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          } else {
1349                    bIntra =
1350                            MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1351                                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1352                                                             iLimit);
1353            }
1354          stop_motion_timer();          stop_motion_timer();
1355    
1356          if (bIntra == 1)          if (bIntra == 1) {
1357                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1358            }
1359    
1360          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1361    
1362          if(vol_header)          if(vol_header)
1363                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1364    
1365          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
1366    
1367          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1368    
# Line 467  Line 1371 
1371          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1372          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1373    
1374          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1375          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1376                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1377                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1378    
1379                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1380    
1381                          if (!bIntra)                          if (!bIntra) {
                         {  
1382                                  start_timer();                                  start_timer();
1383                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1384                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1385                                                       &pEnc->sReference,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1386                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->sCurrent,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1387                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1388                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1389                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1390                                  stop_comp_timer();                                  stop_comp_timer();
1391    
1392                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1393                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1394                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1395                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1396                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1397                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1398                                                    else if (pEnc->current->quant < 1)
1399                                                            pEnc->current->quant = 1;
1400                                          }                                          }
1401                                  }                                  }
1402                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
1403    
1404                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1405    
1406                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->cbp =
1407                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1408                          else                                                                            dct_codes, qcoeff);
1409                          {                          } else {
1410                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1411                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1412                                                                      dct_codes, qcoeff);
1413                          }                          }
1414    
1415                          start_timer();                          start_timer();
1416                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1417                          stop_prediction_timer();                          stop_prediction_timer();
1418    
1419                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1420                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1421                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1422                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1423                                   pMB->mvs[0].x || pMB->mvs[0].y ||                                             pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
                                  pMB->mvs[1].x || pMB->mvs[1].y ||  
                                  pMB->mvs[2].x || pMB->mvs[2].y ||  
                                  pMB->mvs[3].x || pMB->mvs[3].y)  
                         {  
1424                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1425                          }                          } else {
                         else  
                         {  
1426                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1427                          }                          }
1428    
1429                          start_timer();                          start_timer();
1430                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1431                          stop_coding_timer();                          stop_coding_timer();
1432                  }                  }
1433          }          }
1434    
1435          emms();          emms();
1436    
1437            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1438                    HintedMEGet(pEnc, 0);
1439            }
1440    
1441          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1442                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1443    
1444          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1445    
1446          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1447    
1448          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1449              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1450          {          {
1451                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1452                  iSearchRange *= 2;                  iSearchRange *= 2;
1453          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1454                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1455                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1456                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1457          {          {
1458                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1459                  iSearchRange /= 2;                  iSearchRange /= 2;
1460          }          }
1461    
# Line 569  Line 1465 
1465    
1466          return 0;                                        // inter          return 0;                                        // inter
1467  }  }
1468    
1469    
1470    #ifdef BFRAMES
1471    static void
1472    FrameCodeB(Encoder * pEnc,
1473                       FRAMEINFO * frame,
1474                       Bitstream * bs,
1475                       uint32_t * pBits)
1476    {
1477            int16_t dct_codes[6 * 64];
1478            int16_t qcoeff[6 * 64];
1479            uint32_t x, y;
1480            VECTOR forward;
1481            VECTOR backward;
1482    
1483            IMAGE *f_ref = &pEnc->reference->image;
1484            IMAGE *b_ref = &pEnc->current->image;
1485    
1486            // forward
1487            image_setedges(f_ref, pEnc->mbParam.edged_width,
1488                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1489                                       pEnc->mbParam.height,
1490                                       frame->global_flags & XVID_INTERLACING);
1491            start_timer();
1492            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1493                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1494                                              0);
1495            stop_inter_timer();
1496    
1497            // backward
1498            image_setedges(b_ref, pEnc->mbParam.edged_width,
1499                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1500                                       pEnc->mbParam.height,
1501                                       frame->global_flags & XVID_INTERLACING);
1502            start_timer();
1503            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1504                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1505                                              0);
1506            stop_inter_timer();
1507    
1508            start_timer();
1509            MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1510                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1511                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1512                                                     &pEnc->vInterV, &pEnc->vInterHV);
1513    
1514    
1515            stop_motion_timer();
1516    
1517            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1518               {
1519               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1520               } */
1521    
1522            frame->coding_type = B_VOP;
1523            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);
1524    
1525            *pBits = BitstreamPos(bs);
1526    
1527            pEnc->sStat.iTextBits = 0;
1528            pEnc->sStat.iMvSum = 0;
1529            pEnc->sStat.iMvCount = 0;
1530            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1531    
1532    
1533            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1534                    // reset prediction
1535    
1536                    forward.x = 0;
1537                    forward.y = 0;
1538                    backward.x = 0;
1539                    backward.y = 0;
1540    
1541                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1542                            MACROBLOCK *f_mb =
1543                                    &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1544                            MACROBLOCK *b_mb =
1545                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1546                            MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1547    
1548                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1549                            if (mb->mode == MODE_NOT_CODED) {
1550                                    mb->mvs[0].x = 0;
1551                                    mb->mvs[0].y = 0;
1552                                    continue;
1553                            }
1554    
1555                            MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1556                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1557                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1558                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1559                                                                             dct_codes);
1560    
1561                            mb->quant = frame->quant;
1562                            mb->cbp =
1563                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1564                                                                      qcoeff);
1565                            //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1566    
1567    
1568                            if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1569                                    && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
1570                                    mb->mode = 5;   // skipped
1571                            }
1572    
1573                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1574                                    mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1575                                    mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1576                                    forward.x = mb->mvs[0].x;
1577                                    forward.y = mb->mvs[0].y;
1578                            }
1579    
1580                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
1581                                    mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1582                                    mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1583                                    backward.x = mb->b_mvs[0].x;
1584                                    backward.y = mb->b_mvs[0].y;
1585                            }
1586    //          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);
1587    
1588                            start_timer();
1589                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1590                                                     &pEnc->sStat);
1591                            stop_coding_timer();
1592                    }
1593            }
1594    
1595            emms();
1596    
1597            // TODO: dynamic fcode/bcode ???
1598    
1599            *pBits = BitstreamPos(bs) - *pBits;
1600    }
1601    #endif

Legend:
Removed from v.83  
changed lines
  Added in v.198

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