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

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.63

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