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

Diff of /xvidcore/src/encoder.c

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

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

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.54

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