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

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.84

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