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

Diff of /xvidcore/src/encoder.c

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

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

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

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