[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.3, Sat Mar 9 14:58:50 2002 UTC revision 1.90.2.2, Wed May 28 16:44:38 2003 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Encoder main module -
5     *
6     *  Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7     *               2002 Peter Ross <pross@xvid.org>
8     *               2002 Daniel Smith <danielsmith@astroboymail.com>
9     *
10     *  This file is part of XviD, a free MPEG-4 video encoder/decoder
11     *
12     *  XviD is free software; you can redistribute it and/or modify it
13     *  under the terms of the GNU General Public License as published by
14     *  the Free Software Foundation; either version 2 of the License, or
15     *  (at your option) any later version.
16     *
17     *  This program is distributed in the hope that it will be useful,
18     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20     *  GNU General Public License for more details.
21     *
22     *  You should have received a copy of the GNU General Public License
23     *  along with this program; if not, write to the Free Software
24     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25     *
26     *  Under section 8 of the GNU General Public License, the copyright
27     *  holders of XVID explicitly forbid distribution in the following
28     *  countries:
29     *
30     *    - Japan
31     *    - United States of America
32     *
33     *  Linking XviD statically or dynamically with other modules is making a
34     *  combined work based on XviD.  Thus, the terms and conditions of the
35     *  GNU General Public License cover the whole combination.
36     *
37     *  As a special exception, the copyright holders of XviD give you
38     *  permission to link XviD with independent modules that communicate with
39     *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
40     *  license terms of these independent modules, and to copy and distribute
41     *  the resulting combined work under terms of your choice, provided that
42     *  every copy of the combined work is accompanied by a complete copy of
43     *  the source code of XviD (the version of XviD used to produce the
44     *  combined work), being distributed under the terms of the GNU General
45     *  Public License plus this exception.  An independent module is a module
46     *  which is not derived from or based on XviD.
47     *
48     *  Note that people who make modified versions of XviD are not obligated
49     *  to grant this special exception for their modified versions; it is
50     *  their choice whether to do so.  The GNU General Public License gives
51     *  permission to release a modified version without this exception; this
52     *  exception also makes it possible to release a modified version which
53     *  carries forward this exception.
54     *
55     * $Id$
56     *
57     ****************************************************************************/
58    
59  #include <stdlib.h>  #include <stdlib.h>
60  #include <stdio.h>  #include <stdio.h>
61  #include <math.h>  #include <math.h>
62    #include <string.h>
63    
64  #include "encoder.h"  #include "encoder.h"
65  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
66  #include "global.h"  #include "global.h"
67  #include "utils/timer.h"  #include "utils/timer.h"
68  #include "image/image.h"  #include "image/image.h"
69    #include "motion/motion.h"
70  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
71  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
72  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 16  Line 76 
76  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
77  #include "quant/adapt_quant.h"  #include "quant/adapt_quant.h"
78  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
79    #include "utils/mem_align.h"
80    
81  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
82     * Local macros
83     ****************************************************************************/
84    
85    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
86    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
87    
88  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
89  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
90     ****************************************************************************/
91    
92    static int FrameCodeI(Encoder * pEnc,
93                                              Bitstream * bs,
94                                              uint32_t * pBits);
95    
96    static int FrameCodeP(Encoder * pEnc,
97                                              Bitstream * bs,
98                                              uint32_t * pBits,
99                                              bool force_inter,
100                                              bool vol_header);
101    
102    /*****************************************************************************
103     * Local data
104     ****************************************************************************/
105    
106  static int DQtab[4] =  static int DQtab[4] = {
 {  
107          -1, -2, 1, 2          -1, -2, 1, 2
108  };  };
109    
110  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
111          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
112  };  };
113    
114    
115  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
116    image_null(IMAGE * image)
117    {
118            image->y = image->u = image->v = NULL;
119    }
120    
121    
122    /*****************************************************************************
123     * Encoder creation
124     *
125     * This function creates an Encoder instance, it allocates all necessary
126     * image buffers (reference, current) and initialize the internal xvid
127     * encoder paremeters according to the XVID_ENC_PARAM input parameter.
128     *
129     * The code seems to be very long but is very basic, mainly memory allocation
130     * and cleaning code.
131     *
132     * Returned values :
133     *    - XVID_ERR_OK     - no errors
134     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
135     *                        cleans the structure before exiting.
136     *                        pParam->handle is also set to NULL.
137     *
138     ****************************************************************************/
139    
140    int
141    encoder_create(XVID_ENC_PARAM * pParam)
142  {  {
143          Encoder *pEnc;          Encoder *pEnc;
144          uint32_t i;          int i;
145    
146          pParam->handle = NULL;          pParam->handle = NULL;
147    
# Line 48  Line 152 
152          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
153          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
154    
155          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
156          {  
157            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
158                  pParam->fincr = 1;                  pParam->fincr = 1;
159                  pParam->fbase = 25;                  pParam->fbase = 25;
160          }          }
161    
162          // simplify the "fincr/fbase" fraction          /*
163          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
164             * (neccessary, since windows supplies us with huge numbers)
165             */
166    
167          i = pParam->fincr;          i = pParam->fincr;
168          while (i > 1)          while (i > 1) {
169          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
170                          pParam->fincr /= i;                          pParam->fincr /= i;
171                          pParam->fbase /= i;                          pParam->fbase /= i;
172                          i = pParam->fincr;                          i = pParam->fincr;
# Line 70  Line 175 
175                  i--;                  i--;
176          }          }
177    
178          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
179                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
180    
181                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
182                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
183          }          }
184    
185          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
186                  pParam->bitrate = 900000;  
187            if (pParam->rc_bitrate <= 0)
188                    pParam->rc_bitrate = 900000;
189    
190            if (pParam->rc_reaction_delay_factor <= 0)
191                    pParam->rc_reaction_delay_factor = 16;
192    
193          if (pParam->rc_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
194                  pParam->rc_buffersize = pParam->bitrate * pParam->fbase;                  pParam->rc_averaging_period = 100;
195    
196            if (pParam->rc_buffer <= 0)
197                    pParam->rc_buffer = 100;
198    
199            /* Max and min quantizers */
200    
201          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
202                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 89  Line 204 
204          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
205                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
206    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
207          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
208                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
209    
210          if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL)          /* 1 keyframe each 10 seconds */
211    
212            if (pParam->max_key_interval <= 0)
213                    pParam->max_key_interval = 10 * pParam->fbase / pParam->fincr;
214    
215            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
216            if (pEnc == NULL)
217                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
218    
219            /* Zero the Encoder Structure */
220    
221            memset(pEnc, 0, sizeof(Encoder));
222    
223          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
224    
225          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 109  Line 231 
231          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
232          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
233    
234            pEnc->mbParam.fbase = pParam->fbase;
235            pEnc->mbParam.fincr = pParam->fincr;
236    
237            pEnc->mbParam.m_quant_type = H263_QUANT;
238    
239          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
240    
241          /* Fill rate control parameters */          /* Fill rate control parameters */
242    
243          pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
244    
245          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
246          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
247    
248          if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* try to allocate frame memory */
         {  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
249    
250          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
251          {          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
252    
253          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          if (pEnc->current == NULL || pEnc->reference == NULL)
254          {                  goto xvid_err_memory1;
                 image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
255    
256          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          /* try to allocate mb memory */
257          {  
258                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          pEnc->current->mbs =
259                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
260                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                          pEnc->mbParam.mb_height, CACHE_LINE);
261                  free(pEnc);          pEnc->reference->mbs =
262                  return XVID_ERR_MEMORY;                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
263          }                                          pEnc->mbParam.mb_height, CACHE_LINE);
264    
265            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
266                    goto xvid_err_memory2;
267    
268            /* try to allocate image memory */
269    
270    #ifdef _DEBUG_PSNR
271            image_null(&pEnc->sOriginal);
272    #endif
273            image_null(&pEnc->current->image);
274            image_null(&pEnc->reference->image);
275            image_null(&pEnc->vInterH);
276            image_null(&pEnc->vInterV);
277            image_null(&pEnc->vInterHV);
278    
279    #ifdef _DEBUG_PSNR
280            if (image_create
281                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
282                     pEnc->mbParam.edged_height) < 0)
283                    goto xvid_err_memory3;
284    #endif
285            if (image_create
286                    (&pEnc->current->image, pEnc->mbParam.edged_width,
287                     pEnc->mbParam.edged_height) < 0)
288                    goto xvid_err_memory3;
289            if (image_create
290                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
291                     pEnc->mbParam.edged_height) < 0)
292                    goto xvid_err_memory3;
293            if (image_create
294                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
295                     pEnc->mbParam.edged_height) < 0)
296                    goto xvid_err_memory3;
297            if (image_create
298                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
299                     pEnc->mbParam.edged_height) < 0)
300                    goto xvid_err_memory3;
301            if (image_create
302                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
303                     pEnc->mbParam.edged_height) < 0)
304                    goto xvid_err_memory3;
305    
306          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          pParam->handle = (void *) pEnc;
         {  
                 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);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
         }  
307    
308          pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height);          if (pParam->rc_bitrate) {
309          if (pEnc->pMBs == NULL)                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
310          {                                                  pParam->rc_reaction_delay_factor,
311                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                                  pParam->rc_averaging_period, pParam->rc_buffer,
312                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                                  pParam->fbase * 1000 / pParam->fincr,
313                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                                  pParam->max_quantizer, pParam->min_quantizer);
                 image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
                 free(pEnc);  
                 return XVID_ERR_MEMORY;  
314          }          }
315    
316          // init macroblock array          init_timer();
         for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)  
         {  
                 pEnc->pMBs[i].dquant = NO_CHANGE;  
         }  
317    
318          pParam->handle = (void *)pEnc;          return XVID_ERR_OK;
319    
320          if (pParam->bitrate)          /*
321          {           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
322                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width,           */
323                                  pParam->height, pParam->max_quantizer, pParam->min_quantizer);  
324          }    xvid_err_memory3:
325    #ifdef _DEBUG_PSNR
326            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
327                                      pEnc->mbParam.edged_height);
328    #endif
329    
330            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
331                                      pEnc->mbParam.edged_height);
332            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
333                                      pEnc->mbParam.edged_height);
334            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
335                                      pEnc->mbParam.edged_height);
336            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
337                                      pEnc->mbParam.edged_height);
338            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
339                                      pEnc->mbParam.edged_height);
340    
341      xvid_err_memory2:
342            xvid_free(pEnc->current->mbs);
343            xvid_free(pEnc->reference->mbs);
344    
345      xvid_err_memory1:
346            xvid_free(pEnc->current);
347            xvid_free(pEnc->reference);
348            xvid_free(pEnc);
349    
350          create_vlc_tables();          pParam->handle = NULL;
351    
352          return XVID_ERR_OK;          return XVID_ERR_MEMORY;
353  }  }
354    
355    /*****************************************************************************
356     * Encoder destruction
357     *
358     * This function destroy the entire encoder structure created by a previous
359     * successful encoder_create call.
360     *
361     * Returned values (for now only one returned value) :
362     *    - XVID_ERR_OK     - no errors
363     *
364     ****************************************************************************/
365    
366  int encoder_destroy(Encoder * pEnc)  int
367    encoder_destroy(Encoder * pEnc)
368  {  {
369    
370          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
371    
372          free(pEnc->pMBs);          /* All images, reference, current etc ... */
373          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
374          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
375          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
376          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
377          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
378          free(pEnc);                                    pEnc->mbParam.edged_height);
379            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
380                                      pEnc->mbParam.edged_height);
381            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
382                                      pEnc->mbParam.edged_height);
383    
384    #ifdef _DEBUG_PSNR
385            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
386                                      pEnc->mbParam.edged_height);
387    #endif
388    
389            /* Encoder structure */
390            xvid_free(pEnc->current->mbs);
391            xvid_free(pEnc->current);
392    
393          destroy_vlc_tables();          xvid_free(pEnc->reference->mbs);
394            xvid_free(pEnc->reference);
395    
396            xvid_free(pEnc);
397    
398          return XVID_ERR_OK;          return XVID_ERR_OK;
399  }  }
400    
401  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
402    void inc_frame_num(Encoder * pEnc)
403    {
404            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
405            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
406            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
407    }
408    
409    /*****************************************************************************
410     * "original" IP frame encoder entry point
411     *
412     * Returned values :
413     *    - XVID_ERR_OK     - no errors
414     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
415     *                        format
416     ****************************************************************************/
417    
418    int
419    encoder_encode(Encoder * pEnc,
420                               XVID_ENC_FRAME * pFrame,
421                               XVID_ENC_STATS * pResult)
422  {  {
423          uint16_t x, y;          uint16_t x, y;
424          Bitstream bs;          Bitstream bs;
425          uint32_t bits;          uint32_t bits;
         uint16_t quant_type = 0;  
426          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
427    
428    #ifdef _DEBUG_PSNR
429            float psnr;
430            uint8_t temp[128];
431    #endif
432    
433          start_global_timer();          start_global_timer();
434    
435          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 226  Line 437 
437          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
438          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
439    
440          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
441          pEnc->mbParam.motion_flags = pFrame->motion;  
442            pEnc->current->global_flags = pFrame->general;
443            pEnc->current->motion_flags = pFrame->motion;
444            pEnc->current->seconds = pEnc->mbParam.m_seconds;
445            pEnc->current->ticks = pEnc->mbParam.m_ticks;
446            pEnc->mbParam.hint = &pFrame->hint;
447    
448          start_timer();          start_timer();
449          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
450                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
451          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
452                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
453          stop_conv_timer();          stop_conv_timer();
454    
455    #ifdef _DEBUG_PSNR
456            image_copy(&pEnc->sOriginal, &pEnc->current->image,
457                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
458    #endif
459    
460            emms();
461    
462          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
463    
464          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
465          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
466                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
467          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
468          }          }
469    
470          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
471          {                  int *temp_dquants =
472                  int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int));                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
473                                                                    pEnc->mbParam.mb_height * sizeof(int),
474                                                                    CACHE_LINE);
475    
476                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width,                  pEnc->current->quant =
477                                                              temp_dquants, pFrame->quant, pFrame->quant,                          adaptive_quantization(pEnc->current->image.y,
478                                                              2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height);                                                                    pEnc->mbParam.edged_width, temp_dquants,
479                                                                      pEnc->current->quant, pEnc->current->quant,
480                                                                      2 * pEnc->current->quant,
481                                                                      pEnc->mbParam.mb_width,
482                                                                      pEnc->mbParam.mb_height);
483    
484                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
485                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
486                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
487                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
488                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
489    
490    
491                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
492    
493                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
494                          }                          }
495                  free(temp_dquants);  
496    #undef OFFSET
497          }          }
498    
499          if(pEnc->mbParam.global_flags & XVID_H263QUANT)                  xvid_free(temp_dquants);
500                  quant_type = H263_QUANT;          }
         else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT)  
                 quant_type = MPEG4_QUANT;  
501    
502          if(pEnc->mbParam.quant_type != quant_type) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
503                  pEnc->mbParam.quant_type = quant_type;                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
504                  write_vol_header = 1;                  write_vol_header = 1;
505          }                  pEnc->mbParam.m_quant_type = H263_QUANT;
506          else          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
507                  write_vol_header = 0;                  int matrix1_changed, matrix2_changed;
508    
509          if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0)                  matrix1_changed = matrix2_changed = 0;
510          {  
511                  int ret1, ret2;                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
512                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                          write_vol_header = 1;
513                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);  
514                    pEnc->mbParam.m_quant_type = MPEG4_QUANT;
515    
516                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
517                            if (pFrame->quant_intra_matrix != NULL)
518                                    matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
519                            if (pFrame->quant_inter_matrix != NULL)
520                                    matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
521                    } else {
522                            matrix1_changed = set_intra_matrix(get_default_intra_matrix());
523                            matrix2_changed = set_inter_matrix(get_default_inter_matrix());
524                    }
525                  if(write_vol_header == 0)                  if(write_vol_header == 0)
526                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
527          }          }
528    
529          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
530          {                  if ((pEnc->iFrameNum == 0)
531                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
532                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
533                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
534                  else                  } else {
535                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
536          }          }
537          else          } else {
538          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
539                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
540                  else                  } else {
541                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
542          }          }
543    
544          BitstreamPutBits(&bs, 0xFFFF, 16);          }
545          BitstreamPutBits(&bs, 0xFFFF, 16);  
546            /* Fix from CVS_HEAD (2003-03-17)
547             *
548             * Relic from OpenDivX - now disabled
549             *
550             * BitstreamPutBits(&bs, 0xFFFF, 16);
551             * BitstreamPutBits(&bs, 0xFFFF, 16);
552             */
553          BitstreamPad(&bs);          BitstreamPad(&bs);
554          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
555    
556          if (pResult)          if (pResult) {
557          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
558                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
559                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
560                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
561                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
562          }          }
563    
564          if (pEnc->bitrate)          emms();
565          {  
566                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);          if (pFrame->quant == 0) {
567          }                  RateControlUpdate(&pEnc->rate_control, (int16_t)pEnc->current->quant,
568                                                      pFrame->length, pFrame->intra);
569            }
570    #ifdef _DEBUG_PSNR
571            psnr =
572                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
573                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
574                                       pEnc->mbParam.height);
575    
576            snprintf(temp, 127, "PSNR: %f\n", psnr);
577            DEBUG(temp);
578    #endif
579    
580            inc_frame_num(pEnc);
581          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
582    
583          stop_global_timer();          stop_global_timer();
584          write_timer();          write_timer();
# Line 332  Line 587 
587  }  }
588    
589    
590  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
591    CodeIntraMB(Encoder * pEnc,
592                            MACROBLOCK * pMB)
593    {
594    
595          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
596    
597          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
598                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
599                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
600            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
601            pMB->sad16 = 0;
602    
603            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
604                    if (pMB->dquant != NO_CHANGE) {
605                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
606                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
607    
608                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
609                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
610                            if (pEnc->current->quant < 1)
611                                    pEnc->current->quant = 1;
612                  }                  }
613          }          }
614    
615          pMB->quant = pEnc->mbParam.quant;          pMB->quant = pEnc->current->quant;
616  }  }
617    
618    
619  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  #define FCODEBITS       3
620    #define MODEBITS        5
621    
622    void
623    HintedMESet(Encoder * pEnc,
624                            int *intra)
625  {  {
626          int16_t dct_codes[6][64];          HINTINFO *hint;
627          int16_t qcoeff[6][64];          Bitstream bs;
628            int length, high;
629            uint32_t x, y;
630    
631            hint = pEnc->mbParam.hint;
632    
633            if (hint->rawhints) {
634                    *intra = hint->mvhint.intra;
635            } else {
636                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
637                    *intra = BitstreamGetBit(&bs);
638            }
639    
640            if (*intra) {
641                    return;
642            }
643    
644            pEnc->current->fcode = (hint->rawhints != 0) ?
645                    (uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);
646    
647            length = pEnc->current->fcode + 5;
648            high = 1 << (length - 1);
649    
650            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
651                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
652                            MACROBLOCK *pMB =
653                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
654                            MVBLOCKHINT *bhint =
655                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
656                            VECTOR pred;
657                            VECTOR tmp;
658                            int vec;
659    
660                            pMB->mode = (hint->rawhints != 0) ?
661                                    (uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS);
662    
663                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
664                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
665    
666                            if (pMB->mode == MODE_INTER) {
667                                    tmp.x = (hint->rawhints) ?
668                                            bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length);
669                                    tmp.y = (hint->rawhints) ?
670                                            bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length);
671                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
672                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
673    
674                                    pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
675    
676                                    for (vec = 0; vec < 4; ++vec) {
677                                            pMB->mvs[vec].x = tmp.x;
678                                            pMB->mvs[vec].y = tmp.y;
679                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
680                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
681                                    }
682                            } else if (pMB->mode == MODE_INTER4V) {
683                                    for (vec = 0; vec < 4; ++vec) {
684                                            tmp.x = (hint->rawhints) ?
685                                                    bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length);
686                                            tmp.y = (hint->rawhints) ?
687                                                    bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length);
688                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
689                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
690    
691                                            pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
692    
693                                            pMB->mvs[vec].x = tmp.x;
694                                            pMB->mvs[vec].y = tmp.y;
695                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
696                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
697                                    }
698                            } else                          /* intra / stuffing / not_coded */
699                            {
700                                    for (vec = 0; vec < 4; ++vec) {
701                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
702                                    }
703                            }
704    
705                            if (pMB->mode == MODE_INTER4V &&
706                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
707                                    && pMB->dquant != NO_CHANGE) {
708                                    pMB->mode = MODE_INTRA;
709    
710                                    for (vec = 0; vec < 4; ++vec) {
711                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
712                                    }
713                            }
714                    }
715            }
716    }
717    
718    
719    void
720    HintedMEGet(Encoder * pEnc,
721                            int intra)
722    {
723            HINTINFO *hint;
724            Bitstream bs;
725            uint32_t x, y;
726            int length, high;
727    
728            hint = pEnc->mbParam.hint;
729    
730            if (hint->rawhints) {
731                    hint->mvhint.intra = intra;
732            } else {
733                    BitstreamInit(&bs, hint->hintstream, 0);
734                    BitstreamPutBit(&bs, intra);
735            }
736    
737            if (intra) {
738                    if (!hint->rawhints) {
739                            BitstreamPad(&bs);
740                            hint->hintlength = BitstreamLength(&bs);
741                    }
742                    return;
743            }
744    
745            length = pEnc->current->fcode + 5;
746            high = 1 << (length - 1);
747    
748            if (hint->rawhints) {
749                    hint->mvhint.fcode = pEnc->current->fcode;
750            } else {
751                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
752            }
753    
754            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
755                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
756                            MACROBLOCK *pMB =
757                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
758                            MVBLOCKHINT *bhint =
759                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
760                            VECTOR tmp;
761    
762                            if (hint->rawhints) {
763                                    bhint->mode = pMB->mode;
764                            } else {
765                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
766                            }
767    
768                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
769                                    tmp.x = pMB->mvs[0].x;
770                                    tmp.y = pMB->mvs[0].y;
771                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
772                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
773    
774                                    if (hint->rawhints) {
775                                            bhint->mvs[0].x = tmp.x;
776                                            bhint->mvs[0].y = tmp.y;
777                                    } else {
778                                            BitstreamPutBits(&bs, tmp.x, length);
779                                            BitstreamPutBits(&bs, tmp.y, length);
780                                    }
781                            } else if (pMB->mode == MODE_INTER4V) {
782                                    int vec;
783    
784                                    for (vec = 0; vec < 4; ++vec) {
785                                            tmp.x = pMB->mvs[vec].x;
786                                            tmp.y = pMB->mvs[vec].y;
787                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
788                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
789    
790                                            if (hint->rawhints) {
791                                                    bhint->mvs[vec].x = tmp.x;
792                                                    bhint->mvs[vec].y = tmp.y;
793                                            } else {
794                                                    BitstreamPutBits(&bs, tmp.x, length);
795                                                    BitstreamPutBits(&bs, tmp.y, length);
796                                            }
797                                    }
798                            }
799                    }
800            }
801    
802            if (!hint->rawhints) {
803                    BitstreamPad(&bs);
804                    hint->hintlength = BitstreamLength(&bs);
805            }
806    }
807    
808    
809    static int
810    FrameCodeI(Encoder * pEnc,
811                       Bitstream * bs,
812                       uint32_t * pBits)
813    {
814    
815            DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
816            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
817    
818          uint16_t x, y;          uint16_t x, y;
819    
820          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
821          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
822          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
823            pEnc->current->coding_type = I_VOP;
824    
825          BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
826          BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type,  
827                                  pEnc->mbParam.quant,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.fixed_code);  
828    
829          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
830    
# Line 373  Line 833 
833          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
834    
835          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
836                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
837                  {                          MACROBLOCK *pMB =
838                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
839    
840                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
841    
842                          MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
843                                                              dct_codes, qcoeff);
844    
845                          start_timer();                          start_timer();
846                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
847                          stop_prediction_timer();                          stop_prediction_timer();
848    
849                          start_timer();                          start_timer();
850                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
851                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
852                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
853                                    qcoeff[5*64+0]=0;
854                            }
855                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
856                          stop_coding_timer();                          stop_coding_timer();
857                  }                  }
858    
# Line 396  Line 862 
862          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
863          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
864          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
865          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
866    
867          return 1;                                        // intra          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
868                    HintedMEGet(pEnc, 1);
869            }
870    
871            return 1;                                       /* intra */
872  }  }
873    
874    
875  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
876    
877  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
878    FrameCodeP(Encoder * pEnc,
879                       Bitstream * bs,
880                       uint32_t * pBits,
881                       bool force_inter,
882                       bool vol_header)
883  {  {
884          float fSigma;          float fSigma;
885          int16_t dct_codes[6][64];  
886          int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
887            DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
888    
889          int iLimit;          int iLimit;
890          uint32_t x, y;          unsigned int x, y;
891          int iSearchRange;          int iSearchRange;
892          bool bIntra;          int bIntra;
   
         IMAGE *pCurrent = &pEnc->sCurrent;  
         IMAGE *pRef = &pEnc->sReference;  
893    
894          image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          /* IMAGE *pCurrent = &pEnc->current->image; */
895            IMAGE *pRef = &pEnc->reference->image;
896    
897          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          start_timer();
898            image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
899                                       pEnc->mbParam.width, pEnc->mbParam.height);
900            stop_edges_timer();
901    
902            pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
903            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
904            pEnc->current->fcode = pEnc->mbParam.m_fcode;
905    
906          if (!force_inter)          if (!force_inter)
907                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
908                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
909                                       INTRA_THRESHOLD);
910          else          else
911                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
912    
913          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
914                  start_timer();                  start_timer();
915                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
916                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
917                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
918                                                      pEnc->current->rounding_type);
919                  stop_inter_timer();                  stop_inter_timer();
920          }          }
921    
922          start_timer();          start_timer();
923          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
924                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
925                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          } else {
926    
927                    bIntra =
928                            MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
929                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
930                             iLimit);
931    
932            }
933          stop_motion_timer();          stop_motion_timer();
934    
935          if (bIntra == 1)          if (bIntra == 1) {
936                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
937            }
938    
939          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
940    
941          if(vol_header)          if(vol_header)
942                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
943    
944          BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type,          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
                                 pEnc->mbParam.quant,  
                                 pEnc->mbParam.fixed_code);  
945    
946          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
947    
# Line 459  Line 950 
950          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
951          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
952    
953          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
954          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
955                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
956                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
957    
958                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
959    
960                          if (!bIntra)                          if (!bIntra) {
                         {  
961                                  start_timer();                                  start_timer();
962                                  MBMotionCompensation(pMB, x, y, &pEnc->sReference,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
963                                                       &pEnc->vInterH, &pEnc->vInterV,                                                       &pEnc->vInterH, &pEnc->vInterV,
964                                                       &pEnc->vInterHV, &pEnc->sCurrent, dct_codes,                                                                           &pEnc->vInterHV, &pEnc->current->image,
965                                                       pEnc->mbParam.width,                                                                           dct_codes, pEnc->mbParam.width,
966                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
967                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
968                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
969                                  stop_comp_timer();                                  stop_comp_timer();
970    
971                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
972                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
973                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
974                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
975                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
976                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
977                                                    else if (pEnc->current->quant < 1)
978                                                            pEnc->current->quant = 1;
979                                          }                                          }
980                                  }                                  }
981                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
982    
983                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->field_pred = 0;
984                          }  
985                          else                                  pMB->cbp =
986                          {                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
987                                                                              dct_codes, qcoeff);
988                            } else {
989                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
990                                  MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
991                          }                                                                    dct_codes, qcoeff);
992    
993                          start_timer();                          start_timer();
994                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
995                          stop_prediction_timer();                          stop_prediction_timer();
996                            }
997    
998                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
999                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1000                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1001                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1002                                   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)  
                         {  
1003                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1004                          }                          }  else {
                         else  
                         {  
1005                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1006                          }                          }
1007    
1008                          start_timer();                          start_timer();
1009                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1010                            /* Finished processing the MB, now check if to CODE or SKIP */
1011    
1012                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1013                                    pMB->mvs[0].y == 0) {
1014    
1015                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1016    
1017                            }
1018                            else {
1019                                    if (pEnc->current->global_flags & XVID_GREYSCALE) {
1020                                            pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1021                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1022                                            qcoeff[5*64+0]=0;
1023                                    }
1024                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1025                            }
1026    
1027                          stop_coding_timer();                          stop_coding_timer();
1028                  }                  }
1029          }          }
1030    
1031          emms();          emms();
1032    
1033            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1034                    HintedMEGet(pEnc, 0);
1035            }
1036    
1037          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1038                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1039    
1040          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1041    
1042          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1043    
1044          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1045              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        /* maximum search range 128 */
1046          {          {
1047                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1048                  iSearchRange *= 2;                  iSearchRange *= 2;
1049          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1050                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1051                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1052                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     /* minimum search range 16 */
1053          {          {
1054                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1055                  iSearchRange /= 2;                  iSearchRange /= 2;
1056          }          }
1057    
# Line 552  Line 1059 
1059    
1060          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1061    
1062          return 0;                                        // inter          return 0;                                       /* inter */
1063    
1064  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.90.2.2

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