[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.25, Mon Apr 15 08:03:50 2002 UTC revision 1.76.2.41, Sat Jan 25 22:44:50 2003 UTC
# Line 1  Line 1 
1  // 14.04.2002   added FrameCodeB()  /*****************************************************************************
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    #include "image/font.h"
57    #include "motion/sad.h"
58    #include "motion/motion.h"
59  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
60  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
61  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 20  Line 67 
67  #include "quant/quant_matrix.h"  #include "quant/quant_matrix.h"
68  #include "utils/mem_align.h"  #include "utils/mem_align.h"
69    
70  #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT  /*****************************************************************************
71     * Local macros
72     ****************************************************************************/
73    
74    #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT
75    #define SWAP(_T_,A,B)    { _T_ tmp = A; A = B; B = tmp; }
76    
77  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits);  /*****************************************************************************
78  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header);   * Local function prototypes
79     ****************************************************************************/
80    
81    static int FrameCodeI(Encoder * pEnc,
82                                              Bitstream * bs,
83                                              uint32_t * pBits);
84    
85    static int FrameCodeP(Encoder * pEnc,
86                                              Bitstream * bs,
87                                              uint32_t * pBits,
88                                              bool force_inter,
89                                              bool vol_header);
90    
91    static void FrameCodeB(Encoder * pEnc,
92                                               FRAMEINFO * frame,
93                                               Bitstream * bs,
94                                               uint32_t * pBits);
95    
96    /*****************************************************************************
97     * Local data
98     ****************************************************************************/
99    
100  static int DQtab[4] =  static int DQtab[4] = {
 {  
101          -1, -2, 1, 2          -1, -2, 1, 2
102  };  };
103    
104  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
105          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
106  };  };
107    
108    
109  int encoder_create(XVID_ENC_PARAM * pParam)  /*****************************************************************************
110     * Encoder creation
111     *
112     * This function creates an Encoder instance, it allocates all necessary
113     * image buffers (reference, current and bframes) and initialize the internal
114     * xvid encoder paremeters according to the XVID_ENC_PARAM input parameter.
115     *
116     * The code seems to be very long but is very basic, mainly memory allocation
117     * and cleaning code.
118     *
119     * Returned values :
120     *    - XVID_ERR_OK     - no errors
121     *    - XVID_ERR_MEMORY - the libc could not allocate memory, the function
122     *                        cleans the structure before exiting.
123     *                        pParam->handle is also set to NULL.
124     *
125     ****************************************************************************/
126    
127    int
128    encoder_create(XVID_ENC_PARAM * pParam)
129  {  {
130          Encoder *pEnc;          Encoder *pEnc;
131          uint32_t i;          int i;
132    
133          pParam->handle = NULL;          pParam->handle = NULL;
134    
# Line 51  Line 139 
139          ENC_CHECK(!(pParam->width % 2));          ENC_CHECK(!(pParam->width % 2));
140          ENC_CHECK(!(pParam->height % 2));          ENC_CHECK(!(pParam->height % 2));
141    
142          if (pParam->fincr <= 0 || pParam->fbase <= 0)          /* Fps */
143          {  
144            if (pParam->fincr <= 0 || pParam->fbase <= 0) {
145                  pParam->fincr = 1;                  pParam->fincr = 1;
146                  pParam->fbase = 25;                  pParam->fbase = 25;
147          }          }
148    
149          // simplify the "fincr/fbase" fraction          /*
150          // (neccessary, since windows supplies us with huge numbers)           * Simplify the "fincr/fbase" fraction
151             * (neccessary, since windows supplies us with huge numbers)
152             */
153    
154          i = pParam->fincr;          i = pParam->fincr;
155          while (i > 1)          while (i > 1) {
156          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
157                          pParam->fincr /= i;                          pParam->fincr /= i;
158                          pParam->fbase /= i;                          pParam->fbase /= i;
159                          i = pParam->fincr;                          i = pParam->fincr;
# Line 73  Line 162 
162                  i--;                  i--;
163          }          }
164    
165          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
166                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
167    
168                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
169                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
170          }          }
171    
172            /* Bitrate allocator defaults */
173    
174          if (pParam->rc_bitrate <= 0)          if (pParam->rc_bitrate <= 0)
175                  pParam->rc_bitrate = 900000;                  pParam->rc_bitrate = 900000;
176    
# Line 92  Line 183 
183          if (pParam->rc_buffer <= 0)          if (pParam->rc_buffer <= 0)
184                  pParam->rc_buffer = 100;                  pParam->rc_buffer = 100;
185    
186            /* Max and min quantizers */
187    
188          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))          if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31))
189                  pParam->min_quantizer = 1;                  pParam->min_quantizer = 1;
190    
191          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))          if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31))
192                  pParam->max_quantizer = 31;                  pParam->max_quantizer = 31;
193    
         if (pParam->max_key_interval == 0)              /* 1 keyframe each 10 seconds */  
                 pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;  
   
194          if (pParam->max_quantizer < pParam->min_quantizer)          if (pParam->max_quantizer < pParam->min_quantizer)
195                  pParam->max_quantizer = pParam->min_quantizer;                  pParam->max_quantizer = pParam->min_quantizer;
196    
197          if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL)          /* 1 keyframe each 10 seconds */
198    
199            if (pParam->max_key_interval <= 0)
200                    pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase;
201    
202            pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE);
203            if (pEnc == NULL)
204                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
205    
206            /* Zero the Encoder Structure */
207    
208            memset(pEnc, 0, sizeof(Encoder));
209    
210          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
211    
212          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 118  Line 218 
218          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;          pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE;
219          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;          pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE;
220    
221          pEnc->sStat.fMvPrevSigma = -1;          pEnc->mbParam.fbase = pParam->fbase;
222            pEnc->mbParam.fincr = pParam->fincr;
223    
224          /* Fill rate control parameters */          pEnc->mbParam.m_quant_type = H263_QUANT;
225    
226          pEnc->mbParam.quant = 4;          pEnc->fMvPrevSigma = -1;
227    
228            /* Fill rate control parameters */
229    
230          pEnc->bitrate = pParam->rc_bitrate;          pEnc->bitrate = pParam->rc_bitrate;
231    
232          pEnc->iFrameNum = 0;          pEnc->iFrameNum = -1;
233          pEnc->iMaxKeyInterval = pParam->max_key_interval;          pEnc->mbParam.iMaxKeyInterval = pParam->max_key_interval;
234    
235          /* try to allocate memory */          /* try to allocate frame memory */
236    
237            pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
238            pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
239    
240            if (pEnc->current == NULL || pEnc->reference == NULL)
241                    goto xvid_err_memory1;
242    
243            /* try to allocate mb memory */
244    
245            pEnc->current->mbs =
246                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
247                                            pEnc->mbParam.mb_height, CACHE_LINE);
248            pEnc->reference->mbs =
249                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
250                                            pEnc->mbParam.mb_height, CACHE_LINE);
251    
252            if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
253                    goto xvid_err_memory2;
254    
255            /* try to allocate image memory */
256    
257            if (pParam->global & XVID_GLOBAL_EXTRASTATS)
258                    image_null(&pEnc->sOriginal);
259    
260            image_null(&pEnc->f_refh);
261            image_null(&pEnc->f_refv);
262            image_null(&pEnc->f_refhv);
263    
264            image_null(&pEnc->current->image);
265            image_null(&pEnc->reference->image);
266            image_null(&pEnc->vInterH);
267            image_null(&pEnc->vInterV);
268            image_null(&pEnc->vInterVf);
269            image_null(&pEnc->vInterHV);
270            image_null(&pEnc->vInterHVf);
271    
272            if (pParam->global & XVID_GLOBAL_EXTRASTATS)
273            {       if (image_create
274                            (&pEnc->sOriginal, pEnc->mbParam.edged_width,
275                             pEnc->mbParam.edged_height) < 0)
276                            goto xvid_err_memory3;
277            }
278    
279            if (image_create
280                    (&pEnc->f_refh, pEnc->mbParam.edged_width,
281                     pEnc->mbParam.edged_height) < 0)
282                    goto xvid_err_memory3;
283            if (image_create
284                    (&pEnc->f_refv, pEnc->mbParam.edged_width,
285                     pEnc->mbParam.edged_height) < 0)
286                    goto xvid_err_memory3;
287            if (image_create
288                    (&pEnc->f_refhv, pEnc->mbParam.edged_width,
289                     pEnc->mbParam.edged_height) < 0)
290                    goto xvid_err_memory3;
291    
292            if (image_create
293                    (&pEnc->current->image, pEnc->mbParam.edged_width,
294                     pEnc->mbParam.edged_height) < 0)
295                    goto xvid_err_memory3;
296            if (image_create
297                    (&pEnc->reference->image, pEnc->mbParam.edged_width,
298                     pEnc->mbParam.edged_height) < 0)
299                    goto xvid_err_memory3;
300            if (image_create
301                    (&pEnc->vInterH, pEnc->mbParam.edged_width,
302                     pEnc->mbParam.edged_height) < 0)
303                    goto xvid_err_memory3;
304            if (image_create
305                    (&pEnc->vInterV, pEnc->mbParam.edged_width,
306                     pEnc->mbParam.edged_height) < 0)
307                    goto xvid_err_memory3;
308            if (image_create
309                    (&pEnc->vInterVf, pEnc->mbParam.edged_width,
310                     pEnc->mbParam.edged_height) < 0)
311                    goto xvid_err_memory3;
312            if (image_create
313                    (&pEnc->vInterHV, pEnc->mbParam.edged_width,
314                     pEnc->mbParam.edged_height) < 0)
315                    goto xvid_err_memory3;
316            if (image_create
317                    (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
318                     pEnc->mbParam.edged_height) < 0)
319                    goto xvid_err_memory3;
320    
321    /* Create full bitplane for GMC, this might be wasteful */
322            if (image_create
323                    (&pEnc->vGMC, pEnc->mbParam.edged_width,
324                     pEnc->mbParam.edged_height) < 0)
325                    goto xvid_err_memory3;
326    
327    
328    
329            pEnc->mbParam.global = pParam->global;
330    
331            /* B Frames specific init */
332            pEnc->mbParam.max_bframes = pParam->max_bframes;
333            pEnc->mbParam.bquant_ratio = pParam->bquant_ratio;
334            pEnc->mbParam.bquant_offset = pParam->bquant_offset;
335            pEnc->mbParam.frame_drop_ratio = pParam->frame_drop_ratio;
336            pEnc->bframes = NULL;
337    
338            if (pEnc->mbParam.max_bframes > 0) {
339                    int n;
340    
341                    pEnc->bframes =
342                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
343                                                    CACHE_LINE);
344    
345                    if (pEnc->bframes == NULL)
346                            goto xvid_err_memory3;
347    
348                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
349                            pEnc->bframes[n] = NULL;
350    
351    
352                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
353                            pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
354    
355                            if (pEnc->bframes[n] == NULL)
356                                    goto xvid_err_memory4;
357    
358                            pEnc->bframes[n]->mbs =
359                                    xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
360                                                            pEnc->mbParam.mb_height, CACHE_LINE);
361    
362                            if (pEnc->bframes[n]->mbs == NULL)
363                                    goto xvid_err_memory4;
364    
365                            image_null(&pEnc->bframes[n]->image);
366    
367                            if (image_create
368                                    (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
369                                     pEnc->mbParam.edged_height) < 0)
370                                    goto xvid_err_memory4;
371    
         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 ||  
 #ifdef _DEBUG  
                 image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 ||  
 #endif  
                 (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);  
 #ifdef _DEBUG  
                 image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
 #endif  
                 if (pEnc)  
                 {  
                         xvid_free(pEnc);  
372                  }                  }
                 return XVID_ERR_MEMORY;  
373          }          }
374    
375          // init macroblock array          pEnc->bframenum_head = 0;
376          for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++)          pEnc->bframenum_tail = 0;
377          {          pEnc->flush_bframes = 0;
378                  pEnc->pMBs[i].dquant = NO_CHANGE;          pEnc->bframenum_dx50bvop = -1;
379    
380            pEnc->queue = NULL;
381    
382    
383            if (pEnc->mbParam.max_bframes > 0) {
384                    int n;
385    
386                    pEnc->queue =
387                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
388                                                    CACHE_LINE);
389    
390                    if (pEnc->queue == NULL)
391                            goto xvid_err_memory4;
392    
393                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
394                            image_null(&pEnc->queue[n]);
395    
396                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
397                            if (image_create
398                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
399                                     pEnc->mbParam.edged_height) < 0)
400                                    goto xvid_err_memory5;
401    
402          }          }
403            }
404    
405            pEnc->queue_head = 0;
406            pEnc->queue_tail = 0;
407            pEnc->queue_size = 0;
408    
409            pEnc->mbParam.m_stamp = 0;
410    
411            pEnc->m_framenum = 0;
412            pEnc->current->stamp = 0;
413            pEnc->reference->stamp = 0;
414    
415          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
416    
417          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
418          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
419                  RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor,                                                  pParam->rc_reaction_delay_factor,
420                          pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
421                                                    pParam->fbase * 1000 / pParam->fincr,
422                          pParam->max_quantizer, pParam->min_quantizer);                          pParam->max_quantizer, pParam->min_quantizer);
423          }          }
424    
425          init_timer();          init_timer();
426    
427          return XVID_ERR_OK;          return XVID_ERR_OK;
428    
429            /*
430             * We handle all XVID_ERR_MEMORY here, this makes the code lighter
431             */
432    
433      xvid_err_memory5:
434    
435    
436            if (pEnc->mbParam.max_bframes > 0) {
437    
438                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
439                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
440                                                      pEnc->mbParam.edged_height);
441                    }
442                    xvid_free(pEnc->queue);
443  }  }
444    
445      xvid_err_memory4:
446    
447            if (pEnc->mbParam.max_bframes > 0) {
448    
449                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
450    
451                            if (pEnc->bframes[i] == NULL)
452                                    continue;
453    
454                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
455                                                      pEnc->mbParam.edged_height);
456    
457                            xvid_free(pEnc->bframes[i]->mbs);
458    
459                            xvid_free(pEnc->bframes[i]);
460    
461                    }
462    
463                    xvid_free(pEnc->bframes);
464            }
465    
466      xvid_err_memory3:
467    
468            if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
469            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
470                                              pEnc->mbParam.edged_height);
471            }
472    
473            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
474                                      pEnc->mbParam.edged_height);
475            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
476                                      pEnc->mbParam.edged_height);
477            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
478                                      pEnc->mbParam.edged_height);
479    
480            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
481                                      pEnc->mbParam.edged_height);
482            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
483                                      pEnc->mbParam.edged_height);
484            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
485                                      pEnc->mbParam.edged_height);
486            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
487                                      pEnc->mbParam.edged_height);
488            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
489                                      pEnc->mbParam.edged_height);
490            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
491                                      pEnc->mbParam.edged_height);
492            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
493                                      pEnc->mbParam.edged_height);
494    
495    /* destroy GMC image */
496            image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width,
497                                      pEnc->mbParam.edged_height);
498    
499    
500  int encoder_destroy(Encoder * pEnc)    xvid_err_memory2:
501            xvid_free(pEnc->current->mbs);
502            xvid_free(pEnc->reference->mbs);
503    
504      xvid_err_memory1:
505            xvid_free(pEnc->current);
506            xvid_free(pEnc->reference);
507            xvid_free(pEnc);
508    
509            pParam->handle = NULL;
510    
511            return XVID_ERR_MEMORY;
512    }
513    
514    /*****************************************************************************
515     * Encoder destruction
516     *
517     * This function destroy the entire encoder structure created by a previous
518     * successful encoder_create call.
519     *
520     * Returned values (for now only one returned value) :
521     *    - XVID_ERR_OK     - no errors
522     *
523     ****************************************************************************/
524    
525    int
526    encoder_destroy(Encoder * pEnc)
527  {  {
528            int i;
529    
530          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
         ENC_CHECK(pEnc->sCurrent.y);  
         ENC_CHECK(pEnc->sReference.y);  
531    
532          xvid_free(pEnc->pMBs);          /* B Frames specific */
533          image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          if (pEnc->mbParam.max_bframes > 0) {
534          image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
535          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
536          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);  
537          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                          image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
538          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                                            pEnc->mbParam.edged_height);
539          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);                  }
540  #ifdef _DEBUG                  xvid_free(pEnc->queue);
541                  image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height);          }
542  #endif  
543    
544            if (pEnc->mbParam.max_bframes > 0) {
545    
546                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
547    
548                            if (pEnc->bframes[i] == NULL)
549                                    continue;
550    
551                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
552                                              pEnc->mbParam.edged_height);
553    
554                            xvid_free(pEnc->bframes[i]->mbs);
555    
556                            xvid_free(pEnc->bframes[i]);
557                    }
558    
559                    xvid_free(pEnc->bframes);
560    
561            }
562    
563            /* All images, reference, current etc ... */
564    
565            image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
566                                      pEnc->mbParam.edged_height);
567            image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
568                                      pEnc->mbParam.edged_height);
569            image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
570                                      pEnc->mbParam.edged_height);
571            image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
572                                      pEnc->mbParam.edged_height);
573            image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
574                                      pEnc->mbParam.edged_height);
575            image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
576                                      pEnc->mbParam.edged_height);
577            image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
578                                      pEnc->mbParam.edged_height);
579    
580            image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
581                                      pEnc->mbParam.edged_height);
582            image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
583                                      pEnc->mbParam.edged_height);
584            image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
585                                      pEnc->mbParam.edged_height);
586    
587            if (pEnc->mbParam.global & XVID_GLOBAL_EXTRASTATS)
588            {       image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
589                                              pEnc->mbParam.edged_height);
590            }
591    
592            /* Encoder structure */
593    
594            xvid_free(pEnc->current->mbs);
595            xvid_free(pEnc->current);
596    
597            xvid_free(pEnc->reference->mbs);
598            xvid_free(pEnc->reference);
599    
600          xvid_free(pEnc);          xvid_free(pEnc);
601    
602          return XVID_ERR_OK;          return XVID_ERR_OK;
603  }  }
604    
605  int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult)  
606    static __inline void inc_frame_num(Encoder * pEnc)
607    {
608            pEnc->current->stamp = pEnc->mbParam.m_stamp;   // first frame is zero
609            pEnc->mbParam.m_stamp += pEnc->mbParam.fincr;
610    }
611    
612    
613    static __inline void
614    queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
615    {
616            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
617            {
618                    DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL");
619                    return;
620            }
621    
622            DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
623                                    pEnc->bframenum_head, pEnc->bframenum_tail,
624                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
625    
626    
627            start_timer();
628            if (image_input
629                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
630                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
631                    return;
632            stop_conv_timer();
633    
634            pEnc->queue_size++;
635            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
636    }
637    
638    static __inline void
639    set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base)
640    {
641    
642                    pCur->ticks = (int32_t)pCur->stamp % time_base;
643                    pCur->seconds =  ((int32_t)pCur->stamp / time_base)     - ((int32_t)pRef->stamp / time_base) ;
644    
645                    //HEAVY DEBUG OUTPUT    remove when timecodes prove to be stable
646    
647    /*              fprintf(stderr,"WriteVop:   %d - %d \n",
648                            ((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base));
649                    fprintf(stderr,"set_timecodes: VOP %1d   stamp=%lld ref_stamp=%lld  base=%d\n",
650                            pCur->coding_type, pCur->stamp, pRef->stamp, time_base);
651                    fprintf(stderr,"set_timecodes: VOP %1d   seconds=%d   ticks=%d   (ref-sec=%d  ref-tick=%d)\n",
652                            pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks);
653    
654    */
655    }
656    
657    
658    
659    /* convert pFrame->intra to coding_type */
660    static int intra2coding_type(int intra)
661    {
662            if (intra < 0)  return -1;
663            if (intra == 1) return I_VOP;
664            if (intra == 2) return B_VOP;
665    
666            return P_VOP;
667    }
668    
669    
670    
671    /*****************************************************************************
672     * IPB frame encoder entry point
673     *
674     * Returned values :
675     *    - XVID_ERR_OK     - no errors
676     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
677     *                        format
678     ****************************************************************************/
679    
680    int
681    encoder_encode_bframes(Encoder * pEnc,
682                               XVID_ENC_FRAME * pFrame,
683                               XVID_ENC_STATS * pResult)
684    {
685            uint16_t x, y;
686            Bitstream bs;
687            uint32_t bits;
688            int mode;
689    
690            int input_valid = 1;
691            int bframes_count = 0;
692    
693            float psnr;
694            char temp[128];
695    
696            ENC_CHECK(pEnc);
697            ENC_CHECK(pFrame);
698            ENC_CHECK(pFrame->image);
699    
700            start_global_timer();
701    
702            BitstreamInit(&bs, pFrame->bitstream, 0);
703    
704    ipvop_loop:
705    
706            /*
707             * bframe "flush" code
708             */
709    
710            if ((pFrame->image == NULL || pEnc->flush_bframes)
711                    && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
712    
713                    if (pEnc->flush_bframes == 0) {
714                            /*
715                             * we have reached the end of stream without getting
716                             * a future reference frame... so encode last final
717                             * frame as a pframe
718                             */
719    
720                            DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
721                                    pEnc->bframenum_head, pEnc->bframenum_tail,
722                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
723    
724                            pEnc->bframenum_tail--;
725                            SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
726    
727                            SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
728    
729                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
730                            bframes_count = 0;
731    
732                            BitstreamPadAlways(&bs);
733                            pFrame->length = BitstreamLength(&bs);
734                            pFrame->intra = 0;
735    
736    
737                            emms();
738    
739                            if (pResult) {
740                                    pResult->quant = pEnc->current->quant;
741                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
742                                    pResult->kblks = pEnc->current->sStat.kblks;
743                                    pResult->mblks = pEnc->current->sStat.mblks;
744                                    pResult->ublks = pEnc->current->sStat.ublks;
745                            }
746    
747                            return XVID_ERR_OK;
748                    }
749    
750    
751                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
752                                    pEnc->bframenum_head, pEnc->bframenum_tail,
753                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
754    
755                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
756                    pEnc->bframenum_head++;
757    
758                    BitstreamPadAlways(&bs);
759                    pFrame->length = BitstreamLength(&bs);
760                    pFrame->intra = 2;
761    
762                    if (pResult) {
763                            pResult->quant = pEnc->current->quant;
764                            pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
765                            pResult->kblks = pEnc->current->sStat.kblks;
766                            pResult->mblks = pEnc->current->sStat.mblks;
767                            pResult->ublks = pEnc->current->sStat.ublks;
768                    }
769    
770                    if (input_valid)
771                            queue_image(pEnc, pFrame);
772    
773                    emms();
774    
775                    return XVID_ERR_OK;
776            }
777    
778            if (pEnc->bframenum_head > 0) {
779                    pEnc->bframenum_head = pEnc->bframenum_tail = 0;
780    
781                    /* write an empty marker to the bitstream.
782    
783                       for divx5 decoder compatibility, this marker must consist
784                       of a not-coded p-vop, with a time_base of zero, and time_increment
785                       indentical to the future-referece frame.
786                    */
787    
788                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED)) {
789                            int tmp;
790    
791                            DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
792                                    pEnc->bframenum_head, pEnc->bframenum_tail,
793                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
794    
795    
796                            tmp = pEnc->current->seconds;
797                            pEnc->current->seconds = 0; /* force time_base = 0 */
798    
799                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
800                            pEnc->current->seconds = tmp;
801    
802                            BitstreamPadAlways(&bs);
803                            pFrame->length = BitstreamLength(&bs);
804                            pFrame->intra = 4;
805    
806                            if (pResult) {
807                                    pResult->quant = pEnc->current->quant;
808                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
809                                    pResult->kblks = pEnc->current->sStat.kblks;
810                                    pResult->mblks = pEnc->current->sStat.mblks;
811                                    pResult->ublks = pEnc->current->sStat.ublks;
812                            }
813    
814                            if (input_valid)
815                                    queue_image(pEnc, pFrame);
816    
817                            emms();
818    
819                            return XVID_ERR_OK;
820                    }
821            }
822    
823    
824    bvop_loop:
825    
826            if (pEnc->bframenum_dx50bvop != -1)
827            {
828    
829                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
830                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
831    
832                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
833                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP");
834                    }
835    
836                    if (input_valid)
837                    {
838                            queue_image(pEnc, pFrame);
839                            input_valid = 0;
840                    }
841    
842            } else if (input_valid) {
843    
844                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
845    
846                    start_timer();
847                    if (image_input
848                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
849                            pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING))
850                    {
851                            emms();
852                            return XVID_ERR_FORMAT;
853                    }
854                    stop_conv_timer();
855    
856                    // queue input frame, and dequue next image
857                    if (pEnc->queue_size > 0)
858                    {
859                            image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
860                            if (pEnc->queue_head != pEnc->queue_tail)
861                            {
862                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
863                            }
864                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
865                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
866                    }
867    
868            } else if (pEnc->queue_size > 0) {
869    
870                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
871    
872                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
873                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
874                    pEnc->queue_size--;
875    
876            } else {
877    
878                    /* if nothing was encoded, write an 'ignore this frame' flag
879                       to the bitstream */
880    
881                    if (BitstreamPos(&bs) == 0) {
882    
883                            DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
884                                    pEnc->bframenum_head, pEnc->bframenum_tail,
885                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
886    
887                    //      BitstreamPutBits(&bs, 0x7f, 8);
888                            pFrame->intra = 5;
889    
890                            if (pResult) {
891                                    /*
892                                     * We must decide what to put there because i know some apps
893                                     * are storing statistics about quantizers and just do
894                                     * stats[quant]++ or stats[quant-1]++
895                                     * transcode is one of these app with its 2pass module
896                                     */
897    
898                                    /*
899                                     * For now i prefer 31 than 0 that could lead to a segfault
900                                     * in transcode
901                                     */
902                                    pResult->quant = 31;
903    
904                                    pResult->hlength = 0;
905                                    pResult->kblks = 0;
906                                    pResult->mblks = 0;
907                                    pResult->ublks = 0;
908                            }
909    
910                    } else {
911    
912                            if (pResult) {
913                                    pResult->quant = pEnc->current->quant;
914                                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
915                                    pResult->kblks = pEnc->current->sStat.kblks;
916                                    pResult->mblks = pEnc->current->sStat.mblks;
917                                    pResult->ublks = pEnc->current->sStat.ublks;
918                            }
919    
920                    }
921    
922                    pFrame->length = BitstreamLength(&bs);
923    
924                    emms();
925    
926                    return XVID_ERR_OK;
927            }
928    
929            pEnc->flush_bframes = 0;
930    
931            emms();
932    
933            // only inc frame num, adapt quant, etc. if we havent seen it before
934            if (pEnc->bframenum_dx50bvop < 0 )
935            {
936                    mode = intra2coding_type(pFrame->intra);
937                    if (pFrame->quant == 0)
938                            pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
939                    else
940                            pEnc->current->quant = pFrame->quant;
941    
942    /*              if (pEnc->current->quant < 1)
943                            pEnc->current->quant = 1;
944    
945                    if (pEnc->current->quant > 31)
946                            pEnc->current->quant = 31;
947    */
948                    pEnc->current->global_flags = pFrame->general;
949                    pEnc->current->motion_flags = pFrame->motion;
950    
951                    /* ToDo : dynamic fcode (in both directions) */
952                    pEnc->current->fcode = pEnc->mbParam.m_fcode;
953                    pEnc->current->bcode = pEnc->mbParam.m_fcode;
954    
955                    inc_frame_num(pEnc);
956    
957                    if (pFrame->general & XVID_EXTRASTATS)
958                    {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
959                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
960                    }
961    
962                    emms();
963    
964                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
965                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
966                                    "%i  if:%i  st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp);
967                    }
968    
969            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970             * Luminance masking
971             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
972    
973                    if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
974                            int *temp_dquants =
975                                    (int *) xvid_malloc(pEnc->mbParam.mb_width *
976                                                                    pEnc->mbParam.mb_height * sizeof(int),
977                                                                    CACHE_LINE);
978    
979                            pEnc->current->quant =
980                                    adaptive_quantization(pEnc->current->image.y,
981                                                                      pEnc->mbParam.edged_width, temp_dquants,
982                                                                      pEnc->current->quant, pEnc->current->quant,
983                                                                      2 * pEnc->current->quant,
984                                                                      pEnc->mbParam.mb_width,
985                                                                      pEnc->mbParam.mb_height);
986    
987                            for (y = 0; y < pEnc->mbParam.mb_height; y++) {
988    
989    #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
990    
991                                    for (x = 0; x < pEnc->mbParam.mb_width; x++) {
992                                            MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
993    
994                                            pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
995                                    }
996    
997    #undef OFFSET
998                            }
999    
1000                            xvid_free(temp_dquants);
1001                    }
1002    
1003            }
1004    
1005            /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006             * ivop/pvop/bvop selection
1007             * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
1008            pEnc->iFrameNum++;
1009    
1010            if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 ||
1011                    (mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 &&
1012                            pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))
1013            {
1014                    mode = I_VOP;
1015            }else{
1016                    mode = MEanalysis(&pEnc->reference->image, pEnc->current,
1017                                            &pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval,
1018                                            (mode < 0) ? pEnc->iFrameNum : 0,
1019                                            bframes_count++);
1020            }
1021    
1022            if (mode == I_VOP) {
1023                    /*
1024                     * This will be coded as an Intra Frame
1025                     */
1026                    if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1027                            pEnc->mbParam.m_quarterpel = 1;
1028                    else
1029                            pEnc->mbParam.m_quarterpel = 0;
1030    
1031                    if (pEnc->current->global_flags & XVID_MPEGQUANT) pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1032    
1033                    if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1034                            if (pFrame->quant_intra_matrix != NULL)
1035                                    set_intra_matrix(pFrame->quant_intra_matrix);
1036                            if (pFrame->quant_inter_matrix != NULL)
1037                                    set_inter_matrix(pFrame->quant_inter_matrix);
1038                    }
1039    
1040    
1041                    DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1042                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1043                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1044    
1045                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1046                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
1047                    }
1048    
1049                    // when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe
1050    
1051                    if ((pEnc->mbParam.global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) {
1052    
1053                            pEnc->bframenum_tail--;
1054                            pEnc->bframenum_dx50bvop = pEnc->bframenum_tail;
1055    
1056                            SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]);
1057                            if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1058                                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP");
1059                            }
1060                            FrameCodeP(pEnc, &bs, &bits, 1, 0);
1061                            bframes_count = 0;
1062                            pFrame->intra = 0;
1063    
1064                    } else {
1065    
1066                            FrameCodeI(pEnc, &bs, &bits);
1067                            bframes_count = 0;
1068                            pFrame->intra = 1;
1069    
1070                            pEnc->bframenum_dx50bvop = -1;
1071                    }
1072    
1073                    pEnc->flush_bframes = 1;
1074    
1075                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) {
1076                            BitstreamPadAlways(&bs);
1077                            input_valid = 0;
1078                            goto ipvop_loop;
1079                    }
1080    
1081                    /*
1082                     * NB : sequences like "IIBB" decode fine with msfdam but,
1083                     *      go screwy with divx 5.00
1084                     */
1085            } else if (mode == P_VOP || mode == S_VOP || pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
1086                    /*
1087                     * This will be coded as a Predicted Frame
1088                     */
1089    
1090                    DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
1091                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1092                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
1093    
1094                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1095                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
1096                    }
1097    
1098                    FrameCodeP(pEnc, &bs, &bits, 1, 0);
1099                    bframes_count = 0;
1100                    pFrame->intra = 0;
1101                    pEnc->flush_bframes = 1;
1102    
1103                    if ((pEnc->mbParam.global & XVID_GLOBAL_PACKED) && (pEnc->bframenum_tail > 0)) {
1104                            BitstreamPadAlways(&bs);
1105                            input_valid = 0;
1106                            goto ipvop_loop;
1107                    }
1108    
1109            } else {        /* mode == B_VOP */
1110                    /*
1111                     * This will be coded as a Bidirectional Frame
1112                     */
1113    
1114                    if ((pEnc->mbParam.global & XVID_GLOBAL_DEBUG)) {
1115                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
1116                    }
1117    
1118                    if (pFrame->bquant < 1) {
1119                            pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) *
1120                                    pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100;
1121    
1122                    } else {
1123                            pEnc->current->quant = pFrame->bquant;
1124                    }
1125    
1126                    if (pEnc->current->quant < 1)
1127                            pEnc->current->quant = 1;
1128                    else if (pEnc->current->quant > 31)
1129                pEnc->current->quant = 31;
1130    
1131                    DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i  quant=%i\n",
1132                                    pEnc->bframenum_head, pEnc->bframenum_tail,
1133                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant);
1134    
1135                    /* store frame into bframe buffer & swap ref back to current */
1136                    SWAP(FRAMEINFO *, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
1137                    SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1138    
1139                    pEnc->bframenum_tail++;
1140    
1141    // bframe report by koepi
1142                    pFrame->intra = 2;
1143                    pFrame->length = 0;
1144    
1145                    input_valid = 0;
1146                    goto bvop_loop;
1147            }
1148    
1149            BitstreamPadAlways(&bs);
1150            pFrame->length = BitstreamLength(&bs);
1151    
1152            if (pResult) {
1153                    pResult->quant = pEnc->current->quant;
1154                    pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1155                    pResult->kblks = pEnc->current->sStat.kblks;
1156                    pResult->mblks = pEnc->current->sStat.mblks;
1157                    pResult->ublks = pEnc->current->sStat.ublks;
1158    
1159                    if (pFrame->general & XVID_EXTRASTATS)
1160                    {       pResult->sse_y =
1161                                    plane_sse( pEnc->sOriginal.y, pEnc->current->image.y,
1162                                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1163                                                       pEnc->mbParam.height);
1164    
1165                            pResult->sse_u =
1166                                    plane_sse( pEnc->sOriginal.u, pEnc->current->image.u,
1167                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1168                                                       pEnc->mbParam.height/2);
1169    
1170                            pResult->sse_v =
1171                                    plane_sse( pEnc->sOriginal.v, pEnc->current->image.v,
1172                                                       pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2,
1173                                                       pEnc->mbParam.height/2);
1174                    }
1175            }
1176    
1177            emms();
1178    
1179            if (pFrame->quant == 0) {
1180                    RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1181                                                      pFrame->length, pFrame->intra);
1182            }
1183    
1184            stop_global_timer();
1185            write_timer();
1186    
1187            emms();
1188            return XVID_ERR_OK;
1189    }
1190    
1191    
1192    
1193    /*****************************************************************************
1194     * "original" IP frame encoder entry point
1195     *
1196     * Returned values :
1197     *    - XVID_ERR_OK     - no errors
1198     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1199     *                        format
1200     ****************************************************************************/
1201    
1202    int
1203    encoder_encode(Encoder * pEnc,
1204                               XVID_ENC_FRAME * pFrame,
1205                               XVID_ENC_STATS * pResult)
1206  {  {
1207          uint16_t x, y;          uint16_t x, y;
1208          Bitstream bs;          Bitstream bs;
1209          uint32_t bits;          uint32_t bits;
1210          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1211  #ifdef _DEBUG  
1212          float psnr;          float psnr;
1213          uint8_t temp[100];          uint8_t temp[128];
 #endif  
1214    
1215          start_global_timer();          start_global_timer();
1216    
# Line 231  Line 1219 
1219          ENC_CHECK(pFrame->bitstream);          ENC_CHECK(pFrame->bitstream);
1220          ENC_CHECK(pFrame->image);          ENC_CHECK(pFrame->image);
1221    
1222          pEnc->mbParam.global_flags = pFrame->general;          SWAP(FRAMEINFO *, pEnc->current, pEnc->reference);
1223          pEnc->mbParam.motion_flags = pFrame->motion;  
1224            pEnc->current->global_flags = pFrame->general;
1225            pEnc->current->motion_flags = pFrame->motion;
1226          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1227    
1228          start_timer();          inc_frame_num(pEnc);
1229          if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width,  
1230                          pFrame->image, pFrame->colorspace))          /* disable alternate scan flag if interlacing is not enabled */
1231            if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) &&
1232                    !(pEnc->current->global_flags & XVID_INTERLACING))
1233          {          {
1234                    pEnc->current->global_flags -= XVID_ALTERNATESCAN;
1235            }
1236    
1237            start_timer();
1238            if (image_input
1239                    (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1240                     pEnc->mbParam.edged_width, pFrame->image, pFrame->stride, pFrame->colorspace, pFrame->general & XVID_INTERLACING) < 0)
1241                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
         }  
1242          stop_conv_timer();          stop_conv_timer();
1243    
1244          EMMS();          if (pFrame->general & XVID_EXTRASTATS)
1245            {       image_copy(&pEnc->sOriginal, &pEnc->current->image,
1246                                       pEnc->mbParam.edged_width, pEnc->mbParam.height);
1247            }
1248    
1249  #ifdef _DEBUG          emms();
         image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height);  
 #endif  
1250    
1251          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1252    
1253          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1254          {                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
1255                  pEnc->mbParam.quant = RateControlGetQ(0);          } else {
1256          }                  pEnc->current->quant = pFrame->quant;
         else  
         {  
                 pEnc->mbParam.quant = pFrame->quant;  
1257          }          }
1258    
1259          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0)          if ((pEnc->current->global_flags & XVID_QUARTERPEL))
1260          {                  pEnc->mbParam.m_quarterpel = 1;
1261                  int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE);          else
1262                    pEnc->mbParam.m_quarterpel = 0;
1263    
1264                  pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y,          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1265                                                              pEnc->mbParam.width,                  int *temp_dquants =
1266                                                              temp_dquants,                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1267                                                              pEnc->mbParam.quant,                                                                  pEnc->mbParam.mb_height * sizeof(int),
1268                                                              pEnc->mbParam.quant,                                                                  CACHE_LINE);
1269                                                              2*pEnc->mbParam.quant,  
1270                    pEnc->current->quant =
1271                            adaptive_quantization(pEnc->current->image.y,
1272                                                                      pEnc->mbParam.edged_width, temp_dquants,
1273                                                                      pEnc->current->quant, pEnc->current->quant,
1274                                                                      2 * pEnc->current->quant,
1275                                                              pEnc->mbParam.mb_width,                                                              pEnc->mbParam.mb_width,
1276                                                              pEnc->mbParam.mb_height);                                                              pEnc->mbParam.mb_height);
1277    
1278                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1279                          for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1280                          {  #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1281                                  MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];  
1282                                  pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)];                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1283    
1284    
1285                                    MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1286    
1287                                    pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
1288                            }
1289    
1290    #undef OFFSET
1291                          }                          }
1292    
1293                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1294          }          }
1295    
1296          if(pEnc->mbParam.global_flags & XVID_H263QUANT) {          if (pEnc->current->global_flags & XVID_H263QUANT) {
1297                  if(pEnc->mbParam.quant_type != H263_QUANT)                  if (pEnc->mbParam.m_quant_type != H263_QUANT)
1298                          write_vol_header = 1;                          write_vol_header = 1;
1299                  pEnc->mbParam.quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1300          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
1301          else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) {                  int matrix1_changed, matrix2_changed;
                 int ret1, ret2;  
1302    
1303                  ret1 = ret2 = 0;                  matrix1_changed = matrix2_changed = 0;
1304    
1305                  if(pEnc->mbParam.quant_type != MPEG4_QUANT)                  if (pEnc->mbParam.m_quant_type != MPEG4_QUANT)
1306                          write_vol_header = 1;                          write_vol_header = 1;
1307    
1308                  pEnc->mbParam.quant_type = MPEG4_QUANT;                  pEnc->mbParam.m_quant_type = MPEG4_QUANT;
1309    
1310                  if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1311                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1312                                  ret1 = set_intra_matrix(pFrame->quant_intra_matrix);                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
1313                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1314                                  ret2 = set_inter_matrix(pFrame->quant_inter_matrix);                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1315                  }                  } else {
1316                  else {                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1317                          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());  
1318                  }                  }
1319                  if(write_vol_header == 0)                  if(write_vol_header == 0)
1320                          write_vol_header = ret1 | ret2;                          write_vol_header = matrix1_changed | matrix2_changed;
1321          }          }
1322    
1323          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1324          {                  if ((pEnc->iFrameNum == 0)
1325                  if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0)                          || ((pEnc->mbParam.iMaxKeyInterval > 0)
1326                                                 && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                                  && (pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval))) {
   
1327                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1328                  else                  } else {
1329                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1330          }          }
1331          else          } else {
1332          {                  if (pFrame->intra == 1) {
                 if (pFrame->intra == 1)  
1333                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1334                  else                  } else {
1335                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
1336          }          }
1337    
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPutBits(&bs, 0xFFFF, 16);  
         BitstreamPad(&bs);  
         pFrame->length = BitstreamLength(&bs);  
   
         if (pResult)  
         {  
                 pResult->quant = pEnc->mbParam.quant;  
                 pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);  
                 pResult->kblks = pEnc->sStat.kblks;  
                 pResult->mblks = pEnc->sStat.mblks;  
                 pResult->ublks = pEnc->sStat.ublks;  
1338          }          }
1339    
1340          EMMS();  //      BitstreamPutBits(&bs, 0xFFFF, 16);
1341    //      BitstreamPutBits(&bs, 0xFFFF, 16);
1342            BitstreamPadAlways(&bs);
1343            pFrame->length = BitstreamLength(&bs);
1344    
1345          if (pFrame->quant == 0)          if (pResult) {
1346          {                  pResult->quant = pEnc->current->quant;
1347                  RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra);                  pResult->hlength = pFrame->length - (pEnc->current->sStat.iTextBits / 8);
1348                    pResult->kblks = pEnc->current->sStat.kblks;
1349                    pResult->mblks = pEnc->current->sStat.mblks;
1350                    pResult->ublks = pEnc->current->sStat.ublks;
1351          }          }
1352    
1353  #ifdef _DEBUG          emms();
         psnr = image_psnr(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width,  
                                 pEnc->mbParam.width, pEnc->mbParam.height);  
1354    
1355          sprintf(temp, "PSNR: %f\n", psnr);          if (pFrame->quant == 0) {
1356          DEBUG(temp);                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1357  #endif                                                    pFrame->length, pFrame->intra);
1358            }
1359            if (pFrame->general & XVID_EXTRASTATS)
1360            {
1361                    psnr =
1362                            image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1363                                               pEnc->mbParam.edged_width, pEnc->mbParam.width,
1364                                               pEnc->mbParam.height);
1365    
1366                    snprintf(temp, 127, "PSNR: %f\n", psnr);
1367            }
1368    
1369          pEnc->iFrameNum++;          pEnc->iFrameNum++;
         image_swap(&pEnc->sCurrent, &pEnc->sReference);  
1370    
1371          stop_global_timer();          stop_global_timer();
1372          write_timer();          write_timer();
# Line 367  Line 1375 
1375  }  }
1376    
1377    
1378  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1379    CodeIntraMB(Encoder * pEnc,
1380                            MACROBLOCK * pMB)
1381    {
1382    
1383          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1384    
1385          if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {          /* zero mv statistics */
1386                  if(pMB->dquant != NO_CHANGE)          pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0;
1387                  {          pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0;
1388            pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1389            pMB->sad16 = 0;
1390    
1391            if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1392                    if (pMB->dquant != NO_CHANGE) {
1393                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1394                          pEnc->mbParam.quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1395    
1396                          if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                          if (pEnc->current->quant > 31)
1397                          if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                  pEnc->current->quant = 31;
1398                            if (pEnc->current->quant < 1)
1399                                    pEnc->current->quant = 1;
1400                  }                  }
1401          }          }
1402    
1403          pMB->quant = pEnc->mbParam.quant;          pMB->quant = pEnc->current->quant;
1404  }  }
1405    
1406    
1407  #define FCODEBITS       3  #define FCODEBITS       3
1408  #define MODEBITS        5  #define MODEBITS        5
1409    
1410  void HintedMESet(Encoder * pEnc, int * intra)  void
1411    HintedMESet(Encoder * pEnc,
1412                            int *intra)
1413  {  {
1414          HINTINFO * hint;          HINTINFO * hint;
1415          Bitstream bs;          Bitstream bs;
# Line 398  Line 1418 
1418    
1419          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1420    
1421          if (hint->rawhints)          if (hint->rawhints) {
         {  
1422                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1423          }          } else {
         else  
         {  
1424                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1425                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1426          }          }
1427    
1428          if (*intra)          if (*intra) {
         {  
1429                  return;                  return;
1430          }          }
1431    
1432          pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1433                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1434                                                                                                                                     FCODEBITS);
1435    
1436          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
1437          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1438    
1439          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1440          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1441                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1442                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1443                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1444                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1445                          VECTOR pred[4];                          VECTOR pred;
1446                          VECTOR tmp;                          VECTOR tmp;
                         int dummy[4];  
1447                          int vec;                          int vec;
1448    
1449                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1450                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1451                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                                                                                                                                    MODEBITS);
1452                          {  
1453                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1454                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1455    
1456                            if (pMB->mode == MODE_INTER) {
1457                                    tmp.x =
1458                                            (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1459                                                                                                                                                      length);
1460                                    tmp.y =
1461                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1462                                                                                                                                                      length);
1463                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1464                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1465    
1466                                  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);
1467    
1468                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1469                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1470                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1471                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x;
1472                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y;
                                 }  
1473                          }                          }
1474                          else if (pMB->mode == MODE_INTER4V)                          } else if (pMB->mode == MODE_INTER4V) {
1475                          {                                  for (vec = 0; vec < 4; ++vec) {
1476                                  for (vec=0 ; vec<4 ; ++vec)                                          tmp.x =
1477                                  {                                                  (hint->rawhints) ? bhint->mvs[vec].
1478                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                                  x : BitstreamGetBits(&bs, length);
1479                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                          tmp.y =
1480                                                    (hint->rawhints) ? bhint->mvs[vec].
1481                                                    y : BitstreamGetBits(&bs, length);
1482                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1483                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1484    
1485                                          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);
1486    
1487                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1488                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1489                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x;
1490                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y;
1491                                  }                                  }
1492                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / intra_q / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1493                                  {                                  {
1494                                    for (vec = 0; vec < 4; ++vec) {
1495                                            pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1496                                    }
1497                            }
1498    
1499                            if (pMB->mode == MODE_INTER4V &&
1500                                    (pEnc->current->global_flags & XVID_LUMIMASKING)
1501                                    && pMB->dquant != NO_CHANGE) {
1502                                    pMB->mode = MODE_INTRA;
1503    
1504                                    for (vec = 0; vec < 4; ++vec) {
1505                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1506                                  }                                  }
1507                          }                          }
# Line 477  Line 1510 
1510  }  }
1511    
1512    
1513  void HintedMEGet(Encoder * pEnc, int intra)  void
1514    HintedMEGet(Encoder * pEnc,
1515                            int intra)
1516  {  {
1517          HINTINFO * hint;          HINTINFO * hint;
1518          Bitstream bs;          Bitstream bs;
# Line 486  Line 1521 
1521    
1522          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1523    
1524          if (hint->rawhints)          if (hint->rawhints) {
         {  
1525                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1526          }          } else {
         else  
         {  
1527                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1528                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1529          }          }
1530    
1531          if (intra)          if (intra) {
1532          {                  if (!hint->rawhints) {
1533                  if (!hint->rawhints)                          BitstreamPadAlways(&bs);
                 {  
                         BitstreamPad(&bs);  
1534                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1535                  }                  }
1536                  return;                  return;
1537          }          }
1538    
1539          length  = pEnc->mbParam.fixed_code + 5;          length = pEnc->current->fcode + 5;
1540          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1541    
1542          if (hint->rawhints)          if (hint->rawhints) {
1543          {                  hint->mvhint.fcode = pEnc->current->fcode;
1544                  hint->mvhint.fcode = pEnc->mbParam.fixed_code;          } else {
1545          }                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
         else  
         {  
                 BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS);  
1546          }          }
1547    
1548          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1549          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1550                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1551                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1552                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1553                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1554                          VECTOR tmp;                          VECTOR tmp;
1555    
1556                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1557                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1558                          }                          } else {
                         else  
                         {  
1559                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1560                          }                          }
1561    
1562                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1563                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1564                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1565                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1566                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1567    
1568                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1569                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1570                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1571                                  }                                  } else {
                                 else  
                                 {  
1572                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1573                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1574                                  }                                  }
1575                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1576                                  int vec;                                  int vec;
1577    
1578                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1579                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1580                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1581                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1582                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1583    
1584                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1585                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1586                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1587                                          }                                          } else {
                                         else  
                                         {  
1588                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1589                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1590                                          }                                          }
# Line 579  Line 1593 
1593                  }                  }
1594          }          }
1595    
1596          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1597                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1598                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1599          }          }
1600  }  }
1601    
1602    
1603  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1604    FrameCodeI(Encoder * pEnc,
1605                       Bitstream * bs,
1606                       uint32_t * pBits)
1607  {  {
1608            int mb_width = pEnc->mbParam.mb_width;
1609            int mb_height = pEnc->mbParam.mb_height;
1610    
1611          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1612          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1613    
1614          uint16_t x, y;          uint16_t x, y;
1615    
1616            if ((pEnc->current->global_flags & XVID_REDUCED))
1617            {
1618                    mb_width = (pEnc->mbParam.width + 31) / 32;
1619                    mb_height = (pEnc->mbParam.height + 31) / 32;
1620    
1621                    /* 16x16->8x8 downsample requires 1 additional edge pixel*/
1622                    /* XXX: setedges is overkill */
1623                    start_timer();
1624                    image_setedges(&pEnc->current->image,
1625                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1626                            pEnc->mbParam.width, pEnc->mbParam.height);
1627                    stop_edges_timer();
1628            }
1629    
1630          pEnc->iFrameNum = 0;          pEnc->iFrameNum = 0;
1631          pEnc->mbParam.rounding_type = 1;          pEnc->mbParam.m_rounding_type = 1;
1632          pEnc->mbParam.coding_type = I_VOP;          pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1633            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1634            pEnc->current->coding_type = I_VOP;
1635    
1636          BitstreamWriteVolHeader(bs, &pEnc->mbParam);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
         BitstreamWriteVopHeader(bs, &pEnc->mbParam);  
1637    
1638          *pBits = BitstreamPos(bs);          set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1639    
1640          pEnc->sStat.iTextBits = 0;          BitstreamPadAlways(bs);
1641          pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height;          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
         pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1642    
1643          for (y = 0; y < pEnc->mbParam.mb_height; y++)          *pBits = BitstreamPos(bs);
1644                  for (x = 0; x < pEnc->mbParam.mb_width; x++)  
1645                  {          pEnc->current->sStat.iTextBits = 0;
1646                          MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];          pEnc->current->sStat.kblks = mb_width * mb_height;
1647            pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
1648    
1649            for (y = 0; y < mb_height; y++)
1650                    for (x = 0; x < mb_width; x++) {
1651                            MACROBLOCK *pMB =
1652                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1653    
1654                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1655    
1656                          MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1657                                                              dct_codes, qcoeff);
1658    
1659                          start_timer();                          start_timer();
1660                          MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1661                          stop_prediction_timer();                          stop_prediction_timer();
1662    
1663                          start_timer();                          start_timer();
1664                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          if (pEnc->current->global_flags & XVID_GREYSCALE)
1665                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1666                                    qcoeff[4*64+0]=0;               /* zero, because for INTRA MBs DC value is saved */
1667                                    qcoeff[5*64+0]=0;
1668                            }
1669                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1670                          stop_coding_timer();                          stop_coding_timer();
1671                  }                  }
1672    
1673            if ((pEnc->current->global_flags & XVID_REDUCED))
1674            {
1675                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
1676                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
1677                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1678            }
1679          emms();          emms();
1680    
1681          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
1682          pEnc->sStat.fMvPrevSigma = -1;          pEnc->fMvPrevSigma = -1;
1683          pEnc->sStat.iMvSum = 0;          pEnc->mbParam.m_fcode = 2;
         pEnc->sStat.iMvCount = 0;  
         pEnc->mbParam.fixed_code = 2;  
1684    
1685          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1686                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1687          }          }
1688    
# Line 644  Line 1691 
1691    
1692    
1693  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1694    #define BFRAME_SKIP_THRESHHOLD 30
1695    
1696  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  
1697    /* FrameCodeP also handles S(GMC)-VOPs */
1698    static int
1699    FrameCodeP(Encoder * pEnc,
1700                       Bitstream * bs,
1701                       uint32_t * pBits,
1702                       bool force_inter,
1703                       bool vol_header)
1704  {  {
1705          float fSigma;          float fSigma;
1706    
1707          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
1708          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(qcoeff,    6, 64, int16_t, CACHE_LINE);
1709    
1710            int mb_width = pEnc->mbParam.mb_width;
1711            int mb_height = pEnc->mbParam.mb_height;
1712    
1713          int iLimit;          int iLimit;
1714          uint32_t x, y;          int x, y, k;
1715          int iSearchRange;          int iSearchRange;
1716          bool bIntra;          int bIntra, skip_possible;
1717    
1718            /* IMAGE *pCurrent = &pEnc->current->image; */
1719            IMAGE *pRef = &pEnc->reference->image;
1720    
1721            if ((pEnc->current->global_flags & XVID_REDUCED))
1722            {
1723                    mb_width = (pEnc->mbParam.width + 31) / 32;
1724                    mb_height = (pEnc->mbParam.height + 31) / 32;
1725            }
1726    
         IMAGE *pCurrent = &pEnc->sCurrent;  
         IMAGE *pRef = &pEnc->sReference;  
1727    
1728          start_timer();          start_timer();
1729          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1730                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height);
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
                        pEnc->mbParam.global_flags & XVID_INTERLACING);  
1731          stop_edges_timer();          stop_edges_timer();
1732    
1733          pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type;          pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type;
1734            pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type;
1735            pEnc->current->quarterpel =  pEnc->mbParam.m_quarterpel;
1736            pEnc->current->fcode = pEnc->mbParam.m_fcode;
1737    
1738          if (!force_inter)          if (!force_inter)
1739                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit = (int)(mb_width * mb_height *  INTRA_THRESHOLD);
1740          else          else
1741                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = mb_width * mb_height + 1;
1742    
1743          if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1744                  start_timer();                  start_timer();
1745                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1746                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1747                                    pEnc->mbParam.rounding_type);                                                    pEnc->mbParam.edged_height,
1748                                                      pEnc->mbParam.m_quarterpel,
1749                                                      pEnc->current->rounding_type);
1750                  stop_inter_timer();                  stop_inter_timer();
1751          }          }
1752    
1753            pEnc->current->coding_type = P_VOP;
1754    
1755          start_timer();          start_timer();
1756          if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET)
         {  
1757                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
         }  
1758          else          else
1759          {                  bIntra =
1760                  bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference,                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1761                                            &pEnc->vInterH, &pEnc->vInterV,                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1762                                            &pEnc->vInterHV, &pEnc->sCurrent, iLimit);                           iLimit);
1763          }  
1764          stop_motion_timer();          stop_motion_timer();
1765    
1766          if (bIntra == 1)          if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits);
1767    
1768            if ( ( pEnc->current->global_flags & XVID_GMC )
1769                    && ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) )
1770          {          {
1771                  return FrameCodeI(pEnc, bs, pBits);                  pEnc->current->coding_type = S_VOP;
1772          }  
1773                    generate_GMCparameters( 2, 16, &pEnc->current->warp,
1774                                            pEnc->mbParam.width, pEnc->mbParam.height,
1775                                            &pEnc->current->gmc_data);
1776    
1777                    generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image,
1778                                    pEnc->mbParam.mb_width, pEnc->mbParam.mb_height,
1779                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2,
1780                                    pEnc->mbParam.m_fcode, pEnc->mbParam.m_quarterpel, 0,
1781                                    pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC);
1782    
1783          pEnc->mbParam.coding_type = P_VOP;          }
1784    
1785            set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
1786          if(vol_header)          if(vol_header)
1787                  BitstreamWriteVolHeader(bs, &pEnc->mbParam);          {       BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1788                    BitstreamPadAlways(bs);
1789            }
1790    
1791          BitstreamWriteVopHeader(bs, &pEnc->mbParam);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1792    
1793          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1794    
1795          pEnc->sStat.iTextBits = 0;          pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount =
1796          pEnc->sStat.iMvSum = 0;                  pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0;
         pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
1797    
1798          for(y = 0; y < pEnc->mbParam.mb_height; y++)  
1799          {          for (y = 0; y < mb_height; y++) {
1800                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < mb_width; x++) {
1801                  {                          MACROBLOCK *pMB =
1802                          MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1803    
1804    /* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */
1805    /* For a start, leave INTRA decision as is, only choose only between INTER/GMC  - gruel, 9.1.2002 */
1806    
1807                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1808    
1809                          if (!bIntra)                          if (bIntra) {
1810                          {                                  CodeIntraMB(pEnc, pMB);
1811                                    MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1812                                                                      dct_codes, qcoeff);
1813    
1814                                    start_timer();
1815                                    MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1816                                    stop_prediction_timer();
1817    
1818                                    pEnc->current->sStat.kblks++;
1819    
1820                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1821                                    stop_coding_timer();
1822                                    continue;
1823                            }
1824    
1825                            if (pEnc->current->coding_type == S_VOP) {
1826    
1827                                    int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1828                                            pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1829                                            pEnc->mbParam.edged_width, 65536);
1830    
1831                                    if (pEnc->current->motion_flags & PMV_CHROMA16) {
1832                                            iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1833                                            pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1834    
1835                                            iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x,
1836                                            pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2);
1837                                    }
1838    
1839                                    if (iSAD <= pMB->sad16) {               /* mode decision GMC */
1840    
1841                                            if (pEnc->mbParam.m_quarterpel)
1842                                                    pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1843                                            else
1844                                                    pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1845    
1846                                            pMB->mode = MODE_INTER;
1847                                            pMB->mcsel = 1;
1848                                            pMB->sad16 = iSAD;
1849                                    } else {
1850                                            pMB->mcsel = 0;
1851                                    }
1852                            } else {
1853                                    pMB->mcsel = 0; /* just a precaution */
1854                            }
1855    
1856                                  start_timer();                                  start_timer();
1857                                  MBMotionCompensation(pMB,                          MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1858                                                       x, y,                                                                   &pEnc->vInterH, &pEnc->vInterV,
1859                                                       &pEnc->sReference,                                                                   &pEnc->vInterHV, &pEnc->vGMC,
1860                                                       &pEnc->vInterH,                                                                   &pEnc->current->image,
1861                                                       &pEnc->vInterV,                                                                   dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterHV,  
                                                      &pEnc->sCurrent,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1862                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1863                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1864                                                       pEnc->mbParam.rounding_type);                                                                   pEnc->mbParam.m_quarterpel,
1865                                                                     (pEnc->current->global_flags & XVID_REDUCED),
1866                                                                     pEnc->current->rounding_type);
1867    
1868                                  stop_comp_timer();                                  stop_comp_timer();
1869    
1870                                  if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) {                          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1871                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1872                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1873                                                  pEnc->mbParam.quant += DQtab[pMB->dquant];                                          pEnc->current->quant += DQtab[pMB->dquant];
1874                                                  if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31;                                          if (pEnc->current->quant > 31)
1875                                                  else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1;                                                  pEnc->current->quant = 31;
1876                                            else if (pEnc->current->quant < 1)
1877                                                    pEnc->current->quant = 1;
1878                                          }                                          }
1879                                  }                                  }
1880                                  pMB->quant = pEnc->mbParam.quant;                          pMB->quant = pEnc->current->quant;
1881    
1882                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1883    
1884                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                          if (pMB->mode != MODE_NOT_CODED)
1885                            {       pMB->cbp =
1886                                            MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1887                                                                              dct_codes, qcoeff);
1888                          }                          }
1889                          else  
1890                          {                          if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1891                                  CodeIntraMB(pEnc, pMB);                                     pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1892                                  MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent);                                     pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) {
1893                                    pEnc->current->sStat.mblks++;
1894                            }  else {
1895                                    pEnc->current->sStat.ublks++;
1896                          }                          }
1897    
1898                          start_timer();                          start_timer();
                         MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs);  
                         stop_prediction_timer();  
1899    
1900                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          /* Finished processing the MB, now check if to CODE or SKIP */
1901                          {  
1902                                  pEnc->sStat.kblks++;                          skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) &&
1903                                                            (pMB->dquant == NO_CHANGE);
1904    
1905                            if (pEnc->current->coding_type == S_VOP)
1906                                    skip_possible &= (pMB->mcsel == 1);
1907                            else if (pEnc->current->coding_type == P_VOP) {
1908                                    if (pEnc->mbParam.m_quarterpel)
1909                                            skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) );
1910                                    else
1911                                            skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) );
1912                          }                          }
1913                          else if (pMB->cbp ||  
1914                                   pMB->mvs[0].x || pMB->mvs[0].y ||                          if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) {
1915                                   pMB->mvs[1].x || pMB->mvs[1].y ||  
1916                                   pMB->mvs[2].x || pMB->mvs[2].y ||  /* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */
1917                                   pMB->mvs[3].x || pMB->mvs[3].y)  
1918                                    if (pEnc->current->coding_type == P_VOP)        /* special rule for P-VOP's SKIP */
1919                                    {
1920                                            int bSkip = 1;
1921    
1922                                            for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++)
1923                          {                          {
1924                                  pEnc->sStat.mblks++;                                                  int iSAD;
1925                                                    iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1926                                                                            pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x,
1927                                                                    pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD);
1928                                                    if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant)
1929                                                    {       bSkip = 0;
1930                                                            break;
1931                          }                          }
1932                          else                                          }
1933    
1934                                            if (!bSkip) {   /* no SKIP, but trivial block */
1935                                                    if(pEnc->mbParam.m_quarterpel) {
1936                                                            VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1937                                                            pMB->pmvs[0].x = - predMV.x;
1938                                                            pMB->pmvs[0].y = - predMV.y;
1939                                                    }
1940                                                    else {
1941                                                            VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1942                                                            pMB->pmvs[0].x = - predMV.x;
1943                                                            pMB->pmvs[0].y = - predMV.y;
1944                                                    }
1945                                                    pMB->mode = MODE_INTER;
1946                                                    pMB->cbp = 0;
1947                                                    MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
1948                                                    stop_coding_timer();
1949    
1950                                                    continue;       /* next MB */
1951                                            }
1952                                    }
1953                                    /* do SKIP */
1954    
1955                                    pMB->mode = MODE_NOT_CODED;
1956                                    MBSkip(bs);
1957                                    stop_coding_timer();
1958                                    continue;       /* next MB */
1959                            }
1960                            /* ordinary case: normal coded INTER/INTER4V block */
1961    
1962                            if (pEnc->current->global_flags & XVID_GREYSCALE)
1963                            {       pMB->cbp &= 0x3C;               /* keep only bits 5-2 */
1964                                    qcoeff[4*64+0]=0;               /* zero, because DC for INTRA MBs DC value is saved */
1965                                    qcoeff[5*64+0]=0;
1966                            }
1967    
1968                            if(pEnc->mbParam.m_quarterpel) {
1969                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1970                                    pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x;
1971                                    pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y;
1972                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1973                            } else {
1974                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0);
1975                                    pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x;
1976                                    pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y;
1977                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y);
1978                            }
1979    
1980    
1981                            if (pMB->mode == MODE_INTER4V)
1982                            {       int k;
1983                                    for (k=1;k<4;k++)
1984                          {                          {
1985                                  pEnc->sStat.ublks++;                                          if(pEnc->mbParam.m_quarterpel) {
1986                                                    VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1987                                                    pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x;
1988                                                    pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y;
1989                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1990                                            } else {
1991                                                    VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k);
1992                                                    pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x;
1993                                                    pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y;
1994                                    DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y);
1995                          }                          }
1996    
1997                          start_timer();                                  }
1998                          MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat);                          }
1999    
2000                            MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat);
2001                          stop_coding_timer();                          stop_coding_timer();
2002    
2003                    }
2004                  }                  }
2005    
2006            if ((pEnc->current->global_flags & XVID_REDUCED))
2007            {
2008                    image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width,
2009                            pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width,
2010                            16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
2011          }          }
2012    
2013          emms();          emms();
2014    
2015          if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
2016                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
2017          }          }
2018    
2019          if (pEnc->sStat.iMvCount == 0)          if (pEnc->current->sStat.iMvCount == 0)
2020                  pEnc->sStat.iMvCount = 1;                  pEnc->current->sStat.iMvCount = 1;
2021    
2022          fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount);          fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount);
2023    
2024          iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code);          iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode);
2025    
2026          if ((fSigma > iSearchRange / 3)          if ((fSigma > iSearchRange / 3)
2027              && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128                  && (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128
2028          {          {
2029                  pEnc->mbParam.fixed_code++;                  pEnc->mbParam.m_fcode++;
2030                  iSearchRange *= 2;                  iSearchRange *= 2;
2031          }          } else if ((fSigma < iSearchRange / 6)
2032          else if ((fSigma < iSearchRange / 6)                             && (pEnc->fMvPrevSigma >= 0)
2033                   && (pEnc->sStat.fMvPrevSigma >= 0)                             && (pEnc->fMvPrevSigma < iSearchRange / 6)
2034                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                          && (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16
                  && (pEnc->mbParam.fixed_code >= 2))    // minimum search range 16  
2035          {          {
2036                  pEnc->mbParam.fixed_code--;                  pEnc->mbParam.m_fcode--;
2037                  iSearchRange /= 2;                  iSearchRange /= 2;
2038          }          }
2039    
2040          pEnc->sStat.fMvPrevSigma = fSigma;          pEnc->fMvPrevSigma = fSigma;
2041    
2042            /* frame drop code */
2043            DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks);
2044            if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks <
2045                    (pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100)
2046            {
2047                    pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0;
2048                    pEnc->current->sStat.ublks = mb_width * mb_height;
2049    
2050                    BitstreamReset(bs);
2051    
2052                    set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase);
2053                    BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0);
2054    
2055                    // copy reference frame details into the current frame
2056                    pEnc->current->quant = pEnc->reference->quant;
2057                    pEnc->current->motion_flags = pEnc->reference->motion_flags;
2058                    pEnc->current->rounding_type = pEnc->reference->rounding_type;
2059                    pEnc->current->quarterpel =  pEnc->reference->quarterpel;
2060                    pEnc->current->fcode = pEnc->reference->fcode;
2061                    pEnc->current->bcode = pEnc->reference->bcode;
2062                    image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height);
2063                    memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height);
2064            }
2065    
2066            /* XXX: debug
2067            {
2068                    char s[100];
2069                    sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum);
2070                    image_dump_yuvpgm(&pEnc->current->image,
2071                            pEnc->mbParam.edged_width,
2072                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2073    
2074                    sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum);
2075                    image_dump_yuvpgm(&pEnc->reference->image,
2076                            pEnc->mbParam.edged_width,
2077                            pEnc->mbParam.width, pEnc->mbParam.height, s);
2078            }
2079            */
2080    
2081    
2082          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2083    
# Line 825  Line 2085 
2085  }  }
2086    
2087    
2088    static void
2089  /*  FrameCodeB(Encoder * pEnc,
2090  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)                     FRAMEINFO * frame,
2091                       Bitstream * bs,
2092                       uint32_t * pBits)
2093  {  {
2094      int16_t dct_codes[6][64];          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
2095      int16_t qcoeff[6][64];          DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE);
2096      uint32_t x, y;      uint32_t x, y;
         VECTOR forward;  
         VECTOR backward;  
2097    
2098      IMAGE *f_ref = &pEnc->reference->image;      IMAGE *f_ref = &pEnc->reference->image;
2099          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
2100    
2101    #ifdef BFRAMES_DEC_DEBUG
2102            FILE *fp;
2103            static char first=0;
2104    #define BFRAME_DEBUG    if (!first && fp){ \
2105                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
2106            }
2107    
2108            pEnc->current->global_flags &= ~XVID_REDUCED;   /* reduced resoltion not yet supported */
2109    
2110            if (!first){
2111                    fp=fopen("C:\\XVIDDBGE.TXT","w");
2112            }
2113    #endif
2114    
2115            frame->quarterpel =  pEnc->mbParam.m_quarterpel;
2116    
2117          // forward          // forward
2118          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(f_ref, pEnc->mbParam.edged_width,
2119                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
2120                                       pEnc->mbParam.height);
2121          start_timer();          start_timer();
2122          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2123                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2124                                              pEnc->mbParam.m_quarterpel, 0);
2125          stop_inter_timer();          stop_inter_timer();
2126    
2127          // backward          // backward
2128          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height);          image_setedges(b_ref, pEnc->mbParam.edged_width,
2129                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
2130                                       pEnc->mbParam.height);
2131      start_timer();      start_timer();
2132          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
2133                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
2134                                              pEnc->mbParam.m_quarterpel, 0);
2135          stop_inter_timer();          stop_inter_timer();
2136    
2137          start_timer();          start_timer();
2138    
2139          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame,
2140                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                  ((int32_t)(pEnc->current->stamp - frame->stamp)),                               // time_bp
2141                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                  ((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)),     // time_pp
2142                            pEnc->reference->mbs, f_ref,
2143                                                     &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
2144                                                     pEnc->current, b_ref, &pEnc->vInterH,
2145                                                     &pEnc->vInterV, &pEnc->vInterHV);
2146    
2147    
2148          stop_motion_timer();          stop_motion_timer();
2149    
2150          if (test_quant_type(&pEnc->mbParam, pEnc->current))          /*if (test_quant_type(&pEnc->mbParam, pEnc->current))
2151          {          {
2152                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);                  BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type);
2153          }             } */
2154    
2155      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
     BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0,  
                         frame->quant, frame->fcode, frame->bcode);  
   
     *pBits = BitstreamPos(bs);  
2156    
2157      pEnc->sStat.iTextBits = 0;          set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase);
2158      pEnc->sStat.iMvSum = 0;          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
     pEnc->sStat.iMvCount = 0;  
         pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;  
2159    
2160            *pBits = BitstreamPos(bs);
2161    
2162      for (y = 0; y < pEnc->mbParam.mb_height; y++)          frame->sStat.iTextBits = 0;
2163          {          frame->sStat.iMvSum = 0;
2164                  // reset prediction          frame->sStat.iMvCount = 0;
2165            frame->sStat.kblks = frame->sStat.mblks = frame->sStat.ublks = 0;
2166    
                 forward.x = 0;  
                 forward.y = 0;  
                 backward.x = 0;  
                 backward.y = 0;  
2167    
2168                  for (x = 0; x < pEnc->mbParam.mb_width; x++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
2169                  {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
2170                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
2171                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          int direction = pEnc->mbParam.global & XVID_ALTERNATESCAN ? 2 : 0;
                         MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];  
2172    
2173                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
2174                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
2175                          {                                  //mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0;
                                 mb->mvs[0].x = 0;  
                                 mb->mvs[0].y = 0;  
2176                                  continue;                                  continue;
2177                          }                          }
2178    
2179                            if (mb->mode != MODE_DIRECT_NONE_MV) {
2180                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
2181                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
2182                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
2183                                                                             &pEnc->vInterV, &pEnc->vInterHV,
2184                                          dct_codes);                                          dct_codes);
2185    
2186                                    if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT;
2187                          mb->quant = frame->quant;                          mb->quant = frame->quant;
                         mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff);  
                         //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);  
2188    
2189                                    mb->cbp =
2190                                            MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff);
2191    
2192                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                                  if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0)
2193                                  mb->cbp == 0 &&                                          && (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) {
2194                                  mb->mvs[0].x == 0 &&                                          mb->mode = MODE_DIRECT_NONE_MV; // skipped
                                 mb->mvs[0].y == 0)  
                         {  
                                 mb->mode = 5;  // skipped  
                         }  
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)  
                         {  
                                 mb->pmvs[0].x = mb->mvs[0].x - forward.x;  
                                 mb->pmvs[0].y = mb->mvs[0].y - forward.y;  
                                 forward.x = mb->mvs[0].x;  
                                 forward.y = mb->mvs[0].y;  
2195                          }                          }
   
                         if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)  
                         {  
                                 mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;  
                                 mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;  
                                 backward.x = mb->b_mvs[0].x;  
                                 backward.y = mb->b_mvs[0].y;  
2196                          }                          }
2197    
2198  //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  #ifdef BFRAMES_DEC_DEBUG
2199            BFRAME_DEBUG
2200    #endif
2201                          start_timer();                          start_timer();
2202                          MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
2203                                                     &frame->sStat, direction);
2204                          stop_coding_timer();                          stop_coding_timer();
2205                  }                  }
2206          }          }
# Line 947  Line 2211 
2211    
2212          *pBits = BitstreamPos(bs) - *pBits;          *pBits = BitstreamPos(bs) - *pBits;
2213    
2214    #ifdef BFRAMES_DEC_DEBUG
2215            if (!first){
2216                    first=1;
2217                    if (fp)
2218                            fclose(fp);
2219            }
2220    #endif
2221  }  }
   
 */  

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.76.2.41

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