[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 108, Sun Apr 7 11:57:47 2002 UTC revision 686, Thu Nov 28 07:27:37 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  - Encoder main module -
5     *
6     *  Copyright(C) 2002 Michael Militzer <isibaar@xvid.org>
7     *               2002 Peter Ross <pross@xvid.org>
8     *               2002 Daniel Smith <danielsmith@astroboymail.com>
9     *
10     *  This 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.89 2002-11-28 07:27:37 suxen_drol 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_buffersize <= 0)          if (pParam->rc_averaging_period <= 0)
194                  pParam->rc_buffersize = 16;                  pParam->rc_averaging_period = 100;
195    
196            if (pParam->rc_buffer <= 0)
197                    pParam->rc_buffer = 100;
198    
199            /* Max and min quantizers */
200    
201          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
202                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 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 * 1000 / 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    
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                                      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          xvid_free(pEnc);          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 209  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;          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          BitstreamInit(&bs, pFrame->bitstream, 0);                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
458    #endif
459    
460          if (pFrame->quant == 0)          emms();
         {  
                 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 333  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  #define FCODEBITS       3  #define FCODEBITS       3
615  #define MODEBITS        5  #define MODEBITS        5
616    
617  void HintedMESet(Encoder * pEnc, int * intra)  void
618    HintedMESet(Encoder * pEnc,
619                            int *intra)
620  {  {
621          HINTINFO * hint;          HINTINFO * hint;
622          Bitstream bs;          Bitstream bs;
# Line 364  Line 625 
625    
626          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
627    
628          if (hint->rawhints)          if (hint->rawhints) {
         {  
629                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
630          }          } else {
         else  
         {  
631                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
632                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
633          }          }
634    
635          if (*intra)          if (*intra) {
         {  
636                  return;                  return;
637          }          }
638    
639          pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
640                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
641                                                                                                                                     FCODEBITS);
642    
643          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
644          high    = 1 << (length - 1);          high    = 1 << (length - 1);
645    
646          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
647          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
648                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
649                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
650                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
651                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
652                          VECTOR pred[4];                          VECTOR pred;
653                          VECTOR tmp;                          VECTOR tmp;
                         int dummy[4];  
654                          int vec;                          int vec;
655    
656                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
657                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
658                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                                                                                                                                    MODEBITS);
659                          {  
660                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
661                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
662    
663                            if (pMB->mode == MODE_INTER) {
664                                    tmp.x =
665                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
666                                                                                                                                                      length);
667                                    tmp.y =
668                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
669                                                                                                                                                      length);
670                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
671                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
672    
673                                  get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0);
674    
675                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
676                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
677                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
678                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
679                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
680                                  }                                  }
681                          }                          } else if (pMB->mode == MODE_INTER4V) {
682                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
683                          {                                          tmp.x =
684                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
685                                  {                                                  x : BitstreamGetBits(&bs, length);
686                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
687                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
688                                                    y : BitstreamGetBits(&bs, length);
689                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
690                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
691    
692                                          get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec);
693    
694                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
695                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
696                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
697                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
698                                  }                                  }
699                          }                          } else                          /* intra / stuffing / not_coded */
                         else    // intra / intra_q / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
700                                  {                                  {
701                                    for (vec = 0; vec < 4; ++vec) {
702                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
703                                    }
704                            }
705    
706                            if (pMB->mode == MODE_INTER4V &&
707                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
708                                    && pMB->dquant != NO_CHANGE) {
709                                    pMB->mode = MODE_INTRA;
710    
711                                    for (vec = 0; vec < 4; ++vec) {
712                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
713                                  }                                  }
714                          }                          }
# Line 443  Line 717 
717  }  }
718    
719    
720  void HintedMEGet(Encoder * pEnc, int intra)  void
721    HintedMEGet(Encoder * pEnc,
722                            int intra)
723  {  {
724          HINTINFO * hint;          HINTINFO * hint;
725          Bitstream bs;          Bitstream bs;
# Line 452  Line 728 
728    
729          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
730    
731          if (hint->rawhints)          if (hint->rawhints) {
         {  
732                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
733          }          } else {
         else  
         {  
734                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
735                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
736          }          }
737    
738          if (intra)          if (intra) {
739          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
740                          BitstreamPad(&bs);                          BitstreamPad(&bs);
741                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
742                  }                  }
743                  return;                  return;
744          }          }
745    
746          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
747          high    = 1 << (length - 1);          high    = 1 << (length - 1);
748    
749          if (hint->rawhints)          if (hint->rawhints) {
750          {                  hint->mvhint.fcode = pEnc->current->fcode;
751                  hint->mvhint.fcode = pEnc->mbParam.fixed_code;          } else {
752          }                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
         else  
         {  
                 BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS);  
753          }          }
754    
755          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
756          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
757                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
758                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
759                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
760                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
761                          VECTOR tmp;                          VECTOR tmp;
762    
763                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
764                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
765                          }                          } else {
                         else  
                         {  
766                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
767                          }                          }
768    
769                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
770                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
771                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
772                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
773                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
774    
775                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
776                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
777                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
778                                  }                                  } else {
                                 else  
                                 {  
779                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
780                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
781                                  }                                  }
782                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
783                                  int vec;                                  int vec;
784    
785                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
786                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
787                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
788                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
789                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
790    
791                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
792                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
793                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
794                                          }                                          } else {
                                         else  
                                         {  
795                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
796                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
797                                          }                                          }
# Line 545  Line 800 
800                  }                  }
801          }          }
802    
803          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
804                  BitstreamPad(&bs);                  BitstreamPad(&bs);
805                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
806          }          }
807  }  }
808    
809    
810  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
811    FrameCodeI(Encoder * pEnc,
812                       Bitstream * bs,
813                       uint32_t * pBits)
814  {  {
815    
816          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 562  Line 819 
819          uint16_t x, y;          uint16_t x, y;
820    
821          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
822          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
823          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
824            pEnc->current->coding_type = I_VOP;
825    
826            BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
827    
828          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam);  
829    
830          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
831    
# Line 575  Line 834 
834          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
835    
836          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
837                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
838                  {                          MACROBLOCK *pMB =
839                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
840    
841                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
842    
843                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
844                                                              dct_codes, qcoeff);
845    
846                          start_timer();                          start_timer();
847                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
848                          stop_prediction_timer();                          stop_prediction_timer();
849    
850                          start_timer();                          start_timer();
851                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
852                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
853                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
854                                    qcoeff[5*64+0]=0;
855                            }
856                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
857                          stop_coding_timer();                          stop_coding_timer();
858                  }                  }
859    
# Line 598  Line 863 
863          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
864          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
865          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
866          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
867    
868          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
869                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
870          }          }
871    
872          return 1;                                        // intra          return 1;                                       /* intra */
873  }  }
874    
875    
876  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
877    
878  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
879    FrameCodeP(Encoder * pEnc,
880                       Bitstream * bs,
881                       uint32_t * pBits,
882                       bool force_inter,
883                       bool vol_header)
884  {  {
885          float fSigma;          float fSigma;
886    
# Line 619  Line 888 
888          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
889    
890          int iLimit;          int iLimit;
891          uint32_t x, y;          unsigned int x, y;
892          int iSearchRange;          int iSearchRange;
893          bool bIntra;          int bIntra;
894    
895          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
896          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
897    
898          start_timer();          start_timer();
899          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
900                         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);  
901          stop_edges_timer();          stop_edges_timer();
902    
903          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
904            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
905            pEnc->current->fcode = pEnc->mbParam.m_fcode;
906    
907          if (!force_inter)          if (!force_inter)
908                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
909                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
910                                       INTRA_THRESHOLD);
911          else          else
912                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
913    
914          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
915                  start_timer();                  start_timer();
916                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
917                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
918                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
919                                                      pEnc->current->rounding_type);
920                  stop_inter_timer();                  stop_inter_timer();
921          }          }
922    
923          start_timer();          start_timer();
924          if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
925                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
926          }          } else {
927          else  
928          {                  bIntra =
929                  bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
930                                            &pEnc->vInterH, &pEnc->vInterV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
931                                            &pEnc->vInterHV, &pEnc->sCurrent, iLimit);                           iLimit);
932    
933          }          }
934          stop_motion_timer();          stop_motion_timer();
935    
936          if (bIntra == 1)          if (bIntra == 1) {
         {  
937                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
938          }          }
939    
940          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
941    
942          if(vol_header)          if(vol_header)
943                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
944    
945          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
946    
947          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
948    
# Line 682  Line 951 
951          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
952          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
953    
954          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
955          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
956                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
957                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
958    
959                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
960    
961                          if (!bIntra)                          if (!bIntra) {
                         {  
962                                  start_timer();                                  start_timer();
963                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
964                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
965                                                       &pEnc->sReference,                                                                           &pEnc->vInterHV, &pEnc->current->image,
966                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->sCurrent,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
967                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
968                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
969                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
970                                  stop_comp_timer();                                  stop_comp_timer();
971    
972                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
973                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
974                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
975                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
976                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
977                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
978                                                    else if (pEnc->current->quant < 1)
979                                                            pEnc->current->quant = 1;
980                                          }                                          }
981                                  }                                  }
982                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
983    
984                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
985    
986                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->cbp =
987                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
988                          else                                                                            dct_codes, qcoeff);
989                          {                          } else {
990                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
991                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
992                          }                                                                    dct_codes, qcoeff);
993    
994                          start_timer();                          start_timer();
995                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                                  MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
996                          stop_prediction_timer();                          stop_prediction_timer();
997                            }
998    
999                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1000                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1001                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1002                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1003                                   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)  
                         {  
1004                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1005                          }                          }  else {
                         else  
                         {  
1006                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1007                          }                          }
1008    
1009                          start_timer();                          start_timer();
1010                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);  
1011                            /* Finished processing the MB, now check if to CODE or SKIP */
1012    
1013                            if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 &&
1014                                    pMB->mvs[0].y == 0) {
1015    
1016                                            MBSkip(bs);     /* without B-frames, no precautions are needed */
1017    
1018                            }
1019                            else {
1020                                    if (pEnc->current->global_flags & XVID_GREYSCALE) {
1021                                            pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1022                                            qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1023                                            qcoeff[5*64+0]=0;
1024                                    }
1025                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1026                            }
1027    
1028                          stop_coding_timer();                          stop_coding_timer();
1029                  }                  }
1030          }          }
1031    
1032          emms();          emms();
1033    
1034          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1035                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1036          }          }
1037    
# Line 766  Line 1040 
1040    
1041          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1042    
1043          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1044    
1045          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1046              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        /* maximum search range 128 */
1047          {          {
1048                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1049                  iSearchRange *= 2;                  iSearchRange *= 2;
1050          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1051                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1052                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1053                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     /* minimum search range 16 */
1054          {          {
1055                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1056                  iSearchRange /= 2;                  iSearchRange /= 2;
1057          }          }
1058    
# Line 787  Line 1060 
1060    
1061          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1062    
1063          return 0;                                        // inter          return 0;                                       /* inter */
1064    
1065  }  }

Legend:
Removed from v.108  
changed lines
  Added in v.686

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