[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

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

Legend:
Removed from v.13  
changed lines
  Added in v.208

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