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

Diff of /trunk/xvidcore/src/encoder.c

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

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

Legend:
Removed from v.87  
changed lines
  Added in v.815

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