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

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

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