[svn] / branches / dev-api-3 / xvidcore / src / encoder.c Repository:
ViewVC logotype

Diff of /branches/dev-api-3/xvidcore/src/encoder.c

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

revision 194, Sun Jun 9 23:30:50 2002 UTC revision 234, Sun Jun 23 03:58:32 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History   *  History
34   *   *
35     *      20.06.2002 bframe patch
36   *  08.05.2002 fix some problem in DEBUG mode;   *  08.05.2002 fix some problem in DEBUG mode;
37   *             MinChen <chenm001@163.com>   *             MinChen <chenm001@163.com>
38   *  14.04.2002 added FrameCodeB()   *  14.04.2002 added FrameCodeB()
39   *   *
40   *  $Id: encoder.c,v 1.40 2002-06-09 23:30:50 edgomez Exp $   *  $Id: encoder.c,v 1.46 2002-06-23 03:58:32 suxen_drol Exp $
41   *   *
42   ****************************************************************************/   ****************************************************************************/
43    
44  #include <stdlib.h>  #include <stdlib.h>
45  #include <stdio.h>  #include <stdio.h>
46  #include <math.h>  #include <math.h>
47    #include <string.h>
48    
49  #include "encoder.h"  #include "encoder.h"
50  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
51  #include "global.h"  #include "global.h"
52  #include "utils/timer.h"  #include "utils/timer.h"
53  #include "image/image.h"  #include "image/image.h"
54    #include "image/font.h"
55  #include "motion/motion.h"  #include "motion/motion.h"
56  #include "bitstream/cbp.h"  #include "bitstream/cbp.h"
57  #include "utils/mbfunctions.h"  #include "utils/mbfunctions.h"
# Line 93  Line 96 
96   * Local data   * Local data
97   ****************************************************************************/   ****************************************************************************/
98    
99  static int DQtab[4] =  static int DQtab[4] = {
 {  
100          -1, -2, 1, 2          -1, -2, 1, 2
101  };  };
102    
103  static int iDQtab[5] =  static int iDQtab[5] = {
 {  
104          1, 0, NO_CHANGE, 2, 3          1, 0, NO_CHANGE, 2, 3
105  };  };
106    
107    
108  static void __inline image_null(IMAGE * image)  static void __inline
109    image_null(IMAGE * image)
110  {  {
111          image->y = image->u = image->v = NULL;          image->y = image->u = image->v = NULL;
112  }  }
# Line 132  Line 134 
134  encoder_create(XVID_ENC_PARAM * pParam)  encoder_create(XVID_ENC_PARAM * pParam)
135  {  {
136          Encoder *pEnc;          Encoder *pEnc;
137          uint32_t i;          int i;
138    
139          pParam->handle = NULL;          pParam->handle = NULL;
140    
# Line 145  Line 147 
147    
148          /* Fps */          /* Fps */
149    
150          if (pParam->fincr <= 0 || pParam->fbase <= 0)          if (pParam->fincr <= 0 || pParam->fbase <= 0) {
         {  
151                  pParam->fincr = 1;                  pParam->fincr = 1;
152                  pParam->fbase = 25;                  pParam->fbase = 25;
153          }          }
# Line 157  Line 158 
158           */           */
159    
160          i = pParam->fincr;          i = pParam->fincr;
161          while (i > 1)          while (i > 1) {
162          {                  if (pParam->fincr % i == 0 && pParam->fbase % i == 0) {
                 if (pParam->fincr % i == 0 && pParam->fbase % i == 0)  
                 {  
163                          pParam->fincr /= i;                          pParam->fincr /= i;
164                          pParam->fbase /= i;                          pParam->fbase /= i;
165                          i = pParam->fincr;                          i = pParam->fincr;
# Line 169  Line 168 
168                  i--;                  i--;
169          }          }
170    
171          if (pParam->fbase > 65535)          if (pParam->fbase > 65535) {
         {  
172                  float div = (float)pParam->fbase / 65535;                  float div = (float)pParam->fbase / 65535;
173    
174                  pParam->fbase = (int)(pParam->fbase / div);                  pParam->fbase = (int)(pParam->fbase / div);
175                  pParam->fincr = (int)(pParam->fincr / div);                  pParam->fincr = (int)(pParam->fincr / div);
176          }          }
# Line 211  Line 210 
210          if (pEnc == NULL)          if (pEnc == NULL)
211                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
212    
213            /* Zero the Encoder Structure */
214    
215            memset(pEnc, 0, sizeof(Encoder));
216    
217          /* Fill members of Encoder structure */          /* Fill members of Encoder structure */
218    
219          pEnc->mbParam.width = pParam->width;          pEnc->mbParam.width = pParam->width;
# Line 225  Line 228 
228          pEnc->mbParam.fbase = pParam->fbase;          pEnc->mbParam.fbase = pParam->fbase;
229          pEnc->mbParam.fincr = pParam->fincr;          pEnc->mbParam.fincr = pParam->fincr;
230    
231            pEnc->mbParam.m_quant_type = H263_QUANT;
232    
233          pEnc->sStat.fMvPrevSigma = -1;          pEnc->sStat.fMvPrevSigma = -1;
234    
235          /* Fill rate control parameters */          /* Fill rate control parameters */
# Line 244  Line 249 
249    
250          /* try to allocate mb memory */          /* try to allocate mb memory */
251    
252          pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \          pEnc->current->mbs =
253                                           pEnc->mbParam.mb_width * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
254                                           pEnc->mbParam.mb_height,                                          pEnc->mbParam.mb_height, CACHE_LINE);
255                                           CACHE_LINE);          pEnc->reference->mbs =
256          pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
257                                             pEnc->mbParam.mb_width * \                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                            pEnc->mbParam.mb_height,  
                                            CACHE_LINE);  
258    
259          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)          if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL)
260                  goto xvid_err_memory2;                  goto xvid_err_memory2;
261    
262          /* try to allocate image memory */          /* try to allocate image memory */
263    
264  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
265          image_null(&pEnc->sOriginal);          image_null(&pEnc->sOriginal);
266  #endif  #endif
267  #ifdef BFRAMES  #ifdef BFRAMES
# Line 274  Line 277 
277          image_null(&pEnc->vInterHV);          image_null(&pEnc->vInterHV);
278          image_null(&pEnc->vInterHVf);          image_null(&pEnc->vInterHVf);
279    
280  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
281          if (image_create(&pEnc->sOriginal,          if (image_create
282                           pEnc->mbParam.edged_width,                  (&pEnc->sOriginal, pEnc->mbParam.edged_width,
283                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
284                  goto xvid_err_memory3;                  goto xvid_err_memory3;
285  #endif  #endif
286  #ifdef BFRAMES  #ifdef BFRAMES
287          if (image_create(&pEnc->f_refh,          if (image_create
288                           pEnc->mbParam.edged_width,                  (&pEnc->f_refh, pEnc->mbParam.edged_width,
289                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
290                  goto xvid_err_memory3;                  goto xvid_err_memory3;
291          if (image_create(&pEnc->f_refv,          if (image_create
292                           pEnc->mbParam.edged_width,                  (&pEnc->f_refv, pEnc->mbParam.edged_width,
293                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
294                  goto xvid_err_memory3;                  goto xvid_err_memory3;
295          if (image_create(&pEnc->f_refhv,          if (image_create
296                           pEnc->mbParam.edged_width,                  (&pEnc->f_refhv, pEnc->mbParam.edged_width,
297                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
298                  goto xvid_err_memory3;                  goto xvid_err_memory3;
299  #endif  #endif
300          if (image_create(&pEnc->current->image,          if (image_create
301                           pEnc->mbParam.edged_width,                  (&pEnc->current->image, pEnc->mbParam.edged_width,
302                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
303                  goto xvid_err_memory3;                  goto xvid_err_memory3;
304          if (image_create(&pEnc->reference->image,          if (image_create
305                           pEnc->mbParam.edged_width,                  (&pEnc->reference->image, pEnc->mbParam.edged_width,
306                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
307                  goto xvid_err_memory3;                  goto xvid_err_memory3;
308          if (image_create(&pEnc->vInterH,          if (image_create
309                           pEnc->mbParam.edged_width,                  (&pEnc->vInterH, pEnc->mbParam.edged_width,
310                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
311                  goto xvid_err_memory3;                  goto xvid_err_memory3;
312          if (image_create(&pEnc->vInterV,          if (image_create
313                           pEnc->mbParam.edged_width,                  (&pEnc->vInterV, pEnc->mbParam.edged_width,
314                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
315                  goto xvid_err_memory3;                  goto xvid_err_memory3;
316          if (image_create(&pEnc->vInterVf,          if (image_create
317                           pEnc->mbParam.edged_width,                  (&pEnc->vInterVf, pEnc->mbParam.edged_width,
318                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
319                  goto xvid_err_memory3;                  goto xvid_err_memory3;
320          if (image_create(&pEnc->vInterHV,          if (image_create
321                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHV, pEnc->mbParam.edged_width,
322                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
323                  goto xvid_err_memory3;                  goto xvid_err_memory3;
324          if (image_create(&pEnc->vInterHVf,          if (image_create
325                           pEnc->mbParam.edged_width,                  (&pEnc->vInterHVf, pEnc->mbParam.edged_width,
326                           pEnc->mbParam.edged_height) < 0)                           pEnc->mbParam.edged_height) < 0)
327                  goto xvid_err_memory3;                  goto xvid_err_memory3;
328    
# Line 328  Line 331 
331          /* B Frames specific init */          /* B Frames specific init */
332  #ifdef BFRAMES  #ifdef BFRAMES
333    
334            pEnc->global = pParam->global;
335          pEnc->mbParam.max_bframes = pParam->max_bframes;          pEnc->mbParam.max_bframes = pParam->max_bframes;
336          pEnc->bquant_ratio = pParam->bquant_ratio;          pEnc->bquant_ratio = pParam->bquant_ratio;
337          pEnc->bframes = NULL;          pEnc->bframes = NULL;
338    
339          if (pEnc->mbParam.max_bframes > 0)          if (pEnc->mbParam.max_bframes > 0) {
         {  
340                  int n;                  int n;
341    
342                  pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \                  pEnc->bframes =
343                                              sizeof(FRAMEINFO *),                          xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *),
344                                              CACHE_LINE);                                              CACHE_LINE);
345    
346                  if (pEnc->bframes == NULL)                  if (pEnc->bframes == NULL)
# Line 347  Line 350 
350                          pEnc->bframes[n] = NULL;                          pEnc->bframes[n] = NULL;
351    
352    
353                  for (n = 0; n < pEnc->mbParam.max_bframes; n++)                  for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
354                  {                          pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE);
                         pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO),  
                                                        CACHE_LINE);  
355    
356                          if (pEnc->bframes[n] == NULL)                          if (pEnc->bframes[n] == NULL)
357                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
358    
359                          pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \                          pEnc->bframes[n]->mbs =
360                                                              pEnc->mbParam.mb_width * \                                  xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width *
361                                                              pEnc->mbParam.mb_height,                                                          pEnc->mbParam.mb_height, CACHE_LINE);
                                                             CACHE_LINE);  
362    
363                          if (pEnc->bframes[n]->mbs == NULL)                          if (pEnc->bframes[n]->mbs == NULL)
364                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
365    
366                          image_null(&pEnc->bframes[n]->image);                          image_null(&pEnc->bframes[n]->image);
367    
368                          if (image_create(&pEnc->bframes[n]->image,                          if (image_create
369                                           pEnc->mbParam.edged_width,                                  (&pEnc->bframes[n]->image, pEnc->mbParam.edged_width,
370                                           pEnc->mbParam.edged_height) < 0)                                           pEnc->mbParam.edged_height) < 0)
371                                  goto xvid_err_memory4;                                  goto xvid_err_memory4;
372    
# Line 377  Line 377 
377          pEnc->bframenum_tail = 0;          pEnc->bframenum_tail = 0;
378          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
379    
380            pEnc->queue = NULL;
381    
382    
383            if (pEnc->mbParam.max_bframes > 0) {
384                    int n;
385    
386                    pEnc->queue =
387                            xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE),
388                                                    CACHE_LINE);
389    
390                    if (pEnc->queue == NULL)
391                            goto xvid_err_memory4;
392    
393                    for (n = 0; n < pEnc->mbParam.max_bframes; n++)
394                            image_null(&pEnc->queue[n]);
395    
396                    for (n = 0; n < pEnc->mbParam.max_bframes; n++) {
397                            if (image_create
398                                    (&pEnc->queue[n], pEnc->mbParam.edged_width,
399                                     pEnc->mbParam.edged_height) < 0)
400                                    goto xvid_err_memory5;
401    
402                    }
403            }
404    
405            pEnc->queue_head = 0;
406            pEnc->queue_tail = 0;
407            pEnc->queue_size = 0;
408    
409    
410          pEnc->mbParam.m_seconds = 0;          pEnc->mbParam.m_seconds = 0;
411          pEnc->mbParam.m_ticks = 0;          pEnc->mbParam.m_ticks = 0;
412            pEnc->m_framenum = 0;
413  #endif  #endif
414    
415          pParam->handle = (void *)pEnc;          pParam->handle = (void *)pEnc;
416    
417          if (pParam->rc_bitrate)          if (pParam->rc_bitrate) {
418          {                  RateControlInit(&pEnc->rate_control, pParam->rc_bitrate,
                 RateControlInit(&pEnc->rate_control,  
                                 pParam->rc_bitrate,  
419                                  pParam->rc_reaction_delay_factor,                                  pParam->rc_reaction_delay_factor,
420                                  pParam->rc_averaging_period,                                                  pParam->rc_averaging_period, pParam->rc_buffer,
                                 pParam->rc_buffer,  
421                                  pParam->fbase * 1000 / pParam->fincr,                                  pParam->fbase * 1000 / pParam->fincr,
422                                  pParam->max_quantizer,                                                  pParam->max_quantizer, pParam->min_quantizer);
                                 pParam->min_quantizer);  
423          }          }
424    
425          init_timer();          init_timer();
# Line 403  Line 430 
430           * We handle all XVID_ERR_MEMORY here, this makes the code lighter           * We handle all XVID_ERR_MEMORY here, this makes the code lighter
431           */           */
432  #ifdef BFRAMES  #ifdef BFRAMES
433      xvid_err_memory5:
434    
435    
436            if (pEnc->mbParam.max_bframes > 0) {
437    
438                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
439                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
440                                                      pEnc->mbParam.edged_height);
441                    }
442                    xvid_free(pEnc->queue);
443            }
444    
445   xvid_err_memory4:   xvid_err_memory4:
         for (i=0; i<pEnc->mbParam.max_bframes; i++)  
         {  
446    
447                  if (pEnc->bframes[i] == NULL) continue;          if (pEnc->mbParam.max_bframes > 0) {
448    
449                  image_destroy(&pEnc->bframes[i]->image,                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
450                                pEnc->mbParam.edged_width,  
451                            if (pEnc->bframes[i] == NULL)
452                                    continue;
453    
454                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
455                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
456    
457                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
# Line 420  Line 461 
461          }          }
462    
463          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
464            }
465    
466  #endif  #endif
467    
468   xvid_err_memory3:   xvid_err_memory3:
469  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
470          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
471                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
472  #endif  #endif
473    
474  #ifdef BFRAMES  #ifdef BFRAMES
475          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
476                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
477          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
478                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
479          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
480                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
481  #endif  #endif
482    
483          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
484                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
485          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
486                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
487          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
488                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
489          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
490                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
491          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
492                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
493          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
494                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
495          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
496                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
497    
498   xvid_err_memory2:   xvid_err_memory2:
# Line 492  Line 523 
523  int  int
524  encoder_destroy(Encoder * pEnc)  encoder_destroy(Encoder * pEnc)
525  {  {
526    #ifdef BFRAMES
527            int i;
528    #endif
529    
530          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
531    
532          /* B Frames specific */          /* B Frames specific */
533  #ifdef BFRAMES  #ifdef BFRAMES
534          int i;          if (pEnc->mbParam.max_bframes > 0) {
535    
536          for (i=0; i<pEnc->mbParam.max_bframes; i++)                  for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
537          {  
538                            image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width,
539                                              pEnc->mbParam.edged_height);
540                    }
541                    xvid_free(pEnc->queue);
542            }
543    
                 if (pEnc->bframes[i] == NULL) continue;  
544    
545                  image_destroy(&pEnc->bframes[i]->image,          if (pEnc->mbParam.max_bframes > 0) {
546                                pEnc->mbParam.edged_width,  
547                    for (i = 0; i < pEnc->mbParam.max_bframes; i++) {
548    
549                            if (pEnc->bframes[i] == NULL)
550                                    continue;
551    
552                            image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width,
553                                pEnc->mbParam.edged_height);                                pEnc->mbParam.edged_height);
554    
555                  xvid_free(pEnc->bframes[i]->mbs);                  xvid_free(pEnc->bframes[i]->mbs);
556    
557                  xvid_free(pEnc->bframes[i]);                  xvid_free(pEnc->bframes[i]);
   
558          }          }
559    
560          xvid_free(pEnc->bframes);          xvid_free(pEnc->bframes);
561    
562            }
563  #endif  #endif
564    
565          /* All images, reference, current etc ... */          /* All images, reference, current etc ... */
566    
567          image_destroy(&pEnc->current->image,          image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
568                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
569          image_destroy(&pEnc->reference->image,          image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
570                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
571          image_destroy(&pEnc->vInterH,          image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
572                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
573          image_destroy(&pEnc->vInterV,          image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
574                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
575          image_destroy(&pEnc->vInterVf,          image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
576                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
577          image_destroy(&pEnc->vInterHV,          image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
578                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
579          image_destroy(&pEnc->vInterHVf,          image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
580                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
581  #ifdef BFRAMES  #ifdef BFRAMES
582          image_destroy(&pEnc->f_refh,          image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
583                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
584          image_destroy(&pEnc->f_refv,          image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
585                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
586          image_destroy(&pEnc->f_refhv,          image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
587                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
588  #endif  #endif
589  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
590          image_destroy(&pEnc->sOriginal,          image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width,
                       pEnc->mbParam.edged_width,  
591                        pEnc->mbParam.edged_height);                        pEnc->mbParam.edged_height);
592  #endif  #endif
593    
# Line 570  Line 604 
604          return XVID_ERR_OK;          return XVID_ERR_OK;
605  }  }
606    
607    
608    #ifdef BFRAMES
609    void inc_frame_num(Encoder * pEnc)
610    {
611            pEnc->iFrameNum++;
612            pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;
613    
614            pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase;
615            pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase;
616    }
617    #endif
618    
619    
620    #ifdef BFRAMES
621    void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame)
622    {
623            if (pEnc->queue_size >= pEnc->mbParam.max_bframes)
624            {
625                    DPRINTF("FATAL: QUEUE FULL");
626                    return;
627            }
628    
629            DPRINTF("*** QUEUE bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
630                                    pEnc->bframenum_head, pEnc->bframenum_tail,
631                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
632    
633    
634            start_timer();
635            if (image_input
636                    (&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height,
637                     pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
638                    return;
639            stop_conv_timer();
640    
641            pEnc->queue_size++;
642            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
643    }
644    #endif
645    
646    
647    #ifdef BFRAMES
648  /*****************************************************************************  /*****************************************************************************
649   * Frame encoder entry point   * IPB frame encoder entry point
  *  
  * At this moment 2 versions coexist : one for IPB compatible encoder,  
  *                                     another one for the old IP encoder.  
650   *   *
651   * Returned values :   * Returned values :
652   *    - XVID_ERR_OK     - no errors   *    - XVID_ERR_OK     - no errors
# Line 582  Line 654 
654   *                        format   *                        format
655   ****************************************************************************/   ****************************************************************************/
656    
   
 #ifdef BFRAMES  
 /*****************************************************************************  
  * Frame encoder entry point for IPB capable encoder  
  ****************************************************************************/  
657  int  int
658  encoder_encode(Encoder * pEnc,  encoder_encode_bframes(Encoder * pEnc,
659                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
660                 XVID_ENC_STATS * pResult)                 XVID_ENC_STATS * pResult)
661  {  {
# Line 596  Line 663 
663          Bitstream bs;          Bitstream bs;
664          uint32_t bits;          uint32_t bits;
665    
666  #ifdef _DEBUG          int input_valid = 1;
667    
668    #ifdef _DEBUG_PSNR
669          float psnr;          float psnr;
670          char temp[128];          char temp[128];
671  #endif  #endif
672    
673          ENC_CHECK(pEnc);          ENC_CHECK(pEnc);
674          ENC_CHECK(pFrame);          ENC_CHECK(pFrame);
675            ENC_CHECK(pFrame->image);
676    
677          start_global_timer();          start_global_timer();
678    
679          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
680    
681    ipvop_loop:
682    
683          /*          /*
684           * bframe "flush" code           * bframe "flush" code
685           */           */
686    
687          if ( (pFrame->image == NULL || pEnc->flush_bframes) &&          if ((pFrame->image == NULL || pEnc->flush_bframes)
688               (pEnc->bframenum_head < pEnc->bframenum_tail))                  && (pEnc->bframenum_head < pEnc->bframenum_tail)) {
         {  
689    
690                  if (pEnc->flush_bframes == 0)                  if (pEnc->flush_bframes == 0) {
                 {  
691                          /*                          /*
692                           * we have reached the end of stream without getting                           * we have reached the end of stream without getting
693                           * a future reference frame... so encode last final                           * a future reference frame... so encode last final
694                           * frame as a pframe                           * frame as a pframe
695                           */                           */
696    
697                          /* ToDo : remove dprintf calls */                          DPRINTF("*** BFRAME (final frame) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
698                          /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
699                            dprintf("--- PFRAME (final frame correction) --- ");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
700                          */  
701                          pEnc->bframenum_tail--;                          pEnc->bframenum_tail--;
702                          SWAP(pEnc->current, pEnc->reference);                          SWAP(pEnc->current, pEnc->reference);
703    
704                          SWAP(pEnc->current,                          SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]);
                              pEnc->bframes[pEnc->bframenum_tail]);  
705    
706                          FrameCodeP(pEnc, &bs, &bits, 1, 0);                          FrameCodeP(pEnc, &bs, &bits, 1, 0);
707    
708                          BitstreamPad(&bs);                          BitstreamPad(&bs);
709                          pFrame->length = BitstreamLength(&bs);                          pFrame->length = BitstreamLength(&bs);
                         pFrame->input_consumed = 0;  
710                          pFrame->intra = 0;                          pFrame->intra = 0;
711    
712                          return XVID_ERR_OK;                          return XVID_ERR_OK;
713                  }                  }
714    
                 /* ToDo : remove dprintf calls */  
                 /*  
                   dprintf("--- BFRAME (flush) --- ");  
                 */  
                 FrameCodeB(pEnc,  
                            pEnc->bframes[pEnc->bframenum_head],  
                            &bs,  
                            &bits);  
                 pEnc->bframenum_head++;  
715    
716                    DPRINTF("*** BFRAME (flush) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
717                                    pEnc->bframenum_head, pEnc->bframenum_tail,
718                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
719    
720                    FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits);
721                    pEnc->bframenum_head++;
722    
723                  BitstreamPad(&bs);                  BitstreamPad(&bs);
724                  pFrame->length = BitstreamLength(&bs);                  pFrame->length = BitstreamLength(&bs);
                 pFrame->input_consumed = 0;  
725                  pFrame->intra = 0;                  pFrame->intra = 0;
726    
727                    if (input_valid)
728                            queue_image(pEnc, pFrame);
729    
730                  return XVID_ERR_OK;                  return XVID_ERR_OK;
731          }          }
732    
733          if (pFrame->image == NULL)          if (pEnc->bframenum_head > 0) {
734          {                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;
735                  pFrame->length = 0;  
736                  pFrame->input_consumed = 1;                  if ((pEnc->global & XVID_GLOBAL_PACKED)) {
737    
738                            DPRINTF("*** EMPTY bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
739                                    pEnc->bframenum_head, pEnc->bframenum_tail,
740                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
741    
742    
743                            BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0);
744                            BitstreamPad(&bs);
745                            BitstreamPutBits(&bs, 0x7f, 8);
746    
747                            pFrame->length = BitstreamLength(&bs);
748                  pFrame->intra = 0;                  pFrame->intra = 0;
749    
750                            if (input_valid)
751                                    queue_image(pEnc, pFrame);
752    
753                  return XVID_ERR_OK;                  return XVID_ERR_OK;
754          }          }
755            }
756    
757    
758          if (pEnc->bframenum_head > 0)  bvop_loop:
759    
760            if (input_valid) {
761    
762                    SWAP(pEnc->current, pEnc->reference);
763    
764                    start_timer();
765                    if (image_input
766                            (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
767                            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
768                            return XVID_ERR_FORMAT;
769                    stop_conv_timer();
770    
771                    // queue input frame, and dequue next image
772                    if (pEnc->queue_size > 0)
773          {          {
774                  pEnc->bframenum_head = pEnc->bframenum_tail = 0;                          image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]);
775                            if (pEnc->queue_head != pEnc->queue_tail)
776                            {
777                                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
778                            }
779                            pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
780                            pEnc->queue_tail =  (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes;
781                    }
782    
783            } else if (pEnc->queue_size > 0) {
784    
785                    SWAP(pEnc->current, pEnc->reference);
786    
787                    image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]);
788                    pEnc->queue_head =  (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes;
789                    pEnc->queue_size--;
790    
791            } else if (BitstreamPos(&bs) == 0) {
792    
793                    DPRINTF("*** SKIP bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
794                                    pEnc->bframenum_head, pEnc->bframenum_tail,
795                                    pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
796    
797    
798                    pFrame->intra = 0;
799    
800                    BitstreamPutBits(&bs, 0x7f, 8);
801                    BitstreamPad(&bs);
802                    pFrame->length = BitstreamLength(&bs);
803    
804                    return XVID_ERR_OK;
805    
806            } else {
807    
808                    pFrame->length = BitstreamLength(&bs);
809                    return XVID_ERR_OK;
810          }          }
811    
812          pEnc->flush_bframes = 0;          pEnc->flush_bframes = 0;
# Line 684  Line 816 
816           * comment style :-)           * comment style :-)
817           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
818    
819          SWAP(pEnc->current, pEnc->reference);  //$$    SWAP(pEnc->current, pEnc->reference);
820    
821          EMMS();          emms();
822    
823          if (pFrame->quant == 0)          if (pFrame->quant == 0)
824                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0);
# Line 707  Line 839 
839          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
840          pEnc->current->bcode = pEnc->mbParam.m_fcode;          pEnc->current->bcode = pEnc->mbParam.m_fcode;
841    
842          start_timer();  //$$$   start_timer();
843          if (image_input(&pEnc->current->image,  //$$$   if (image_input
844                          pEnc->mbParam.width,  //$$$           (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
845                          pEnc->mbParam.height,  //$$$            pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace))
846                          pEnc->mbParam.edged_width,  //$$$           return XVID_ERR_FORMAT;
847                          pFrame->image,  //$$$   stop_conv_timer();
848                          pFrame->colorspace))  
849                  return XVID_ERR_FORMAT;  #ifdef _DEBUG_PSNR
850          stop_conv_timer();          image_copy(&pEnc->sOriginal, &pEnc->current->image,
851                               pEnc->mbParam.edged_width, pEnc->mbParam.height);
 #ifdef _DEBUG  
         image_copy(&pEnc->sOriginal,  
                    &pEnc->current->image,  
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
852  #endif  #endif
853    
854          EMMS();          emms();
855    
856            if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
857                    image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5,
858                            "%i  if:%i  st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks);
859            }
860    
861    
862          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%          /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863           * Luminance masking           * Luminance masking
864           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
865    
866          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
         {  
867                  int *temp_dquants =                  int *temp_dquants =
868                          (int *) xvid_malloc(pEnc->mbParam.mb_width * \                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
869                                              pEnc->mbParam.mb_height * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                             sizeof(int),  
870                                              CACHE_LINE);                                              CACHE_LINE);
871    
872                  pEnc->current->quant =                  pEnc->current->quant =
873                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
874                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
875                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
876                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
877                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
878                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
879    
880                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
881    
882                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
883    
884                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
885                          {                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
886                                  MACROBLOCK *pMB =  
887                                          &pEnc->current->mbs[OFFSET(x,y)];                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                 pMB->dquant =  
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
888                          }                          }
889    
890                          #undef OFFSET                          #undef OFFSET
# Line 773  Line 899 
899           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */           * %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
900    
901    
902          if (pEnc->iFrameNum == 0 ||          if (pEnc->iFrameNum == 0 || pFrame->intra == 1 ||
903              pFrame->intra   == 1 ||                  (pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 &&
904                     pEnc->iFrameNum >= pEnc->iMaxKeyInterval)
905              (pFrame->intra < 0  &&                  || image_mad(&pEnc->reference->image, &pEnc->current->image,
906               pEnc->iMaxKeyInterval > 0 &&                                           pEnc->mbParam.edged_width, pEnc->mbParam.width,
907               pEnc->iFrameNum >= pEnc->iMaxKeyInterval) ||                                           pEnc->mbParam.height) > 30) {
   
             image_mad(&pEnc->reference->image,  
                       &pEnc->current->image,  
                       pEnc->mbParam.edged_width,  
                       pEnc->mbParam.width,  
                       pEnc->mbParam.height) > 30)  
         {  
908                  /*                  /*
909                   * This will be coded as an Intra Frame                   * This will be coded as an Intra Frame
910                   */                   */
911    
912                  /* ToDo : Remove dprintf calls */                  DPRINTF("*** IFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
913                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
914                    dprintf("--- IFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
915                  */  
916                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
917                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP");
918                    }
919    
920                  FrameCodeI(pEnc, &bs, &bits);                  FrameCodeI(pEnc, &bs, &bits);
921    
922                  pFrame->intra = 1;                  pFrame->intra = 1;
923                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
924    
925                    inc_frame_num(pEnc);
926    
927                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
928                            BitstreamPad(&bs);
929                            input_valid = 0;
930                            goto ipvop_loop;
931                    }
932    
933                  /*                  /*
934                   * NB : sequences like "IIBB" decode fine with msfdam but,                   * NB : sequences like "IIBB" decode fine with msfdam but,
935                   *      go screwy with divx 5.00                   *      go screwy with divx 5.00
936                   */                   */
937          }          } else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) {
         else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes)  
         {  
938                  /*                  /*
939                   * This will be coded as a Predicted Frame                   * This will be coded as a Predicted Frame
940                   */                   */
941    
942                  /* ToDo : Remove dprintf calls */                  DPRINTF("*** PFRAME bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
943                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
944                    dprintf("--- PFRAME ---");                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
945                  */  
946                    if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
947                            image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP");
948                    }
949    
950                  FrameCodeP(pEnc, &bs, &bits, 1, 0);                  FrameCodeP(pEnc, &bs, &bits, 1, 0);
951                  pFrame->intra = 0;                  pFrame->intra = 0;
952                  pEnc->flush_bframes = 1;                  pEnc->flush_bframes = 1;
953    
954                    inc_frame_num(pEnc);
955    
956                    if ((pEnc->global & XVID_GLOBAL_PACKED)) {
957                            BitstreamPad(&bs);
958                            input_valid = 0;
959                            goto ipvop_loop;
960          }          }
961          else  
962          {          } else {
963                  /*                  /*
964                   * This will be coded as a Bidirectional Frame                   * This will be coded as a Bidirectional Frame
965                   */                   */
966    
967                  /* ToDo : Remove dprintf calls */                  DPRINTF("*** BFRAME (store) bf: head=%i tail=%i   queue: head=%i tail=%i size=%i",
968                  /*                                  pEnc->bframenum_head, pEnc->bframenum_tail,
969                    dprintf("--- BFRAME (store) ---  head=%i tail=%i",                                  pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size);
                   pEnc->bframenum_head,  
                   pEnc->bframenum_tail);  
                 */  
970    
971                  if (pFrame->bquant < 1)                  if ((pEnc->global & XVID_GLOBAL_DEBUG)) {
972                  {                          image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP");
                         pEnc->current->quant =  
                                 ((pEnc->reference->quant+pEnc->current->quant)*\  
                                  pEnc->bquant_ratio) / 200;  
973                  }                  }
974                  else  
975                  {                  if (pFrame->bquant < 1) {
976                            pEnc->current->quant =
977                                    ((pEnc->reference->quant +
978                                      pEnc->current->quant) * pEnc->bquant_ratio) / 200;
979                    } else {
980                          pEnc->current->quant = pFrame->bquant;                          pEnc->current->quant = pFrame->bquant;
981                  }                  }
982    
# Line 852  Line 988 
988    
989                  pFrame->intra = 0;                  pFrame->intra = 0;
990                  pFrame->length = 0;                  pFrame->length = 0;
                 pFrame->input_consumed = 1;  
991    
992                  pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;                  inc_frame_num(pEnc);
                 if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
                 {  
                         pEnc->mbParam.m_seconds++;  
                         pEnc->mbParam.m_ticks = 0;  
                 }  
993    
994                  return XVID_ERR_OK;                  input_valid = 0;
995                    goto bvop_loop;
996          }          }
997    
998          BitstreamPad(&bs);          BitstreamPad(&bs);
999          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1000    
1001          if (pResult)          if (pResult) {
         {  
1002                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1003                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8);
1004                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 876  Line 1006 
1006                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1007          }          }
1008    
1009          EMMS();          emms();
1010    
1011  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1012          psnr = image_psnr(&pEnc->sOriginal,          psnr =
1013                            &pEnc->current->image,                  image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1014                            pEnc->mbParam.edged_width,                                     pEnc->mbParam.edged_width, pEnc->mbParam.width,
                           pEnc->mbParam.width,  
1015                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1016    
1017          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1018          DEBUG(temp);          DEBUG(temp);
1019  #endif  #endif
1020    
1021          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
1022          {                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1023                  RateControlUpdate(&pEnc->rate_control,                                                    pFrame->length, pFrame->intra);
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
1024          }          }
1025    
         pEnc->iFrameNum++;  
         pEnc->mbParam.m_ticks += pEnc->mbParam.fincr;  
         if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase)  
         {  
                 pEnc->mbParam.m_seconds++;  
                 pEnc->mbParam.m_ticks = 0;  
         }  
         pFrame->input_consumed = 1;  
1026    
1027          stop_global_timer();          stop_global_timer();
1028          write_timer();          write_timer();
1029    
1030          return XVID_ERR_OK;          return XVID_ERR_OK;
1031  }  }
1032  #else  
1033    #endif
1034    
1035    
1036    
1037  /*****************************************************************************  /*****************************************************************************
1038   * Frame encoder entry point for IP capable encoder   * "original" IP frame encoder entry point
1039     *
1040     * Returned values :
1041     *    - XVID_ERR_OK     - no errors
1042     *    - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong
1043     *                        format
1044   ****************************************************************************/   ****************************************************************************/
1045    
1046  int  int
1047  encoder_encode(Encoder * pEnc,  encoder_encode(Encoder * pEnc,
1048                 XVID_ENC_FRAME * pFrame,                 XVID_ENC_FRAME * pFrame,
# Line 924  Line 1052 
1052          Bitstream bs;          Bitstream bs;
1053          uint32_t bits;          uint32_t bits;
1054          uint16_t write_vol_header = 0;          uint16_t write_vol_header = 0;
1055  #ifdef _DEBUG  
1056    #ifdef _DEBUG_PSNR
1057          float psnr;          float psnr;
1058          uint8_t temp[128];          uint8_t temp[128];
1059  #endif  #endif
# Line 940  Line 1069 
1069    
1070          pEnc->current->global_flags = pFrame->general;          pEnc->current->global_flags = pFrame->general;
1071          pEnc->current->motion_flags = pFrame->motion;          pEnc->current->motion_flags = pFrame->motion;
1072    #ifdef BFRAMES
1073            pEnc->current->seconds = pEnc->mbParam.m_seconds;
1074            pEnc->current->ticks = pEnc->mbParam.m_ticks;
1075    #endif
1076          pEnc->mbParam.hint = &pFrame->hint;          pEnc->mbParam.hint = &pFrame->hint;
1077    
1078          start_timer();          start_timer();
1079          if (image_input(&pEnc->current->image,          if (image_input
1080                          pEnc->mbParam.width,                  (&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height,
1081                          pEnc->mbParam.height,                   pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0)
                         pEnc->mbParam.edged_width,  
                         pFrame->image,  
                         pFrame->colorspace) < 0)  
1082                  return XVID_ERR_FORMAT;                  return XVID_ERR_FORMAT;
1083          stop_conv_timer();          stop_conv_timer();
1084    
1085  #ifdef _DEBUG  #ifdef _DEBUG_PSNR
1086          image_copy(&pEnc->sOriginal,          image_copy(&pEnc->sOriginal, &pEnc->current->image,
1087                     &pEnc->current->image,                             pEnc->mbParam.edged_width, pEnc->mbParam.height);
                    pEnc->mbParam.edged_width,  
                    pEnc->mbParam.height);  
1088  #endif  #endif
1089    
1090          EMMS();          emms();
1091    
1092          BitstreamInit(&bs, pFrame->bitstream, 0);          BitstreamInit(&bs, pFrame->bitstream, 0);
1093    
1094          if (pFrame->quant == 0)          if (pFrame->quant == 0) {
         {  
1095                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);                  pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0);
1096          }          } else {
         else  
         {  
1097                  pEnc->current->quant = pFrame->quant;                  pEnc->current->quant = pFrame->quant;
1098          }          }
1099    
1100          if ((pEnc->current->global_flags & XVID_LUMIMASKING))          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1101          {                  int *temp_dquants =
1102                  int * temp_dquants = (int *)                          (int *) xvid_malloc(pEnc->mbParam.mb_width *
1103                          xvid_malloc(pEnc->mbParam.mb_width * \                                                                  pEnc->mbParam.mb_height * sizeof(int),
                                     pEnc->mbParam.mb_height * \  
                                     sizeof(int),  
1104                                      CACHE_LINE);                                      CACHE_LINE);
1105    
1106                  pEnc->current->quant =                  pEnc->current->quant =
1107                          adaptive_quantization(pEnc->current->image.y,                          adaptive_quantization(pEnc->current->image.y,
1108                                                pEnc->mbParam.edged_width,                                                                    pEnc->mbParam.edged_width, temp_dquants,
1109                                                temp_dquants,                                                                    pEnc->current->quant, pEnc->current->quant,
                                               pEnc->current->quant,  
                                               pEnc->current->quant,  
1110                                                2*pEnc->current->quant,                                                2*pEnc->current->quant,
1111                                                pEnc->mbParam.mb_width,                                                pEnc->mbParam.mb_width,
1112                                                pEnc->mbParam.mb_height);                                                pEnc->mbParam.mb_height);
1113    
1114                  for (y = 0; y < pEnc->mbParam.mb_height; y++)                  for (y = 0; y < pEnc->mbParam.mb_height; y++) {
                 {  
1115    
1116                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)                          #define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width)
1117    
1118                          for (x = 0; x < pEnc->mbParam.mb_width; x++)                          for (x = 0; x < pEnc->mbParam.mb_width; x++) {
                         {  
1119    
1120    
1121                                  MACROBLOCK *pMB =                                  MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)];
1122                                          &pEnc->current->mbs[OFFSET(x,y)];  
1123                                  pMB->dquant =                                  pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2];
                                         iDQtab[temp_dquants[OFFSET(x,y)] + 2];  
1124                          }                          }
1125    
1126                          #undef OFFSET                          #undef OFFSET
# Line 1011  Line 1129 
1129                  xvid_free(temp_dquants);                  xvid_free(temp_dquants);
1130          }          }
1131    
1132          if (pEnc->current->global_flags & XVID_H263QUANT)          if (pEnc->current->global_flags & XVID_H263QUANT) {
         {  
1133                  if(pEnc->mbParam.m_quant_type != H263_QUANT)                  if(pEnc->mbParam.m_quant_type != H263_QUANT)
1134                          write_vol_header = 1;                          write_vol_header = 1;
1135                  pEnc->mbParam.m_quant_type = H263_QUANT;                  pEnc->mbParam.m_quant_type = H263_QUANT;
1136          }          } else if (pEnc->current->global_flags & XVID_MPEGQUANT) {
         else if (pEnc->current->global_flags & XVID_MPEGQUANT)  
         {  
1137                  int matrix1_changed, matrix2_changed;                  int matrix1_changed, matrix2_changed;
1138    
1139                  matrix1_changed = matrix2_changed = 0;                  matrix1_changed = matrix2_changed = 0;
# Line 1030  Line 1145 
1145    
1146                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {                  if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) {
1147                          if(pFrame->quant_intra_matrix != NULL)                          if(pFrame->quant_intra_matrix != NULL)
1148                                  matrix1_changed =                                  matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix);
                                         set_intra_matrix(pFrame->quant_intra_matrix);  
1149                          if(pFrame->quant_inter_matrix != NULL)                          if(pFrame->quant_inter_matrix != NULL)
1150                                  matrix2_changed                                  matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix);
1151                                          = set_inter_matrix(pFrame->quant_inter_matrix);                  } else {
                 }  
                 else {  
1152                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());                          matrix1_changed = set_intra_matrix(get_default_intra_matrix());
1153                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());                          matrix2_changed = set_inter_matrix(get_default_inter_matrix());
1154                  }                  }
# Line 1044  Line 1156 
1156                          write_vol_header = matrix1_changed | matrix2_changed;                          write_vol_header = matrix1_changed | matrix2_changed;
1157          }          }
1158    
1159          if (pFrame->intra < 0)          if (pFrame->intra < 0) {
1160          {                  if ((pEnc->iFrameNum == 0)
1161                  if ((pEnc->iFrameNum == 0) ||                          || ((pEnc->iMaxKeyInterval > 0)
1162                                    && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) {
1163                      ((pEnc->iMaxKeyInterval > 0) &&                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1164                       (pEnc->iFrameNum >= pEnc->iMaxKeyInterval)))                  } else {
1165                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header);
1166                          pFrame->intra = FrameCodeI(pEnc,                  }
1167                                                     &bs,          } else {
1168                                                     &bits);                  if (pFrame->intra == 1) {
1169                  }                          pFrame->intra = FrameCodeI(pEnc, &bs, &bits);
1170                  else                  } else {
1171                  {                          pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header);
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    0,  
                                                    write_vol_header);  
                 }  
         }  
         else  
         {  
                 if (pFrame->intra == 1)  
                 {  
                         pFrame->intra = FrameCodeI(pEnc,  
                                                    &bs,  
                                                    &bits);  
                 }  
                 else  
                 {  
                         pFrame->intra = FrameCodeP(pEnc,  
                                                    &bs,  
                                                    &bits,  
                                                    1,  
                                                    write_vol_header);  
1172                  }                  }
1173    
1174          }          }
# Line 1088  Line 1178 
1178          BitstreamPad(&bs);          BitstreamPad(&bs);
1179          pFrame->length = BitstreamLength(&bs);          pFrame->length = BitstreamLength(&bs);
1180    
1181          if (pResult)          if (pResult) {
         {  
1182                  pResult->quant = pEnc->current->quant;                  pResult->quant = pEnc->current->quant;
1183                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);                  pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8);
1184                  pResult->kblks = pEnc->sStat.kblks;                  pResult->kblks = pEnc->sStat.kblks;
# Line 1097  Line 1186 
1186                  pResult->ublks = pEnc->sStat.ublks;                  pResult->ublks = pEnc->sStat.ublks;
1187          }          }
1188    
1189          EMMS();          emms();
   
         if (pFrame->quant == 0)  
         {  
                 RateControlUpdate(&pEnc->rate_control,  
                                   pEnc->current->quant,  
                                   pFrame->length,  
                                   pFrame->intra);  
         }  
1190    
1191  #ifdef _DEBUG          if (pFrame->quant == 0) {
1192          psnr = image_psnr(&pEnc->sOriginal,                  RateControlUpdate(&pEnc->rate_control, pEnc->current->quant,
1193                            &pEnc->current->image,                                                    pFrame->length, pFrame->intra);
1194                            pEnc->mbParam.edged_width,          }
1195                            pEnc->mbParam.width,  #ifdef _DEBUG_PSNR
1196            psnr =
1197                    image_psnr(&pEnc->sOriginal, &pEnc->current->image,
1198                                       pEnc->mbParam.edged_width, pEnc->mbParam.width,
1199                            pEnc->mbParam.height);                            pEnc->mbParam.height);
1200    
1201          snprintf(temp, 127, "PSNR: %f\n", psnr);          snprintf(temp, 127, "PSNR: %f\n", psnr);
1202          DEBUG(temp);          DEBUG(temp);
1203  #endif  #endif
1204    
1205    #ifdef BFRAMES
1206            inc_frame_num(pEnc);
1207    #else
1208          pEnc->iFrameNum++;          pEnc->iFrameNum++;
1209    #endif
1210    
1211    
1212          stop_global_timer();          stop_global_timer();
1213          write_timer();          write_timer();
1214    
1215          return XVID_ERR_OK;          return XVID_ERR_OK;
1216  }  }
 #endif  
1217    
1218    
1219  static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) {  static __inline void
1220    CodeIntraMB(Encoder * pEnc,
1221                            MACROBLOCK * pMB)
1222    {
1223    
1224          pMB->mode = MODE_INTRA;          pMB->mode = MODE_INTRA;
1225    
# Line 1139  Line 1230 
1230          pMB->sad16 = 0;          pMB->sad16 = 0;
1231    
1232          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {          if ((pEnc->current->global_flags & XVID_LUMIMASKING)) {
1233                  if(pMB->dquant != NO_CHANGE)                  if (pMB->dquant != NO_CHANGE) {
                 {  
1234                          pMB->mode = MODE_INTRA_Q;                          pMB->mode = MODE_INTRA_Q;
1235                          pEnc->current->quant += DQtab[pMB->dquant];                          pEnc->current->quant += DQtab[pMB->dquant];
1236    
1237                          if (pEnc->current->quant > 31) pEnc->current->quant = 31;                          if (pEnc->current->quant > 31)
1238                          if (pEnc->current->quant < 1) pEnc->current->quant = 1;                                  pEnc->current->quant = 31;
1239                            if (pEnc->current->quant < 1)
1240                                    pEnc->current->quant = 1;
1241                  }                  }
1242          }          }
1243    
# Line 1156  Line 1248 
1248  #define FCODEBITS       3  #define FCODEBITS       3
1249  #define MODEBITS        5  #define MODEBITS        5
1250    
1251  void HintedMESet(Encoder * pEnc, int * intra)  void
1252    HintedMESet(Encoder * pEnc,
1253                            int *intra)
1254  {  {
1255          HINTINFO * hint;          HINTINFO * hint;
1256          Bitstream bs;          Bitstream bs;
# Line 1165  Line 1259 
1259    
1260          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1261    
1262          if (hint->rawhints)          if (hint->rawhints) {
         {  
1263                  *intra = hint->mvhint.intra;                  *intra = hint->mvhint.intra;
1264          }          } else {
         else  
         {  
1265                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);                  BitstreamInit(&bs, hint->hintstream, hint->hintlength);
1266                  *intra = BitstreamGetBit(&bs);                  *intra = BitstreamGetBit(&bs);
1267          }          }
1268    
1269          if (*intra)          if (*intra) {
         {  
1270                  return;                  return;
1271          }          }
1272    
1273          pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS);          pEnc->current->fcode =
1274                    (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs,
1275                                                                                                                                     FCODEBITS);
1276    
1277          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1278          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1279    
1280          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1281          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1282                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1283                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1284                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1285                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1286                          VECTOR pred[4];                          VECTOR pred[4];
1287                          VECTOR tmp;                          VECTOR tmp;
1288                          int32_t dummy[4];                          int32_t dummy[4];
1289                          int vec;                          int vec;
1290    
1291                          pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS);                          pMB->mode =
1292                                    (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs,
1293                                                                                                                                      MODEBITS);
1294    
1295                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode;
1296                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;                          pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode;
1297    
1298                          if (pMB->mode == MODE_INTER)                          if (pMB->mode == MODE_INTER) {
1299                          {                                  tmp.x =
1300                                  tmp.x  = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length);                                          (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs,
1301                                  tmp.y  = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length);                                                                                                                                                    length);
1302                                    tmp.y =
1303                                            (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs,
1304                                                                                                                                                      length);
1305                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;                                  tmp.x -= (tmp.x >= high) ? high*2 : 0;
1306                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;                                  tmp.y -= (tmp.y >= high) ? high*2 : 0;
1307    
1308                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy);                                  get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width,
1309                                                            0, pred, dummy);
1310    
1311                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1312                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1313                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1314                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x;
1315                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y;
1316                                  }                                  }
1317                          }                          } else if (pMB->mode == MODE_INTER4V) {
1318                          else if (pMB->mode == MODE_INTER4V)                                  for (vec = 0; vec < 4; ++vec) {
1319                          {                                          tmp.x =
1320                                  for (vec=0 ; vec<4 ; ++vec)                                                  (hint->rawhints) ? bhint->mvs[vec].
1321                                  {                                                  x : BitstreamGetBits(&bs, length);
1322                                          tmp.x  = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length);                                          tmp.y =
1323                                          tmp.y  = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length);                                                  (hint->rawhints) ? bhint->mvs[vec].
1324                                                    y : BitstreamGetBits(&bs, length);
1325                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;                                          tmp.x -= (tmp.x >= high) ? high*2 : 0;
1326                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;                                          tmp.y -= (tmp.y >= high) ? high*2 : 0;
1327    
1328                                          get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy);                                          get_pmvdata(pEnc->current->mbs, x, y,
1329                                                                    pEnc->mbParam.mb_width, vec, pred, dummy);
1330    
1331                                          pMB->mvs[vec].x  = tmp.x;                                          pMB->mvs[vec].x  = tmp.x;
1332                                          pMB->mvs[vec].y  = tmp.y;                                          pMB->mvs[vec].y  = tmp.y;
1333                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;                                          pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x;
1334                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;                                          pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y;
1335                                  }                                  }
1336                          }                          } else                          // intra / stuffing / not_coded
                         else    // intra / stuffing / not_coded  
                         {  
                                 for (vec=0 ; vec<4 ; ++vec)  
1337                                  {                                  {
1338                                    for (vec = 0; vec < 4; ++vec) {
1339                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;                                          pMB->mvs[vec].x  = pMB->mvs[vec].y  = 0;
1340                                  }                                  }
1341                          }                          }
1342    
1343                          if (pMB->mode == MODE_INTER4V &&                          if (pMB->mode == MODE_INTER4V &&
1344                                  (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE)                                  (pEnc->current->global_flags & XVID_LUMIMASKING)
1345                          {                                  && pMB->dquant != NO_CHANGE) {
1346                                  pMB->mode = MODE_INTRA;                                  pMB->mode = MODE_INTRA;
1347    
1348                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1349                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;                                          pMB->mvs[vec].x = pMB->mvs[vec].y = 0;
1350                                  }                                  }
1351                          }                          }
# Line 1258  Line 1354 
1354  }  }
1355    
1356    
1357  void HintedMEGet(Encoder * pEnc, int intra)  void
1358    HintedMEGet(Encoder * pEnc,
1359                            int intra)
1360  {  {
1361          HINTINFO * hint;          HINTINFO * hint;
1362          Bitstream bs;          Bitstream bs;
# Line 1267  Line 1365 
1365    
1366          hint = pEnc->mbParam.hint;          hint = pEnc->mbParam.hint;
1367    
1368          if (hint->rawhints)          if (hint->rawhints) {
         {  
1369                  hint->mvhint.intra = intra;                  hint->mvhint.intra = intra;
1370          }          } else {
         else  
         {  
1371                  BitstreamInit(&bs, hint->hintstream, 0);                  BitstreamInit(&bs, hint->hintstream, 0);
1372                  BitstreamPutBit(&bs, intra);                  BitstreamPutBit(&bs, intra);
1373          }          }
1374    
1375          if (intra)          if (intra) {
1376          {                  if (!hint->rawhints) {
                 if (!hint->rawhints)  
                 {  
1377                          BitstreamPad(&bs);                          BitstreamPad(&bs);
1378                          hint->hintlength = BitstreamLength(&bs);                          hint->hintlength = BitstreamLength(&bs);
1379                  }                  }
# Line 1290  Line 1383 
1383          length  = pEnc->current->fcode + 5;          length  = pEnc->current->fcode + 5;
1384          high    = 1 << (length - 1);          high    = 1 << (length - 1);
1385    
1386          if (hint->rawhints)          if (hint->rawhints) {
         {  
1387                  hint->mvhint.fcode = pEnc->current->fcode;                  hint->mvhint.fcode = pEnc->current->fcode;
1388          }          } else {
         else  
         {  
1389                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);                  BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS);
1390          }          }
1391    
1392          for (y=0 ; y<pEnc->mbParam.mb_height ; ++y)          for (y = 0; y < pEnc->mbParam.mb_height; ++y) {
1393          {                  for (x = 0; x < pEnc->mbParam.mb_width; ++x) {
1394                  for (x=0 ; x<pEnc->mbParam.mb_width ; ++x)                          MACROBLOCK *pMB =
1395                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1396                          MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MVBLOCKHINT *bhint =
1397                          MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];                                  &hint->mvhint.block[x + y * pEnc->mbParam.mb_width];
1398                          VECTOR tmp;                          VECTOR tmp;
1399    
1400                          if (hint->rawhints)                          if (hint->rawhints) {
                         {  
1401                                  bhint->mode = pMB->mode;                                  bhint->mode = pMB->mode;
1402                          }                          } else {
                         else  
                         {  
1403                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);                                  BitstreamPutBits(&bs, pMB->mode, MODEBITS);
1404                          }                          }
1405    
1406                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)                          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                         {  
1407                                  tmp.x  = pMB->mvs[0].x;                                  tmp.x  = pMB->mvs[0].x;
1408                                  tmp.y  = pMB->mvs[0].y;                                  tmp.y  = pMB->mvs[0].y;
1409                                  tmp.x += (tmp.x < 0) ? high*2 : 0;                                  tmp.x += (tmp.x < 0) ? high*2 : 0;
1410                                  tmp.y += (tmp.y < 0) ? high*2 : 0;                                  tmp.y += (tmp.y < 0) ? high*2 : 0;
1411    
1412                                  if (hint->rawhints)                                  if (hint->rawhints) {
                                 {  
1413                                          bhint->mvs[0].x = tmp.x;                                          bhint->mvs[0].x = tmp.x;
1414                                          bhint->mvs[0].y = tmp.y;                                          bhint->mvs[0].y = tmp.y;
1415                                  }                                  } else {
                                 else  
                                 {  
1416                                          BitstreamPutBits(&bs, tmp.x, length);                                          BitstreamPutBits(&bs, tmp.x, length);
1417                                          BitstreamPutBits(&bs, tmp.y, length);                                          BitstreamPutBits(&bs, tmp.y, length);
1418                                  }                                  }
1419                          }                          } else if (pMB->mode == MODE_INTER4V) {
                         else if (pMB->mode == MODE_INTER4V)  
                         {  
1420                                  int vec;                                  int vec;
1421    
1422                                  for (vec=0 ; vec<4 ; ++vec)                                  for (vec = 0; vec < 4; ++vec) {
                                 {  
1423                                          tmp.x  = pMB->mvs[vec].x;                                          tmp.x  = pMB->mvs[vec].x;
1424                                          tmp.y  = pMB->mvs[vec].y;                                          tmp.y  = pMB->mvs[vec].y;
1425                                          tmp.x += (tmp.x < 0) ? high*2 : 0;                                          tmp.x += (tmp.x < 0) ? high*2 : 0;
1426                                          tmp.y += (tmp.y < 0) ? high*2 : 0;                                          tmp.y += (tmp.y < 0) ? high*2 : 0;
1427    
1428                                          if (hint->rawhints)                                          if (hint->rawhints) {
                                         {  
1429                                                  bhint->mvs[vec].x = tmp.x;                                                  bhint->mvs[vec].x = tmp.x;
1430                                                  bhint->mvs[vec].y = tmp.y;                                                  bhint->mvs[vec].y = tmp.y;
1431                                          }                                          } else {
                                         else  
                                         {  
1432                                                  BitstreamPutBits(&bs, tmp.x, length);                                                  BitstreamPutBits(&bs, tmp.x, length);
1433                                                  BitstreamPutBits(&bs, tmp.y, length);                                                  BitstreamPutBits(&bs, tmp.y, length);
1434                                          }                                          }
# Line 1360  Line 1437 
1437                  }                  }
1438          }          }
1439    
1440          if (!hint->rawhints)          if (!hint->rawhints) {
         {  
1441                  BitstreamPad(&bs);                  BitstreamPad(&bs);
1442                  hint->hintlength = BitstreamLength(&bs);                  hint->hintlength = BitstreamLength(&bs);
1443          }          }
1444  }  }
1445    
1446    
1447  static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits)  static int
1448    FrameCodeI(Encoder * pEnc,
1449                       Bitstream * bs,
1450                       uint32_t * pBits)
1451  {  {
1452    
1453          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE);
# Line 1382  Line 1461 
1461          pEnc->current->coding_type = I_VOP;          pEnc->current->coding_type = I_VOP;
1462    
1463          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1464          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);  #ifdef BFRAMES
1465    #define DIVX501B481P "DivX501b481p"
1466            if ((pEnc->global & XVID_GLOBAL_PACKED)) {
1467                    BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P));
1468            }
1469    #endif
1470            BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1471    
1472          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1473    
# Line 1391  Line 1476 
1476          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1477    
1478          for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++)
1479                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1480                  {                          MACROBLOCK *pMB =
1481                          MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1482    
1483                          CodeIntraMB(pEnc, pMB);                          CodeIntraMB(pEnc, pMB);
1484    
1485                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                          MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1486                                                              dct_codes, qcoeff);
1487    
1488                          start_timer();                          start_timer();
1489                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
# Line 1416  Line 1502 
1502          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1503          pEnc->mbParam.m_fcode = 2;          pEnc->mbParam.m_fcode = 2;
1504    
1505          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1506                  HintedMEGet(pEnc, 1);                  HintedMEGet(pEnc, 1);
1507          }          }
1508    
# Line 1427  Line 1512 
1512    
1513  #define INTRA_THRESHOLD 0.5  #define INTRA_THRESHOLD 0.5
1514    
1515  static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header)  static int
1516    FrameCodeP(Encoder * pEnc,
1517                       Bitstream * bs,
1518                       uint32_t * pBits,
1519                       bool force_inter,
1520                       bool vol_header)
1521  {  {
1522          float fSigma;          float fSigma;
1523    
# Line 1443  Line 1533 
1533          IMAGE *pRef = &pEnc->reference->image;          IMAGE *pRef = &pEnc->reference->image;
1534    
1535          start_timer();          start_timer();
1536          image_setedges(pRef,          image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1537                         pEnc->mbParam.edged_width,                                     pEnc->mbParam.width, pEnc->mbParam.height,
                        pEnc->mbParam.edged_height,  
                        pEnc->mbParam.width,  
                        pEnc->mbParam.height,  
1538                         pEnc->current->global_flags & XVID_INTERLACING);                         pEnc->current->global_flags & XVID_INTERLACING);
1539          stop_edges_timer();          stop_edges_timer();
1540    
# Line 1456  Line 1543 
1543          pEnc->current->fcode = pEnc->mbParam.m_fcode;          pEnc->current->fcode = pEnc->mbParam.m_fcode;
1544    
1545          if (!force_inter)          if (!force_inter)
1546                  iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD);                  iLimit =
1547                            (int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height *
1548                                       INTRA_THRESHOLD);
1549          else          else
1550                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;                  iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1;
1551    
1552          if ((pEnc->current->global_flags & XVID_HALFPEL)) {          if ((pEnc->current->global_flags & XVID_HALFPEL)) {
1553                  start_timer();                  start_timer();
1554                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                  image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV,
1555                                    pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,                                                    &pEnc->vInterHV, pEnc->mbParam.edged_width,
1556                                                      pEnc->mbParam.edged_height,
1557                                    pEnc->current->rounding_type);                                    pEnc->current->rounding_type);
1558                  stop_inter_timer();                  stop_inter_timer();
1559          }          }
1560    
1561          start_timer();          start_timer();
1562          if (pEnc->current->global_flags & XVID_HINTEDME_SET)          if (pEnc->current->global_flags & XVID_HINTEDME_SET) {
         {  
1563                  HintedMESet(pEnc, &bIntra);                  HintedMESet(pEnc, &bIntra);
1564          }          } else {
1565          else                  bIntra =
1566          {                          MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference,
1567                  bIntra = MotionEstimation(                                                           &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
                         &pEnc->mbParam,  
                         pEnc->current,  
                         pEnc->reference,  
                         &pEnc->vInterH,  
                         &pEnc->vInterV,  
                         &pEnc->vInterHV,  
1568                          iLimit);                          iLimit);
1569          }          }
1570          stop_motion_timer();          stop_motion_timer();
1571    
1572          if (bIntra == 1)          if (bIntra == 1) {
         {  
1573                  return FrameCodeI(pEnc, bs, pBits);                  return FrameCodeI(pEnc, bs, pBits);
1574          }          }
1575    
# Line 1496  Line 1578 
1578          if(vol_header)          if(vol_header)
1579                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);                  BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current);
1580    
1581          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1);
1582    
1583          *pBits = BitstreamPos(bs);          *pBits = BitstreamPos(bs);
1584    
# Line 1505  Line 1587 
1587          pEnc->sStat.iMvCount = 0;          pEnc->sStat.iMvCount = 0;
1588          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1589    
1590          for(y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
1591          {                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1592                  for(x = 0; x < pEnc->mbParam.mb_width; x++)                          MACROBLOCK *pMB =
1593                  {                                  &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
                         MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];  
1594    
1595                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);                          bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q);
1596    
1597                          if (!bIntra)                          if (!bIntra) {
                         {  
1598                                  start_timer();                                  start_timer();
1599                                  MBMotionCompensation(pMB,                                  MBMotionCompensation(pMB, x, y, &pEnc->reference->image,
1600                                                       x, y,                                                                           &pEnc->vInterH, &pEnc->vInterV,
1601                                                       &pEnc->reference->image,                                                                           &pEnc->vInterHV, &pEnc->current->image,
1602                                                       &pEnc->vInterH,                                                                           dct_codes, pEnc->mbParam.width,
                                                      &pEnc->vInterV,  
                                                      &pEnc->vInterHV,  
                                                      &pEnc->current->image,  
                                                      dct_codes,  
                                                      pEnc->mbParam.width,  
1603                                                       pEnc->mbParam.height,                                                       pEnc->mbParam.height,
1604                                                       pEnc->mbParam.edged_width,                                                       pEnc->mbParam.edged_width,
1605                                                       pEnc->current->rounding_type);                                                       pEnc->current->rounding_type);
# Line 1534  Line 1609 
1609                                          if(pMB->dquant != NO_CHANGE) {                                          if(pMB->dquant != NO_CHANGE) {
1610                                                  pMB->mode = MODE_INTER_Q;                                                  pMB->mode = MODE_INTER_Q;
1611                                                  pEnc->current->quant += DQtab[pMB->dquant];                                                  pEnc->current->quant += DQtab[pMB->dquant];
1612                                                  if (pEnc->current->quant > 31) pEnc->current->quant = 31;                                                  if (pEnc->current->quant > 31)
1613                                                  else if(pEnc->current->quant < 1) pEnc->current->quant = 1;                                                          pEnc->current->quant = 31;
1614                                                    else if (pEnc->current->quant < 1)
1615                                                            pEnc->current->quant = 1;
1616                                          }                                          }
1617                                  }                                  }
1618                                  pMB->quant = pEnc->current->quant;                                  pMB->quant = pEnc->current->quant;
1619    
1620                                  pMB->field_pred = 0;                                  pMB->field_pred = 0;
1621    
1622                                  pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  pMB->cbp =
1623                          }                                          MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y,
1624                          else                                                                            dct_codes, qcoeff);
1625                          {                          } else {
1626                                  CodeIntraMB(pEnc, pMB);                                  CodeIntraMB(pEnc, pMB);
1627                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff);                                  MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y,
1628                                                                      dct_codes, qcoeff);
1629                          }                          }
1630    
1631                          start_timer();                          start_timer();
1632                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);                          MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff);
1633                          stop_prediction_timer();                          stop_prediction_timer();
1634    
1635                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q)                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) {
                         {  
1636                                  pEnc->sStat.kblks++;                                  pEnc->sStat.kblks++;
1637                          }                          } else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y ||
1638                          else if (pMB->cbp ||                                             pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x ||
1639                                   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)  
                         {  
1640                                  pEnc->sStat.mblks++;                                  pEnc->sStat.mblks++;
1641                          }                          } else {
                         else  
                         {  
1642                                  pEnc->sStat.ublks++;                                  pEnc->sStat.ublks++;
1643                          }                          }
1644    
# Line 1579  Line 1650 
1650    
1651          emms();          emms();
1652    
1653          if (pEnc->current->global_flags & XVID_HINTEDME_GET)          if (pEnc->current->global_flags & XVID_HINTEDME_GET) {
         {  
1654                  HintedMEGet(pEnc, 0);                  HintedMEGet(pEnc, 0);
1655          }          }
1656    
# Line 1596  Line 1666 
1666          {          {
1667                  pEnc->mbParam.m_fcode++;                  pEnc->mbParam.m_fcode++;
1668                  iSearchRange *= 2;                  iSearchRange *= 2;
1669          }          } else if ((fSigma < iSearchRange / 6)
         else if ((fSigma < iSearchRange / 6)  
1670                   && (pEnc->sStat.fMvPrevSigma >= 0)                   && (pEnc->sStat.fMvPrevSigma >= 0)
1671                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)                   && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6)
1672                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16                   && (pEnc->mbParam.m_fcode >= 2))       // minimum search range 16
# Line 1615  Line 1684 
1684    
1685    
1686  #ifdef BFRAMES  #ifdef BFRAMES
1687  static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits)  static void
1688    FrameCodeB(Encoder * pEnc,
1689                       FRAMEINFO * frame,
1690                       Bitstream * bs,
1691                       uint32_t * pBits)
1692  {  {
1693      int16_t dct_codes[6*64];      int16_t dct_codes[6*64];
1694      int16_t qcoeff[6*64];      int16_t qcoeff[6*64];
# Line 1627  Line 1700 
1700          IMAGE *b_ref = &pEnc->current->image;          IMAGE *b_ref = &pEnc->current->image;
1701    
1702          // forward          // forward
1703          image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          image_setedges(f_ref, pEnc->mbParam.edged_width,
1704                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1705                                       pEnc->mbParam.height,
1706                                       frame->global_flags & XVID_INTERLACING);
1707          start_timer();          start_timer();
1708          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,          image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1709                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1710                                              0);
1711          stop_inter_timer();          stop_inter_timer();
1712    
1713          // backward          // backward
1714          image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING);          image_setedges(b_ref, pEnc->mbParam.edged_width,
1715                                       pEnc->mbParam.edged_height, pEnc->mbParam.width,
1716                                       pEnc->mbParam.height,
1717                                       frame->global_flags & XVID_INTERLACING);
1718      start_timer();      start_timer();
1719          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,          image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,
1720                  pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0);                                            pEnc->mbParam.edged_width, pEnc->mbParam.edged_height,
1721                                              0);
1722          stop_inter_timer();          stop_inter_timer();
1723    
1724          start_timer();          start_timer();
1725          MotionEstimationBVOP(&pEnc->mbParam, frame,          MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref,
1726                  pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                   &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,
1727                  pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV);                                                   pEnc->current->mbs, b_ref, &pEnc->vInterH,
1728                                                     &pEnc->vInterV, &pEnc->vInterHV);
1729    
1730    
1731          stop_motion_timer();          stop_motion_timer();
# Line 1654  Line 1736 
1736          }*/          }*/
1737    
1738      frame->coding_type = B_VOP;      frame->coding_type = B_VOP;
1739      BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame);          BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1);
1740    
1741      *pBits = BitstreamPos(bs);      *pBits = BitstreamPos(bs);
1742    
# Line 1664  Line 1746 
1746          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;          pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0;
1747    
1748    
1749      for (y = 0; y < pEnc->mbParam.mb_height; y++)          for (y = 0; y < pEnc->mbParam.mb_height; y++) {
         {  
1750                  // reset prediction                  // reset prediction
1751    
1752                  forward.x = 0;                  forward.x = 0;
# Line 1673  Line 1754 
1754                  backward.x = 0;                  backward.x = 0;
1755                  backward.y = 0;                  backward.y = 0;
1756    
1757                  for (x = 0; x < pEnc->mbParam.mb_width; x++)                  for (x = 0; x < pEnc->mbParam.mb_width; x++) {
1758                  {                          MACROBLOCK *f_mb =
1759                          MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];                                  &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width];
1760                          MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK *b_mb =
1761                                    &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width];
1762                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];                          MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width];
1763    
1764                          // decoder ignores mb when refence block is INTER(0,0), CBP=0                          // decoder ignores mb when refence block is INTER(0,0), CBP=0
1765                          if (mb->mode == MODE_NOT_CODED)                          if (mb->mode == MODE_NOT_CODED) {
                         {  
1766                                  mb->mvs[0].x = 0;                                  mb->mvs[0].x = 0;
1767                                  mb->mvs[0].y = 0;                                  mb->mvs[0].y = 0;
1768                                  continue;                                  continue;
1769                          }                          }
1770    
1771                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,                          MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image,
1772                                          f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv,                                                                           f_ref, &pEnc->f_refh, &pEnc->f_refv,
1773                                          b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV,                                                                           &pEnc->f_refhv, b_ref, &pEnc->vInterH,
1774                                                                             &pEnc->vInterV, &pEnc->vInterHV,
1775                                          dct_codes);                                          dct_codes);
1776    
1777                          mb->quant = frame->quant;                          mb->quant = frame->quant;
1778                          mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff);                          mb->cbp =
1779                                    MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes,
1780                                                                      qcoeff);
1781                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);                          //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant);
1782    
1783    
1784                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) &&                          if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT)
1785                                  mb->cbp == 0 &&                                  && mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) {
                                 mb->mvs[0].x == 0 &&  
                                 mb->mvs[0].y == 0)  
                         {  
1786                                  mb->mode = 5;  // skipped                                  mb->mode = 5;  // skipped
1787                          }                          }
1788    
1789                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) {
                         {  
1790                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;                                  mb->pmvs[0].x = mb->mvs[0].x - forward.x;
1791                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;                                  mb->pmvs[0].y = mb->mvs[0].y - forward.y;
1792                                  forward.x = mb->mvs[0].x;                                  forward.x = mb->mvs[0].x;
1793                                  forward.y = mb->mvs[0].y;                                  forward.y = mb->mvs[0].y;
1794                          }                          }
1795    
1796                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD)                          if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) {
                         {  
1797                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;                                  mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x;
1798                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;                                  mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y;
1799                                  backward.x = mb->b_mvs[0].x;                                  backward.x = mb->b_mvs[0].x;
1800                                  backward.y = mb->b_mvs[0].y;                                  backward.y = mb->b_mvs[0].y;
1801                          }                          }
1802    //                      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);
 //                      printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i  %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y);  
1803    
1804                          start_timer();                          start_timer();
1805                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat);                          MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs,
1806                                                     &pEnc->sStat);
1807                          stop_coding_timer();                          stop_coding_timer();
1808                  }                  }
1809          }          }

Legend:
Removed from v.194  
changed lines
  Added in v.234

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