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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.82

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