[svn] / tags / branch-release-1-0 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /tags/branch-release-1-0/xvidcore/src/encoder.c

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

revision 85, Fri Mar 29 07:08:09 2002 UTC revision 195, Wed Jun 12 20:38:41 2002 UTC
# Line 1  Line 1 
1    /*****************************************************************************
2     *
3     *  XVID MPEG-4 VIDEO CODEC
4     *  -  Encoder main module  -
5     *
6     *  This program is an implementation of a part of one or more MPEG-4
7     *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
8     *  to use this software module in hardware or software products are
9     *  advised that its use may infringe existing patents or copyrights, and
10     *  any such use would be at such party's own risk.  The original
11     *  developer of this software module and his/her company, and subsequent
12     *  editors and their companies, will have no liability for use of this
13     *  software or modifications or derivatives thereof.
14     *
15     *  This program is free software; you can redistribute it and/or modify
16     *  it under the terms of the GNU General Public License as published by
17     *  the Free Software Foundation; either version 2 of the License, or
18     *  (at your option) any later version.
19     *
20     *  This program is distributed in the hope that it will be useful,
21     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     *  GNU General Public License for more details.
24     *
25     *  You should have received a copy of the GNU General Public License
26     *  along with this program; if not, write to the Free Software
27     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28     *
29     ****************************************************************************/
30    
31    /*****************************************************************************
32     *
33     *  History
34     *
35     *  08.05.2002 fix some problem in DEBUG mode;
36     *             MinChen <chenm001@163.com>
37     *  14.04.2002 added FrameCodeB()
38     *
39     *  $Id: encoder.c,v 1.41 2002-06-12 20:38:40 edgomez Exp $
40     *
41     ****************************************************************************/
42    
43  #include <stdlib.h>  #include <stdlib.h>
44  #include <stdio.h>  #include <stdio.h>
45  #include <math.h>  #include <math.h>
# Line 7  Line 49 
49  #include "global.h"  #include "global.h"
50  #include "utils/timer.h"  #include "utils/timer.h"
51  #include "image/image.h"  #include "image/image.h"
52    #include "motion/motion.h"
53  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
54  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
55  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 18  Line 61 
61  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
65     * Local macros
66     ****************************************************************************/
67    
68    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
69    #define SWAP(A,B)    { void * tmp = A; A = B; B = tmp; }
70    
71  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
72  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
73     ****************************************************************************/
74    
75    static int FrameCodeI(Encoder * pEnc,
76                                              Bitstream * bs,
77                                              uint32_t * pBits);
78    
79    static int FrameCodeP(Encoder * pEnc,
80                                              Bitstream * bs,
81                                              uint32_t * pBits,
82                                              bool force_inter,
83                                              bool vol_header);
84    
85    #ifdef BFRAMES
86    static void FrameCodeB(Encoder * pEnc,
87                                               FRAMEINFO * frame,
88                                               Bitstream * bs,
89                                               uint32_t * pBits);
90    #endif
91    
92    /*****************************************************************************
93     * Local data
94     ****************************************************************************/
95    
96  static int DQtab[4] =  static int DQtab[4] = {
 {  
97          -1, -2, 1, 2          -1, -2, 1, 2
98  };  };
99    
100  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
101          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
102  };  };
103    
104    
105  int encoder_create(XVID_ENC_PARAM * pParam)  static void __inline
106    image_null(IMAGE * image)
107    {
108            image->y = image->u = image->v = NULL;
109    }
110    
111    
112    /*****************************************************************************
113     * Encoder creation
114     *
115     * This function creates an Encoder instance, it allocates all necessary
116     * image buffers (reference, current and bframes) and initialize the internal
117     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
118     *
119     * The code seems to be very long but is very basic, mainly memory allocation
120     * and cleaning code.
121     *
122     * Returned values :
123     *    - XVID_ERR_OK     - no errors
124     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
125     *                        cleans the structure before exiting.
126     *                        pParam->handle is also set to NULL.
127     *
128     ****************************************************************************/
129    
130    int
131    encoder_create(XVID_ENC_PARAM * pParam)
132  {  {
133          Encoder *pEnc;          Encoder *pEnc;
134          uint32_t i;          uint32_t i;
# Line 49  Line 142 
142          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
143          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
144    
145          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
146          {  
147            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
148                  pParam->fincr = 1;                  pParam->fincr = 1;
149                  pParam->fbase = 25;                  pParam->fbase = 25;
150          }          }
151    
152          // simplify the "fincr/fbase" fraction          /*
153          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
154             * (neccessary, since windows supplies us with huge numbers)
155             */
156    
157          i = pParam->fincr;          i = pParam->fincr;
158          while (i > 1)          while (i > 1) {
159          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
160                          pParam->fincr /= i;                          pParam->fincr /= i;
161                          pParam->fbase /= i;                          pParam->fbase /= i;
162                          i = pParam->fincr;                          i = pParam->fincr;
# Line 71  Line 165 
165                  i--;                  i--;
166          }          }
167    
168          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
169                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
170    
171                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
172                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
173          }          }
174    
175          if (pParam->bitrate <= 0)          /* Bitrate allocator defaults */
                 pParam->bitrate = 900000;  
176    
177          if (pParam->rc_buffersize <= 0)          if (pParam->rc_bitrate <= 0)
178                  pParam->rc_buffersize = 16;                  pParam->rc_bitrate = 900000;
179    
180            if (pParam->rc_reaction_delay_factor <= 0)
181                    pParam->rc_reaction_delay_factor = 16;
182    
183            if (pParam->rc_averaging_period <= 0)
184                    pParam->rc_averaging_period = 100;
185    
186            if (pParam->rc_buffer <= 0)
187                    pParam->rc_buffer = 100;
188    
189            /* Max and min quantizers */
190    
191          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
192                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
# Line 90  Line 194 
194          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
195                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
196    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
197          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
198                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
199    
200          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
201    
202            if (pParam->max_key_interval == 0)
203                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
204    
205    
206            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
207            if (pEnc == NULL)
208                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
209    
210          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
# Line 110  Line 218 
218          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
219          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
220    
221            pEnc->mbParam.fbase = pParam->fbase;
222            pEnc->mbParam.fincr = pParam->fincr;
223    
224          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
225    
226          /* Fill rate control parameters */          /* Fill rate control parameters */
227    
228          pEnc->mbParam.quant = 4;          pEnc->bitrate = pParam->rc_bitrate;
   
         pEnc->bitrate = pParam->bitrate;  
229    
230          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
231          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->iMaxKeyInterval = pParam->max_key_interval;
232    
233          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;  
         }  
234    
235          if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
236          {          pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
237                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
238                  xvid_free(pEnc);          if (pEnc->current == NULL || pEnc->reference == NULL)
239                  return XVID_ERR_MEMORY;                  goto xvid_err_memory1;
240    
241            /* try to allocate mb memory */
242    
243            pEnc->current->mbs =
244                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
245                                            pEnc->mbParam.mb_height, CACHE_LINE);
246            pEnc->reference->mbs =
247                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
248                                            pEnc->mbParam.mb_height, CACHE_LINE);
249    
250            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
251                    goto xvid_err_memory2;
252    
253            /* try to allocate image memory */
254    
255    #ifdef _DEBUG
256            image_null(&pEnc->sOriginal);
257    #endif
258    #ifdef BFRAMES
259            image_null(&pEnc->f_refh);
260            image_null(&pEnc->f_refv);
261            image_null(&pEnc->f_refhv);
262    #endif
263            image_null(&pEnc->current->image);
264            image_null(&pEnc->reference->image);
265            image_null(&pEnc->vInterH);
266            image_null(&pEnc->vInterV);
267            image_null(&pEnc->vInterVf);
268            image_null(&pEnc->vInterHV);
269            image_null(&pEnc->vInterHVf);
270    
271    #ifdef _DEBUG
272            if (image_create
273                    (&pEnc->sOriginal, pEnc->mbParam.edged_width,
274                     pEnc->mbParam.edged_height) < 0)
275                    goto xvid_err_memory3;
276    #endif
277    #ifdef BFRAMES
278            if (image_create
279                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
280                     pEnc->mbParam.edged_height) < 0)
281                    goto xvid_err_memory3;
282            if (image_create
283                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
284                     pEnc->mbParam.edged_height) < 0)
285                    goto xvid_err_memory3;
286            if (image_create
287                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
288                     pEnc->mbParam.edged_height) < 0)
289                    goto xvid_err_memory3;
290    #endif
291            if (image_create
292                    (&pEnc->current->image, pEnc->mbParam.edged_width,
293                     pEnc->mbParam.edged_height) < 0)
294                    goto xvid_err_memory3;
295            if (image_create
296                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
297                     pEnc->mbParam.edged_height) < 0)
298                    goto xvid_err_memory3;
299            if (image_create
300                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
301                     pEnc->mbParam.edged_height) < 0)
302                    goto xvid_err_memory3;
303            if (image_create
304                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
305                     pEnc->mbParam.edged_height) < 0)
306                    goto xvid_err_memory3;
307            if (image_create
308                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
309                     pEnc->mbParam.edged_height) < 0)
310                    goto xvid_err_memory3;
311            if (image_create
312                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
313                     pEnc->mbParam.edged_height) < 0)
314                    goto xvid_err_memory3;
315            if (image_create
316                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
317                     pEnc->mbParam.edged_height) < 0)
318                    goto xvid_err_memory3;
319    
320    
321    
322            /* B Frames specific init */
323    #ifdef BFRAMES
324    
325            pEnc->mbParam.max_bframes = pParam->max_bframes;
326            pEnc->bquant_ratio = pParam->bquant_ratio;
327            pEnc->bframes = NULL;
328    
329            if (pEnc->mbParam.max_bframes > 0) {
330                    int n;
331    
332                    pEnc->bframes =
333                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
334                                                    CACHE_LINE);
335    
336                    if (pEnc->bframes == NULL)
337                            goto xvid_err_memory3;
338    
339                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
340                            pEnc->bframes[n] = NULL;
341    
342    
343                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
344                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
345    
346                            if (pEnc->bframes[n] == NULL)
347                                    goto xvid_err_memory4;
348    
349                            pEnc->bframes[n]->mbs =
350                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
351                                                            pEnc->mbParam.mb_height, CACHE_LINE);
352    
353                            if (pEnc->bframes[n]->mbs == NULL)
354                                    goto xvid_err_memory4;
355    
356                            image_null(&pEnc->bframes[n]->image);
357    
358                            if (image_create
359                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
360                                     pEnc->mbParam.edged_height) < 0)
361                                    goto xvid_err_memory4;
362    
363                    }
364            }
365    
366            pEnc->bframenum_head = 0;
367            pEnc->bframenum_tail = 0;
368            pEnc->flush_bframes = 0;
369    
370            pEnc->mbParam.m_seconds = 0;
371            pEnc->mbParam.m_ticks = 0;
372    #endif
373    
374            pParam->handle = (void *) pEnc;
375    
376            if (pParam->rc_bitrate) {
377                    RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
378                                                    pParam->rc_reaction_delay_factor,
379                                                    pParam->rc_averaging_period, pParam->rc_buffer,
380                                                    pParam->fbase * 1000 / pParam->fincr,
381                                                    pParam->max_quantizer, pParam->min_quantizer);
382          }          }
383    
384          if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          init_timer();
385          {  
386                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          return XVID_ERR_OK;
387                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
388                  xvid_free(pEnc);          /*
389                  return XVID_ERR_MEMORY;           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
390             */
391    #ifdef BFRAMES
392      xvid_err_memory4:
393            for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
394    
395                    if (pEnc->bframes[i] == NULL)
396                            continue;
397    
398                    image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
399                                              pEnc->mbParam.edged_height);
400    
401                    xvid_free(pEnc->bframes[i]->mbs);
402    
403                    xvid_free(pEnc->bframes[i]);
404    
405          }          }
406    
407          if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)          xvid_free(pEnc->bframes);
408          {  
409                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #endif
410                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
411                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);    xvid_err_memory3:
412    #ifdef _DEBUG
413            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
414                                      pEnc->mbParam.edged_height);
415    #endif
416    
417    #ifdef BFRAMES
418            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
419                                      pEnc->mbParam.edged_height);
420            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
421                                      pEnc->mbParam.edged_height);
422            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
423                                      pEnc->mbParam.edged_height);
424    #endif
425    
426            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
427                                      pEnc->mbParam.edged_height);
428            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
429                                      pEnc->mbParam.edged_height);
430            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
431                                      pEnc->mbParam.edged_height);
432            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
433                                      pEnc->mbParam.edged_height);
434            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
435                                      pEnc->mbParam.edged_height);
436            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
437                                      pEnc->mbParam.edged_height);
438            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
439                                      pEnc->mbParam.edged_height);
440    
441      xvid_err_memory2:
442            xvid_free(pEnc->current->mbs);
443            xvid_free(pEnc->reference->mbs);
444    
445      xvid_err_memory1:
446            xvid_free(pEnc->current);
447            xvid_free(pEnc->reference);
448                  xvid_free(pEnc);                  xvid_free(pEnc);
449    
450            pParam->handle = NULL;
451    
452                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
453          }          }
454    
455          if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0)  /*****************************************************************************
456     * Encoder destruction
457     *
458     * This function destroy the entire encoder structure created by a previous
459     * successful encoder_create call.
460     *
461     * Returned values (for now only one returned value) :
462     *    - XVID_ERR_OK     - no errors
463     *
464     ****************************************************************************/
465    
466    int
467    encoder_destroy(Encoder * pEnc)
468          {          {
469                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          ENC_CHECK(pEnc);
470                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
471                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* B Frames specific */
472                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  #ifdef BFRAMES
473                  xvid_free(pEnc);          int i;
474                  return XVID_ERR_MEMORY;  
475            for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
476    
477                    if (pEnc->bframes[i] == NULL)
478                            continue;
479    
480                    image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
481                                              pEnc->mbParam.edged_height);
482    
483                    xvid_free(pEnc->bframes[i]->mbs);
484    
485                    xvid_free(pEnc->bframes[i]);
486    
487          }          }
488    
489          pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE);          xvid_free(pEnc->bframes);
490          if (pEnc->pMBs == NULL)  
491          {  #endif
492                  image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
493                  image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          /* All images, reference, current etc ... */
494                  image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
495                  image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
496                  image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                    pEnc->mbParam.edged_height);
497            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
498                                      pEnc->mbParam.edged_height);
499            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
500                                      pEnc->mbParam.edged_height);
501            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
502                                      pEnc->mbParam.edged_height);
503            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
504                                      pEnc->mbParam.edged_height);
505            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
506                                      pEnc->mbParam.edged_height);
507            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
508                                      pEnc->mbParam.edged_height);
509    #ifdef BFRAMES
510            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
511                                      pEnc->mbParam.edged_height);
512            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
513                                      pEnc->mbParam.edged_height);
514            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
515                                      pEnc->mbParam.edged_height);
516    #endif
517    #ifdef _DEBUG
518            image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
519                                      pEnc->mbParam.edged_height);
520    #endif
521    
522            /* Encoder structure */
523    
524            xvid_free(pEnc->current->mbs);
525            xvid_free(pEnc->current);
526    
527            xvid_free(pEnc->reference->mbs);
528            xvid_free(pEnc->reference);
529    
530                  xvid_free(pEnc);                  xvid_free(pEnc);
531                  return XVID_ERR_MEMORY;  
532            return XVID_ERR_OK;
533          }          }
534    
535          // init macroblock array  /*****************************************************************************
536          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)   * Frame encoder entry point
537     *
538     * At this moment 2 versions coexist : one for IPB compatible encoder,
539     *                                     another one for the old IP encoder.
540     *
541     * Returned values :
542     *    - XVID_ERR_OK     - no errors
543     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
544     *                        format
545     ****************************************************************************/
546    
547    
548    #ifdef BFRAMES
549    /*****************************************************************************
550     * Frame encoder entry point for IPB capable encoder
551     ****************************************************************************/
552    int
553    encoder_encode(Encoder * pEnc,
554                               XVID_ENC_FRAME * pFrame,
555                               XVID_ENC_STATS * pResult)
556          {          {
557                  pEnc->pMBs[i].dquant = NO_CHANGE;          uint16_t x, y;
558            Bitstream bs;
559            uint32_t bits;
560    
561    #ifdef _DEBUG
562            float psnr;
563            char temp[128];
564    #endif
565    
566            ENC_CHECK(pEnc);
567            ENC_CHECK(pFrame);
568    
569            start_global_timer();
570    
571            BitstreamInit(&bs, pFrame->bitstream, 0);
572    
573            /*
574             * bframe "flush" code
575             */
576    
577            if ((pFrame->image == NULL || pEnc->flush_bframes)
578                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
579    
580                    if (pEnc->flush_bframes == 0) {
581                            /*
582                             * we have reached the end of stream without getting
583                             * a future reference frame... so encode last final
584                             * frame as a pframe
585                             */
586    
587                            /* ToDo : remove dprintf calls */
588                            /*
589                               dprintf("--- PFRAME (final frame correction) --- ");
590                             */
591                            pEnc->bframenum_tail--;
592                            SWAP(pEnc->current, pEnc->reference);
593    
594                            SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
595    
596                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
597    
598                            BitstreamPad(&bs);
599                            pFrame->length = BitstreamLength(&bs);
600                            pFrame->input_consumed = 0;
601                            pFrame->intra = 0;
602    
603                            return XVID_ERR_OK;
604          }          }
605    
606          pParam->handle = (void *)pEnc;                  /* ToDo : remove dprintf calls */
607                    /*
608                       dprintf("--- BFRAME (flush) --- ");
609                     */
610                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
611                    pEnc->bframenum_head++;
612    
613          if (pParam->bitrate)  
614          {                  BitstreamPad(&bs);
615                  RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase * 100 / pParam->fincr,                  pFrame->length = BitstreamLength(&bs);
616                                  pParam->max_quantizer, pParam->min_quantizer);                  pFrame->input_consumed = 0;
617                    pFrame->intra = 0;
618    
619                    return XVID_ERR_OK;
620          }          }
621    
622          create_vlc_tables();          if (pFrame->image == NULL) {
623          init_timer();                  pFrame->length = 0;
624                    pFrame->input_consumed = 1;
625                    pFrame->intra = 0;
626    
627          return XVID_ERR_OK;          return XVID_ERR_OK;
628  }  }
629    
630            if (pEnc->bframenum_head > 0) {
631                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
632            }
633    
634  int encoder_destroy(Encoder * pEnc)          pEnc->flush_bframes = 0;
 {  
         ENC_CHECK(pEnc);  
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
635    
636          xvid_free(pEnc->pMBs);          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);           * Well there was a separation here so i put it in ANSI C
638          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);           * comment style :-)
639          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
640          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
641          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          SWAP(pEnc->current, pEnc->reference);
642          xvid_free(pEnc);  
643            EMMS();
644    
645            if (pFrame->quant == 0)
646                    pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
647            else
648                    pEnc->current->quant = pFrame->quant;
649    
650            if (pEnc->current->quant < 1)
651                    pEnc->current->quant = 1;
652    
653            if (pEnc->current->quant > 31)
654                    pEnc->current->quant = 31;
655    
656            pEnc->current->global_flags = pFrame->general;
657            pEnc->current->motion_flags = pFrame->motion;
658            pEnc->current->seconds = pEnc->mbParam.m_seconds;
659            pEnc->current->ticks = pEnc->mbParam.m_ticks;
660            /* ToDo : dynamic fcode (in both directions) */
661            pEnc->current->fcode = pEnc->mbParam.m_fcode;
662            pEnc->current->bcode = pEnc->mbParam.m_fcode;
663    
664            start_timer();
665            if (image_input
666                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
667                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
668                    return XVID_ERR_FORMAT;
669            stop_conv_timer();
670    
671          destroy_vlc_tables();  #ifdef _DEBUG
672            image_copy(&pEnc->sOriginal, &pEnc->current->image,
673                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
674    #endif
675    
676            EMMS();
677    
678            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679             * Luminance masking
680             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
681    
682            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
683                    int *temp_dquants =
684                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
685                                                                    pEnc->mbParam.mb_height * sizeof(int),
686                                                                    CACHE_LINE);
687    
688                    pEnc->current->quant =
689                            adaptive_quantization(pEnc->current->image.y,
690                                                                      pEnc->mbParam.edged_width, temp_dquants,
691                                                                      pEnc->current->quant, pEnc->current->quant,
692                                                                      2 * pEnc->current->quant,
693                                                                      pEnc->mbParam.mb_width,
694                                                                      pEnc->mbParam.mb_height);
695    
696                    for (y = 0; y < pEnc->mbParam.mb_height; y++) {
697    
698    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
699    
700                            for (x = 0; x < pEnc->mbParam.mb_width; x++) {
701                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
702    
703                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
704                            }
705    
706    #undef OFFSET
707    
708                    }
709    
710                    xvid_free(temp_dquants);
711            }
712    
713            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714             * ivop/pvop/bvop selection
715             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
716    
717    
718            if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||
719                    (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
720                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
721                    || image_mad(&pEnc->reference->image, &pEnc->current->image,
722                                             pEnc->mbParam.edged_width, pEnc->mbParam.width,
723                                             pEnc->mbParam.height) > 30) {
724                    /*
725                     * This will be coded as an Intra Frame
726                     */
727    
728                    /* ToDo : Remove dprintf calls */
729                    /*
730                       dprintf("--- IFRAME ---");
731                     */
732    
733                    FrameCodeI(pEnc, &bs, &bits);
734    
735                    pFrame->intra = 1;
736                    pEnc->flush_bframes = 1;
737    
738                    /*
739                     * NB : sequences like "IIBB" decode fine with msfdam but,
740                     *      go screwy with divx 5.00
741                     */
742            } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
743                    /*
744                     * This will be coded as a Predicted Frame
745                     */
746    
747                    /* ToDo : Remove dprintf calls */
748                    /*
749                       dprintf("--- PFRAME ---");
750                     */
751    
752                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
753                    pFrame->intra = 0;
754                    pEnc->flush_bframes = 1;
755            } else {
756                    /*
757                     * This will be coded as a Bidirectional Frame
758                     */
759    
760                    /* ToDo : Remove dprintf calls */
761                    /*
762                       dprintf("--- BFRAME (store) ---  head=%i tail=%i",
763                       pEnc->bframenum_head,
764                       pEnc->bframenum_tail);
765                     */
766    
767                    if (pFrame->bquant < 1) {
768                            pEnc->current->quant =
769                                    ((pEnc->reference->quant +
770                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
771                    } else {
772                            pEnc->current->quant = pFrame->bquant;
773                    }
774    
775                    /* store frame into bframe buffer & swap ref back to current */
776                    SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
777                    SWAP(pEnc->current, pEnc->reference);
778    
779                    pEnc->bframenum_tail++;
780    
781                    pFrame->intra = 0;
782                    pFrame->length = 0;
783                    pFrame->input_consumed = 1;
784    
785                    pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
786                    if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {
787                            pEnc->mbParam.m_seconds++;
788                            pEnc->mbParam.m_ticks = 0;
789                    }
790    
791          return XVID_ERR_OK;          return XVID_ERR_OK;
792  }  }
793    
794  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)          BitstreamPad(&bs);
795            pFrame->length = BitstreamLength(&bs);
796    
797            if (pResult) {
798                    pResult->quant = pEnc->current->quant;
799                    pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
800                    pResult->kblks = pEnc->sStat.kblks;
801                    pResult->mblks = pEnc->sStat.mblks;
802                    pResult->ublks = pEnc->sStat.ublks;
803            }
804    
805            EMMS();
806    
807    #ifdef _DEBUG
808            psnr =
809                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
810                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
811                                       pEnc->mbParam.height);
812    
813            snprintf(temp, 127, "PSNR: %f\n", psnr);
814            DEBUG(temp);
815    #endif
816    
817            if (pFrame->quant == 0) {
818                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
819                                                      pFrame->length, pFrame->intra);
820            }
821    
822            pEnc->iFrameNum++;
823            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
824            if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) {
825                    pEnc->mbParam.m_seconds++;
826                    pEnc->mbParam.m_ticks = 0;
827            }
828            pFrame->input_consumed = 1;
829    
830            stop_global_timer();
831            write_timer();
832    
833            return XVID_ERR_OK;
834    }
835    #else
836    /*****************************************************************************
837     * Frame encoder entry point for IP capable encoder
838     ****************************************************************************/
839    int
840    encoder_encode(Encoder * pEnc,
841                               XVID_ENC_FRAME * pFrame,
842                               XVID_ENC_STATS * pResult)
843  {  {
844          uint16_t x, y;          uint16_t x, y;
845          Bitstream bs;          Bitstream bs;
846          uint32_t bits;          uint32_t bits;
847          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
848    
849    #ifdef _DEBUG
850            float psnr;
851            uint8_t temp[128];
852    #endif
853    
854          start_global_timer();          start_global_timer();
855    
856          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
# Line 227  Line 858 
858          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
859          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
860    
861          pEnc->mbParam.global_flags = pFrame->general;          SWAP(pEnc->current, pEnc->reference);
862          pEnc->mbParam.motion_flags = pFrame->motion;  
863            pEnc->current->global_flags = pFrame->general;
864            pEnc->current->motion_flags = pFrame->motion;
865            pEnc->mbParam.hint = &pFrame->hint;
866    
867          start_timer();          start_timer();
868          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,          if (image_input
869                          pFrame->image, pFrame->colorspace))                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
870          {                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
871                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
872          stop_conv_timer();          stop_conv_timer();
873    
874    #ifdef _DEBUG
875            image_copy(&pEnc->sOriginal, &pEnc->current->image,
876                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
877    #endif
878    
879          EMMS();          EMMS();
880    
881          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
882    
883          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
884          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
885                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
886          }                  pEnc->current->quant = pFrame->quant;
887          else          }
888          {  
889                  pEnc->mbParam.quant = pFrame->quant;          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
890          }                  int *temp_dquants =
891                            (int *) xvid_malloc(pEnc->mbParam.mb_width *
892          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)                                                                  pEnc->mbParam.mb_height * sizeof(int),
893          {                                                                  CACHE_LINE);
894                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);  
895                    pEnc->current->quant =
896                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y,                          adaptive_quantization(pEnc->current->image.y,
897                                                              pEnc->mbParam.width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
898                                                              temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
899                                                              pEnc->mbParam.quant,                                                                    2 * pEnc->current->quant,
                                                             pEnc->mbParam.quant,  
                                                             2*pEnc->mbParam.quant,  
900                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
901                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
902    
903                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
904                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
905                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
906                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
907                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
908    
909    
910                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
911    
912                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
913                            }
914    
915    #undef OFFSET
916                          }                          }
917    
918                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
919          }          }
920    
921          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
922                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
923                          write_vol_header = 1;                          write_vol_header = 1;
924                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
925          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
926          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
927    
928                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
929    
930                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
931                          write_vol_header = 1;                          write_vol_header = 1;
932    
933                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
934    
935                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
936                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
937                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
938                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
939                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
940                  }                  } else {
941                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
942                          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());  
943                  }                  }
944                  if(write_vol_header == 0)                  if(write_vol_header == 0)
945                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
946          }          }
947    
948          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
949          {                  if ((pEnc->iFrameNum == 0)
950                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->iMaxKeyInterval > 0)
951                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
   
952                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
953                  else                  } else {
954                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
955          }          }
956          else          } else {
957          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
958                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
959                  else                  } else {
960                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
961          }          }
962    
963            }
964    
965          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
966          BitstreamPutBits(&bs, 0xFFFF, 16);          BitstreamPutBits(&bs, 0xFFFF, 16);
967          BitstreamPad(&bs);          BitstreamPad(&bs);
968          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
969    
970          if (pResult)          if (pResult) {
971          {                  pResult->quant = pEnc->current->quant;
                 pResult->quant = pEnc->mbParam.quant;  
972                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
973                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
974                  pResult->mblks = pEnc->sStat.mblks;                  pResult->mblks = pEnc->sStat.mblks;
# Line 335  Line 977 
977    
978          EMMS();          EMMS();
979    
980          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
981          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
982                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);                                                    pFrame->length, pFrame->intra);
983          }          }
984    #ifdef _DEBUG
985            psnr =
986                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
987                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
988                                       pEnc->mbParam.height);
989    
990            snprintf(temp, 127, "PSNR: %f\n", psnr);
991            DEBUG(temp);
992    #endif
993    
994          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
995    
996          stop_global_timer();          stop_global_timer();
997          write_timer();          write_timer();
998    
999          return XVID_ERR_OK;          return XVID_ERR_OK;
1000  }  }
1001    #endif
1002    
1003    
1004  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1005    CodeIntraMB(Encoder * pEnc,
1006                            MACROBLOCK * pMB)
1007    {
1008    
1009          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1010    
1011          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1012                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1013                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1014            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1015            pMB->sad16 = 0;
1016    
1017            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1018                    if (pMB->dquant != NO_CHANGE) {
1019                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1020                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1021    
1022                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
1023                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
1024                            if (pEnc->current->quant < 1)
1025                                    pEnc->current->quant = 1;
1026                    }
1027                  }                  }
1028    
1029            pMB->quant = pEnc->current->quant;
1030          }          }
1031    
1032          pMB->quant = pEnc->mbParam.quant;  
1033    #define FCODEBITS       3
1034    #define MODEBITS        5
1035    
1036    void
1037    HintedMESet(Encoder * pEnc,
1038                            int *intra)
1039    {
1040            HINTINFO *hint;
1041            Bitstream bs;
1042            int length, high;
1043            uint32_t x, y;
1044    
1045            hint = pEnc->mbParam.hint;
1046    
1047            if (hint->rawhints) {
1048                    *intra = hint->mvhint.intra;
1049            } else {
1050                    BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1051                    *intra = BitstreamGetBit(&bs);
1052            }
1053    
1054            if (*intra) {
1055                    return;
1056            }
1057    
1058            pEnc->current->fcode =
1059                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1060                                                                                                                                     FCODEBITS);
1061    
1062            length = pEnc->current->fcode + 5;
1063            high = 1 << (length - 1);
1064    
1065            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1066                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1067                            MACROBLOCK *pMB =
1068                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1069                            MVBLOCKHINT *bhint =
1070                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1071                            VECTOR pred[4];
1072                            VECTOR tmp;
1073                            int32_t dummy[4];
1074                            int vec;
1075    
1076                            pMB->mode =
1077                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1078                                                                                                                                      MODEBITS);
1079    
1080                            pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1081                            pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1082    
1083                            if (pMB->mode == MODE_INTER) {
1084                                    tmp.x =
1085                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1086                                                                                                                                                      length);
1087                                    tmp.y =
1088                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1089                                                                                                                                                      length);
1090                                    tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1091                                    tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1092    
1093                                    get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,
1094                                                            0, pred, dummy);
1095    
1096                                    for (vec = 0; vec < 4; ++vec) {
1097                                            pMB->mvs[vec].x = tmp.x;
1098                                            pMB->mvs[vec].y = tmp.y;
1099                                            pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;
1100                                            pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
1101                                    }
1102                            } else if (pMB->mode == MODE_INTER4V) {
1103                                    for (vec = 0; vec < 4; ++vec) {
1104                                            tmp.x =
1105                                                    (hint->rawhints) ? bhint->mvs[vec].
1106                                                    x : BitstreamGetBits(&bs, length);
1107                                            tmp.y =
1108                                                    (hint->rawhints) ? bhint->mvs[vec].
1109                                                    y : BitstreamGetBits(&bs, length);
1110                                            tmp.x -= (tmp.x >= high) ? high * 2 : 0;
1111                                            tmp.y -= (tmp.y >= high) ? high * 2 : 0;
1112    
1113                                            get_pmvdata(pEnc->current->mbs, x, y,
1114                                                                    pEnc->mbParam.mb_width, vec, pred, dummy);
1115    
1116                                            pMB->mvs[vec].x = tmp.x;
1117                                            pMB->mvs[vec].y = tmp.y;
1118                                            pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
1119                                            pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
1120                                    }
1121                            } else                          // intra / stuffing / not_coded
1122                            {
1123                                    for (vec = 0; vec < 4; ++vec) {
1124                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1125                                    }
1126                            }
1127    
1128                            if (pMB->mode == MODE_INTER4V &&
1129                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1130                                    && pMB->dquant != NO_CHANGE) {
1131                                    pMB->mode = MODE_INTRA;
1132    
1133                                    for (vec = 0; vec < 4; ++vec) {
1134                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1135                                    }
1136                            }
1137                    }
1138            }
1139  }  }
1140    
1141    
1142  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  void
1143    HintedMEGet(Encoder * pEnc,
1144                            int intra)
1145    {
1146            HINTINFO *hint;
1147            Bitstream bs;
1148            uint32_t x, y;
1149            int length, high;
1150    
1151            hint = pEnc->mbParam.hint;
1152    
1153            if (hint->rawhints) {
1154                    hint->mvhint.intra = intra;
1155            } else {
1156                    BitstreamInit(&bs, hint->hintstream, 0);
1157                    BitstreamPutBit(&bs, intra);
1158            }
1159    
1160            if (intra) {
1161                    if (!hint->rawhints) {
1162                            BitstreamPad(&bs);
1163                            hint->hintlength = BitstreamLength(&bs);
1164                    }
1165                    return;
1166            }
1167    
1168            length = pEnc->current->fcode + 5;
1169            high = 1 << (length - 1);
1170    
1171            if (hint->rawhints) {
1172                    hint->mvhint.fcode = pEnc->current->fcode;
1173            } else {
1174                    BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1175            }
1176    
1177            for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1178                    for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1179                            MACROBLOCK *pMB =
1180                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1181                            MVBLOCKHINT *bhint =
1182                                    &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1183                            VECTOR tmp;
1184    
1185                            if (hint->rawhints) {
1186                                    bhint->mode = pMB->mode;
1187                            } else {
1188                                    BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1189                            }
1190    
1191                            if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
1192                                    tmp.x = pMB->mvs[0].x;
1193                                    tmp.y = pMB->mvs[0].y;
1194                                    tmp.x += (tmp.x < 0) ? high * 2 : 0;
1195                                    tmp.y += (tmp.y < 0) ? high * 2 : 0;
1196    
1197                                    if (hint->rawhints) {
1198                                            bhint->mvs[0].x = tmp.x;
1199                                            bhint->mvs[0].y = tmp.y;
1200                                    } else {
1201                                            BitstreamPutBits(&bs, tmp.x, length);
1202                                            BitstreamPutBits(&bs, tmp.y, length);
1203                                    }
1204                            } else if (pMB->mode == MODE_INTER4V) {
1205                                    int vec;
1206    
1207                                    for (vec = 0; vec < 4; ++vec) {
1208                                            tmp.x = pMB->mvs[vec].x;
1209                                            tmp.y = pMB->mvs[vec].y;
1210                                            tmp.x += (tmp.x < 0) ? high * 2 : 0;
1211                                            tmp.y += (tmp.y < 0) ? high * 2 : 0;
1212    
1213                                            if (hint->rawhints) {
1214                                                    bhint->mvs[vec].x = tmp.x;
1215                                                    bhint->mvs[vec].y = tmp.y;
1216                                            } else {
1217                                                    BitstreamPutBits(&bs, tmp.x, length);
1218                                                    BitstreamPutBits(&bs, tmp.y, length);
1219                                            }
1220                                    }
1221                            }
1222                    }
1223            }
1224    
1225            if (!hint->rawhints) {
1226                    BitstreamPad(&bs);
1227                    hint->hintlength = BitstreamLength(&bs);
1228            }
1229    }
1230    
1231    
1232    static int
1233    FrameCodeI(Encoder * pEnc,
1234                       Bitstream * bs,
1235                       uint32_t * pBits)
1236  {  {
1237    
1238          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 378  Line 1241 
1241          uint16_t x, y;          uint16_t x, y;
1242    
1243          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1244          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1245          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1246            pEnc->current->coding_type = I_VOP;
1247    
1248          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1249          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
1250    
1251          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1252    
# Line 391  Line 1255 
1255          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1256    
1257          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1258                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1259                  {                          MACROBLOCK *pMB =
1260                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1261    
1262                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1263    
1264                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1265                                                              dct_codes, qcoeff);
1266    
1267                          start_timer();                          start_timer();
1268                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1269                          stop_prediction_timer();                          stop_prediction_timer();
1270    
1271                          start_timer();                          start_timer();
1272                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1273                          stop_coding_timer();                          stop_coding_timer();
1274                  }                  }
1275    
# Line 414  Line 1279 
1279          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
1280          pEnc->sStat.iMvSum = 0;          pEnc->sStat.iMvSum = 0;
1281          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1282          pEnc->mbParam.fixed_code = 2;          pEnc->mbParam.m_fcode = 2;
1283    
1284            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1285                    HintedMEGet(pEnc, 1);
1286            }
1287    
1288          return 1;                                        // intra          return 1;                                        // intra
1289  }  }
# Line 422  Line 1291 
1291    
1292  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1293    
1294  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1295    FrameCodeP(Encoder * pEnc,
1296                       Bitstream * bs,
1297                       uint32_t * pBits,
1298                       bool force_inter,
1299                       bool vol_header)
1300  {  {
1301          float fSigma;          float fSigma;
1302    
# Line 432  Line 1306 
1306          int iLimit;          int iLimit;
1307          uint32_t x, y;          uint32_t x, y;
1308          int iSearchRange;          int iSearchRange;
1309          bool bIntra;          int bIntra;
1310    
1311          IMAGE *pCurrent = &pEnc->sCurrent;          /* IMAGE *pCurrent = &pEnc->current->image; */
1312          IMAGE *pRef = &pEnc->sReference;          IMAGE *pRef = &pEnc->reference->image;
1313    
1314          start_timer();          start_timer();
1315          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1316                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
1317                         pEnc->mbParam.edged_height,                                     pEnc->current->global_flags & XVID_INTERLACING);
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->mbParam.global_flags & XVID_INTERLACING);  
1318          stop_edges_timer();          stop_edges_timer();
1319    
1320          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1321            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1322            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1323    
1324          if (!force_inter)          if (!force_inter)
1325                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1326                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1327                                       INTRA_THRESHOLD);
1328          else          else
1329                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1330    
1331          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1332                  start_timer();                  start_timer();
1333                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1334                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1335                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1336                                                      pEnc->current->rounding_type);
1337                  stop_inter_timer();                  stop_inter_timer();
1338          }          }
1339    
1340          start_timer();          start_timer();
1341          bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
1342                                    &pEnc->vInterH, &pEnc->vInterV,                  HintedMESet(pEnc, &bIntra);
1343                                    &pEnc->vInterHV, &pEnc->sCurrent, iLimit);          } else {
1344                    bIntra =
1345                            MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1346                                                             &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1347                                                             iLimit);
1348            }
1349          stop_motion_timer();          stop_motion_timer();
1350    
1351          if (bIntra == 1)          if (bIntra == 1) {
1352                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1353            }
1354    
1355          pEnc->mbParam.coding_type = P_VOP;          pEnc->current->coding_type = P_VOP;
1356    
1357          if(vol_header)          if(vol_header)
1358                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1359    
1360          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);
1361    
1362          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1363    
# Line 484  Line 1366 
1366          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1367          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1368    
1369          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1370          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1371                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1372                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1373    
1374                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1375    
1376                          if (!bIntra)                          if (!bIntra) {
                         {  
1377                                  start_timer();                                  start_timer();
1378                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1379                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1380                                                       &pEnc->sReference,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1381                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->sCurrent,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1382                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1383                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1384                                                       pEnc->mbParam.rounding_type);                                                                           pEnc->current->rounding_type);
1385                                  stop_comp_timer();                                  stop_comp_timer();
1386    
1387                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                                  if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1388                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1389                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1390                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1391                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                                  if (pEnc->current->quant > 31)
1392                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                          pEnc->current->quant = 31;
1393                                                    else if (pEnc->current->quant < 1)
1394                                                            pEnc->current->quant = 1;
1395                                          }                                          }
1396                                  }                                  }
1397                                  pMB->quant = pEnc->mbParam.quant;                                  pMB->quant = pEnc->current->quant;
1398    
1399                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1400    
1401                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  pMB->cbp =
1402                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1403                          else                                                                            dct_codes, qcoeff);
1404                          {                          } else {
1405                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1406                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1407                                                                      dct_codes, qcoeff);
1408                          }                          }
1409    
1410                          start_timer();                          start_timer();
1411                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1412                          stop_prediction_timer();                          stop_prediction_timer();
1413    
1414                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1415                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1416                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1417                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1418                                   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)  
                         {  
1419                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1420                          }                          } else {
                         else  
                         {  
1421                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1422                          }                          }
1423    
1424                          start_timer();                          start_timer();
1425                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat);
1426                          stop_coding_timer();                          stop_coding_timer();
1427                  }                  }
1428          }          }
1429    
1430          emms();          emms();
1431    
1432            if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
1433                    HintedMEGet(pEnc, 0);
1434            }
1435    
1436          if (pEnc->sStat.iMvCount == 0)          if (pEnc->sStat.iMvCount == 0)
1437                  pEnc->sStat.iMvCount = 1;                  pEnc->sStat.iMvCount = 1;
1438    
1439          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);
1440    
1441          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
1442    
1443          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
1444              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= 3))        // maximum search range 128
1445          {          {
1446                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
1447                  iSearchRange *= 2;                  iSearchRange *= 2;
1448          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1449                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1450                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1451                   && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16                             && (pEnc->mbParam.m_fcode >= 2))     // minimum search range 16
1452          {          {
1453                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
1454                  iSearchRange /= 2;                  iSearchRange /= 2;
1455          }          }
1456    
# Line 586  Line 1460 
1460    
1461          return 0;                                        // inter          return 0;                                        // inter
1462  }  }
1463    
1464    
1465    #ifdef BFRAMES
1466    static void
1467    FrameCodeB(Encoder * pEnc,
1468                       FRAMEINFO * frame,
1469                       Bitstream * bs,
1470                       uint32_t * pBits)
1471    {
1472            int16_t dct_codes[6 * 64];
1473            int16_t qcoeff[6 * 64];
1474            uint32_t x, y;
1475            VECTOR forward;
1476            VECTOR backward;
1477    
1478            IMAGE *f_ref = &pEnc->reference->image;
1479            IMAGE *b_ref = &pEnc->current->image;
1480    
1481            // forward
1482            image_setedges(f_ref, pEnc->mbParam.edged_width,
1483                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1484                                       pEnc->mbParam.height,
1485                                       frame->global_flags & XVID_INTERLACING);
1486            start_timer();
1487            image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1488                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1489                                              0);
1490            stop_inter_timer();
1491    
1492            // backward
1493            image_setedges(b_ref, pEnc->mbParam.edged_width,
1494                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1495                                       pEnc->mbParam.height,
1496                                       frame->global_flags & XVID_INTERLACING);
1497            start_timer();
1498            image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1499                                              pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1500                                              0);
1501            stop_inter_timer();
1502    
1503            start_timer();
1504            MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1505                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1506                                                     pEnc->current->mbs, b_ref, &pEnc->vInterH,
1507                                                     &pEnc->vInterV, &pEnc->vInterHV);
1508    
1509    
1510            stop_motion_timer();
1511    
1512            /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
1513               {
1514               BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
1515               } */
1516    
1517            frame->coding_type = B_VOP;
1518            BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);
1519    
1520            *pBits = BitstreamPos(bs);
1521    
1522            pEnc->sStat.iTextBits = 0;
1523            pEnc->sStat.iMvSum = 0;
1524            pEnc->sStat.iMvCount = 0;
1525            pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1526    
1527    
1528            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1529                    // reset prediction
1530    
1531                    forward.x = 0;
1532                    forward.y = 0;
1533                    backward.x = 0;
1534                    backward.y = 0;
1535    
1536                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1537                            MACROBLOCK *f_mb =
1538                                    &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1539                            MACROBLOCK *b_mb =
1540                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1541                            MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1542    
1543                            // decoder ignores mb when refence block is INTER(0,0), CBP=0
1544                            if (mb->mode == MODE_NOT_CODED) {
1545                                    mb->mvs[0].x = 0;
1546                                    mb->mvs[0].y = 0;
1547                                    continue;
1548                            }
1549    
1550                            MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1551                                                                             f_ref, &pEnc->f_refh, &pEnc->f_refv,
1552                                                                             &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1553                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1554                                                                             dct_codes);
1555    
1556                            mb->quant = frame->quant;
1557                            mb->cbp =
1558                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1559                                                                      qcoeff);
1560                            //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1561    
1562    
1563                            if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1564                                    && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
1565                                    mb->mode = 5;   // skipped
1566                            }
1567    
1568                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
1569                                    mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1570                                    mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1571                                    forward.x = mb->mvs[0].x;
1572                                    forward.y = mb->mvs[0].y;
1573                            }
1574    
1575                            if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
1576                                    mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1577                                    mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1578                                    backward.x = mb->b_mvs[0].x;
1579                                    backward.y = mb->b_mvs[0].y;
1580                            }
1581    //          printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);
1582    
1583                            start_timer();
1584                            MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1585                                                     &pEnc->sStat);
1586                            stop_coding_timer();
1587                    }
1588            }
1589    
1590            emms();
1591    
1592            // TODO: dynamic fcode/bcode ???
1593    
1594            *pBits = BitstreamPos(bs) - *pBits;
1595    }
1596    #endif

Legend:
Removed from v.85  
changed lines
  Added in v.195

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