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

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.86

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