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

Diff of /xvidcore/src/decoder.c

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

revision 1.49.2.35, Wed Mar 3 13:18:08 2004 UTC revision 1.49.4.1, Sat May 3 23:23:38 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Decoder Module -   *  - Decoder Module -
5   *   *
6   *  Copyright(C) 2002      MinChen <chenm001@163.com>   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
  *               2002-2003 Peter Ross <pross@xvid.org>  
7   *   *
8   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
9   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 40  Line 39 
39  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
40  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
41    
42  #include "quant/quant.h"  #include "quant/quant_h263.h"
43  #include "quant/quant_matrix.h"  #include "quant/quant_mpeg4.h"
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
# Line 54  Line 53 
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55  #include "motion/motion.h"  #include "motion/motion.h"
 #include "motion/gmc.h"  
56    
57  #include "image/image.h"  #include "image/image.h"
58  #include "image/colorspace.h"  #include "image/colorspace.h"
 #include "image/postprocessing.h"  
59  #include "utils/mem_align.h"  #include "utils/mem_align.h"
60    #include "image/postprocessing.h"
61    
62  static int  int
63  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
64  {  {
65          /* free existing */          /* free existing */
# Line 77  Line 75 
75                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
76          if (dec->mbs)          if (dec->mbs)
77                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
         if (dec->qscale)  
                 xvid_free(dec->qscale);  
78    
79          /* realloc */          /* realloc */
80          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
# Line 163  Line 159 
159    
160          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
161    
162          /* nothing happens if that fails */          return XVID_ERR_OK;
         dec->qscale =  
                 xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);  
   
         if (dec->qscale)  
                 memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);  
   
         return 0;  
163  }  }
164    
165    
166  int  int
167  decoder_create(xvid_dec_create_t * create)  decoder_create(XVID_DEC_PARAM * param)
168  {  {
169          DECODER *dec;          DECODER *dec;
170    
         if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */  
                 return XVID_ERR_VERSION;  
   
171          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
172          if (dec == NULL) {          if (dec == NULL) {
173                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
174          }          }
   
175          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
176    
177          dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);          param->handle = dec;
         if (dec->mpeg_quant_matrices == NULL) {  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         create->handle = dec;  
178    
179          dec->width = create->width;          dec->width = param->width;
180          dec->height = create->height;          dec->height = param->height;
181    
182          image_null(&dec->cur);          image_null(&dec->cur);
183          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
# Line 209  Line 188 
188          /* image based GMC */          /* image based GMC */
189          image_null(&dec->gmc);          image_null(&dec->gmc);
190    
191    
192          dec->mbs = NULL;          dec->mbs = NULL;
193          dec->last_mbs = NULL;          dec->last_mbs = NULL;
         dec->qscale = NULL;  
194    
195          init_timer();          init_timer();
         init_postproc(&dec->postproc);  
         init_mpeg_matrix(dec->mpeg_quant_matrices);  
196    
197          /* For B-frame support (used to save reference frame's time */          /* For B-frame support (used to save reference frame's time */
198          dec->frames = 0;          dec->frames = 0;
# Line 228  Line 205 
205          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
206                  return decoder_resize(dec);                  return decoder_resize(dec);
207          else          else
208                  return 0;                  return XVID_ERR_OK;
209  }  }
210    
211    
# Line 237  Line 214 
214  {  {
215          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
216          xvid_free(dec->mbs);          xvid_free(dec->mbs);
         xvid_free(dec->qscale);  
217    
218          /* image based GMC */          /* image based GMC */
219          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
# Line 247  Line 223 
223          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
225          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
         xvid_free(dec->mpeg_quant_matrices);  
226          xvid_free(dec);          xvid_free(dec);
227    
228          write_timer();          write_timer();
229          return 0;          return XVID_ERR_OK;
230  }  }
231    
232    
233    
234  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
235          -1, -2, 1, 2          -1, -2, 1, 2
236  };  };
237    
238    
239    
240    
241  /* decode an intra macroblock */  /* decode an intra macroblock */
242  static void  void
243  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
244                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
245                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 322  Line 302 
302                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
303                          start_coeff = 1;                          start_coeff = 1;
304    
305                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
306                  } else {                  } else {
307                          start_coeff = 0;                          start_coeff = 0;
308                  }                  }
# Line 343  Line 323 
323    
324                  start_timer();                  start_timer();
325                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
326                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
327                  } else {                  } else {
328                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
329                  }                  }
330                  stop_iquant_timer();                  stop_iquant_timer();
331    
# Line 382  Line 362 
362          stop_transfer_timer();          stop_transfer_timer();
363  }  }
364    
 static void  
 decoder_mb_decode(DECODER * dec,  
                                 const uint32_t cbp,  
                                 Bitstream * bs,  
                                 uint8_t * pY_Cur,  
                                 uint8_t * pU_Cur,  
                                 uint8_t * pV_Cur,  
                                 const int reduced_resolution,  
                                 const MACROBLOCK * pMB)  
 {  
         DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         int stride = dec->edged_width;  
         int next_block = stride * (reduced_resolution ? 16 : 8);  
         const int stride2 = stride/2;  
         int i;  
         const uint32_t iQuant = pMB->quant;  
         const int direction = dec->alternate_vertical_scan ? 2 : 0;  
         const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;  
   
         for (i = 0; i < 6; i++) {  
   
                 if (cbp & (1 << (5 - i))) {     /* coded */  
   
                         memset(block, 0, 64 * sizeof(int16_t)); /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, block, direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices);  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
365    
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
366    
         start_timer();  
         if (reduced_resolution) {  
                 if (cbp & 32)  
                         add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         } else {  
                 if (cbp & 32)  
                         transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         }  
         stop_transfer_timer();  
 }  
367    
368  /* decode an inter macroblock */  /* decode an inter macroblock */
369  static void  void
370  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
371                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
372                                  const uint32_t x_pos,                                  const uint32_t x_pos,
373                                  const uint32_t y_pos,                                  const uint32_t y_pos,
374                                    const uint32_t fcode,
375                                  const uint32_t cbp,                                  const uint32_t cbp,
376                                  Bitstream * bs,                                  Bitstream * bs,
377                                    const uint32_t quant,
378                                  const uint32_t rounding,                                  const uint32_t rounding,
379                                  const int reduced_resolution,                                  const int reduced_resolution)
                                 const int ref)  
380  {  {
381    
382            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
383            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
384    
385          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
386          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
387            uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
388          uint32_t i;          uint32_t i;
389            uint32_t iQuant = pMB->quant;
390          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
391    
392          int uv_dx, uv_dy;          int uv_dx, uv_dy;
# Line 496  Line 408 
408                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
409          }          }
410    
411          start_timer();          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
412    
413          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */                  uv_dx = mv[0].x / (1 + dec->quarterpel);
414                    uv_dy = mv[0].y / (1 + dec->quarterpel);
415    
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
                 if (dec->quarterpel) {  
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
416                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
417                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
418    
419                    start_timer();
420                  if (reduced_resolution)                  if (reduced_resolution)
421                    {
422                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
423                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
424                  else if (dec->quarterpel)                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
425                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,                                                                    uv_dx, uv_dy, stride2, rounding);
426                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
427                                                                      uv_dx, uv_dy, stride2, rounding);
428    
429                    }
430                    else
431                    {
432                            if(dec->quarterpel) {
433                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
434                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
435                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
436                  else                          }
437                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,                          else {
438                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
439                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
440                            }
441    
442                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
443                                                                      uv_dx, uv_dy, stride2, rounding);
444                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
445                                                                      uv_dx, uv_dy, stride2, rounding);
446                    }
447                    stop_comp_timer();
448    
449          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
450                    int sum;
451    
452                  if(dec->quarterpel) {                  if(dec->quarterpel)
453                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
454                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);                  else
455                  } else {                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
                         uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
                         uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;  
                 }  
456    
457                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
                 uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];  
458    
459                  if (reduced_resolution) {                  if(dec->quarterpel)
460                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
461                    else
462                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
463    
464                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
465    
466                    start_timer();
467                    if (reduced_resolution)
468                    {
469                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
470                                                                  mv[0].x, mv[0].y, stride, rounding);                                                                  mv[0].x, mv[0].y, stride, rounding);
471                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
# Line 547  Line 479 
479                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
480                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
481    
482                  } else if (dec->quarterpel) {                          /* set_block(pY_Cur, stride, 32, 32, 127); */
483                    }
484                    else
485                    {
486                            if(dec->quarterpel) {
487                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
488                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
489                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 560  Line 496 
496                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
497                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
498                                                                          mv[3].x, mv[3].y, stride, rounding);                                                                          mv[3].x, mv[3].y, stride, rounding);
499                  } else {                          }
500                            else {
501                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
502                                                                  mv[0].x, mv[0].y, stride, rounding);                                                                  mv[0].x, mv[0].y, stride, rounding);
503                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
# Line 570  Line 507 
507                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
508                                                                  mv[3].x, mv[3].y, stride, rounding);                                                                  mv[3].x, mv[3].y, stride, rounding);
509                  }                  }
         }  
510    
511          /* chroma */                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,
         if (reduced_resolution) {  
                 interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,  
512                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
513                  interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,
514                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
515                    }
516                    stop_comp_timer();
517            }
518    
519            for (i = 0; i < 6; i++) {
520                    int direction = dec->alternate_vertical_scan ? 2 : 0;
521    
522                    if (cbp & (1 << (5 - i)))       /* coded */
523                    {
524                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
525    
526                            start_timer();
527                            get_inter_block(bs, &block[i * 64], direction);
528                            stop_coding_timer();
529    
530                            start_timer();
531                            if (dec->quant_type == 0) {
532                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
533          } else {          } else {
534                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
                                                                 uv_dx, uv_dy, stride2, rounding);  
                 interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
535          }          }
536                            stop_iquant_timer();
537    
538          stop_comp_timer();                          start_timer();
539                            idct(&data[i * 64]);
540                            stop_idct_timer();
541                    }
542            }
543    
544            if (dec->interlacing && pMB->field_dct) {
545                    next_block = stride;
546                    stride *= 2;
547            }
548    
549            start_timer();
550            if (reduced_resolution)
551            {
552                    if (cbp & 32)
553                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
554                    if (cbp & 16)
555                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
556                    if (cbp & 8)
557                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
558                    if (cbp & 4)
559                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
560                    if (cbp & 2)
561                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
562                    if (cbp & 1)
563                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
564            }
565            else
566            {
567                    if (cbp & 32)
568                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
569                    if (cbp & 16)
570                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
571                    if (cbp & 8)
572                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
573                    if (cbp & 4)
574                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
575                    if (cbp & 2)
576                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
577                    if (cbp & 1)
578                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
579            }
580            stop_transfer_timer();
581    }
582    
583          if (cbp)  static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
584                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,  {
585                                                          reduced_resolution, pMB);          int length = 1 << (fcode+4);
586    
587    /*      if (quarterpel) value *= 2; */
588    
589            if (value < -length)
590                    return -length;
591            else if (value >= length)
592                    return length-1;
593            else return value;
594  }  }
595    
596    
597  static void  static void
598  decoder_mbgmc(DECODER * dec,  decoder_mbgmc(DECODER * dec,
599                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
# Line 600  Line 602 
602                                  const uint32_t fcode,                                  const uint32_t fcode,
603                                  const uint32_t cbp,                                  const uint32_t cbp,
604                                  Bitstream * bs,                                  Bitstream * bs,
605                                  const uint32_t rounding)                                  const uint32_t quant,
606                                    const uint32_t rounding,
607                                    const int reduced_resolution)   /* no reduced res support */
608  {  {
609    
610            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
611            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
612    
613          const uint32_t stride = dec->edged_width;          const uint32_t stride = dec->edged_width;
614          const uint32_t stride2 = stride / 2;          const uint32_t stride2 = stride / 2;
615            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
616            uint32_t i;
617            const uint32_t iQuant = pMB->quant;
618          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
619          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
620          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
621    
         NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;  
   
622          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
623    
624          start_timer();          start_timer();
625    
626  /* this is where the calculations are done */  /* this is where the calculations are done */
627    
628          gmc_data->predict_16x16(gmc_data,          {
629                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,                  pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
630                          stride, stride, x_pos, y_pos, rounding);                                          stride, stride2, dec->quarterpel, rounding, &dec->cur);
   
         gmc_data->predict_8x8(gmc_data,  
                         dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,  
                         dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,  
                         stride2, stride2, x_pos, y_pos, rounding);  
   
         gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);  
631    
632          pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);          pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
633          pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);          pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
634            }
635          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
636    
637          stop_transfer_timer();  /*
638            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
639            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
640            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
641    */
642    
         if (cbp)  
                 decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);  
643    
644  }          stop_transfer_timer();
645    
646            if (!cbp) return;
647    
648  static void          for (i = 0; i < 6; i++) {
649  decoder_iframe(DECODER * dec,                  int direction = dec->alternate_vertical_scan ? 2 : 0;
650                                  Bitstream * bs,  
651                                  int reduced_resolution,                  if (cbp & (1 << (5 - i)))       /* coded */
                                 int quant,  
                                 int intra_dc_threshold)  
652  {  {
653          uint32_t bound;                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
         uint32_t x, y;  
         uint32_t mb_width = dec->mb_width;  
         uint32_t mb_height = dec->mb_height;  
654    
655          if (reduced_resolution) {                          start_timer();
656                  mb_width = (dec->width + 31) / 32;                          get_inter_block(bs, &block[i * 64], direction);
657                  mb_height = (dec->height + 31) / 32;                          stop_coding_timer();
658    
659                            start_timer();
660                            if (dec->quant_type == 0) {
661                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
662                            } else {
663                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
664          }          }
665                            stop_iquant_timer();
666    
667          bound = 0;                          start_timer();
668                            idct(&data[i * 64]);
669                            stop_idct_timer();
670                    }
671            }
672    
673          for (y = 0; y < mb_height; y++) {  /* interlace + GMC is this possible ??? */
674                  for (x = 0; x < mb_width; x++) {  /*
675                          MACROBLOCK *mb;    if (dec->interlacing && pMB->field_dct) {
676                          uint32_t mcbpc;            next_block = stride;
677                          uint32_t cbpc;            stride *= 2;
678                          uint32_t acpred_flag;    }
679                          uint32_t cbpy;  */
680                          uint32_t cbp;          start_timer();
681            if (cbp & 32)
682                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
683            if (cbp & 16)
684                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
685            if (cbp & 8)
686                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
687            if (cbp & 4)
688                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
689            if (cbp & 2)
690                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
691            if (cbp & 1)
692                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
693            stop_transfer_timer();
694    }
695    
696    
697    void
698    decoder_iframe(DECODER * dec,
699                               Bitstream * bs,
700                               int reduced_resolution,
701                               int quant,
702                               int intra_dc_threshold)
703    {
704            uint32_t bound;
705            uint32_t x, y;
706            uint32_t mb_width = dec->mb_width;
707            uint32_t mb_height = dec->mb_height;
708    
709            if (reduced_resolution)
710            {
711                    mb_width = (dec->width + 31) / 32;
712                    mb_height = (dec->height + 31) / 32;
713            }
714    
715            bound = 0;
716    
717            for (y = 0; y < mb_height; y++) {
718                    for (x = 0; x < mb_width; x++) {
719                            MACROBLOCK *mb;
720                            uint32_t mcbpc;
721                            uint32_t cbpc;
722                            uint32_t acpred_flag;
723                            uint32_t cbpy;
724                            uint32_t cbp;
725    
726                          while (BitstreamShowBits(bs, 9) == 1)                          while (BitstreamShowBits(bs, 9) == 1)
727                                  BitstreamSkip(bs, 9);                                  BitstreamSkip(bs, 9);
# Line 681  Line 735 
735                          }                          }
736                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
737    
738                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
739    
740                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
741                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 708  Line 762 
762    
763                          if (dec->interlacing) {                          if (dec->interlacing) {
764                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
765                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
766                          }                          }
767    
768                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 722  Line 776 
776  }  }
777    
778    
779  static void  void
780  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
781                                  Bitstream * bs,                                  Bitstream * bs,
782                                  int x,                                  int x,
# Line 733  Line 787 
787                                  const int bound)                                  const int bound)
788  {  {
789    
790          const int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
791          const int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
792          const int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
793          const int range = (64 * scale_fac);          int range = (64 * scale_fac);
794    
795          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          VECTOR pmv;
796          VECTOR mv;          VECTOR mv;
797    
798            pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
799    
800          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
801          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
802    
803          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
804    
805          mv.x += pmv.x;          mv.x += pmv.x;
806          mv.y += pmv.y;          mv.y += pmv.y;
# Line 761  Line 817 
817                  mv.y -= range;                  mv.y -= range;
818          }          }
819    
820          /* clip to valid range */          ret_mv->x = mv.x;
821            ret_mv->y = mv.y;
822          if (mv.x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) )  }
                 mv.x = (int)(dec->mb_width - x) << (5 + dec->quarterpel);  
823    
         else if (mv.x < (int)(-x-1) << (5 + dec->quarterpel))  
                 mv.x = (int)(-x-1) << (5 + dec->quarterpel);  
824    
         if (mv.y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) )  
                 mv.y = (int)(dec->mb_height - y) << (5 + dec->quarterpel);  
825    
         else if (mv.y < ((int)(-y-1)) << (5 + dec->quarterpel) )  
                 mv.y = (int)(-y-1) << (5 + dec->quarterpel);  
826    
         ret_mv->x = mv.x;  
         ret_mv->y = mv.y;  
 }  
827    
828  /* for P_VOP set gmc_warp to NULL */  /* for P_VOP set gmc_warp to NULL */
829  static void  void
830  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
831                                  Bitstream * bs,                                  Bitstream * bs,
832                                  int rounding,                                  int rounding,
# Line 790  Line 836 
836                                  int intra_dc_threshold,                                  int intra_dc_threshold,
837                                  const WARPPOINTS *const gmc_warp)                                  const WARPPOINTS *const gmc_warp)
838  {  {
839    
840          uint32_t x, y;          uint32_t x, y;
841          uint32_t bound;          uint32_t bound;
842          int cp_mb, st_mb;          int cp_mb, st_mb;
843          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
844          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
845    
846          if (reduced_resolution) {          if (reduced_resolution)
847            {
848                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
849                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
850          }          }
851    
852          start_timer();          start_timer();
853          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
854                                          dec->width, dec->height, dec->bs_version);                                     dec->width, dec->height);
855          stop_edges_timer();          stop_edges_timer();
856    
857          if (gmc_warp) {          if (gmc_warp)
858            {
859    
860                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
861                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
862                    {
863                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
864                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
865                                    dec->sprite_warping_points);
866                    }
867    
868                  generate_GMCparameters( dec->sprite_warping_points,                  generate_GMCparameters( dec->sprite_warping_points,
869                                  dec->sprite_warping_accuracy, gmc_warp,                                  (2 << dec->sprite_warping_accuracy), gmc_warp,
870                                  dec->width, dec->height, &dec->new_gmc_data);                                  dec->width, dec->height, &dec->gmc_data);
871    
872                  /* image warping is done block-based in decoder_mbgmc(), now */                  /* image warping is done block-based in decoder_mbgmc(), now */
873    /*
874            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
875                                            mb_width, mb_height,
876                                            dec->edged_width, dec->edged_width/2,
877                                            fcode, dec->quarterpel, 0,
878                                            rounding, dec->mbs, &dec->gmc);
879    */
880          }          }
881    
882          bound = 0;          bound = 0;
# Line 826  Line 890 
890                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
891                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
892    
893                          if (check_resync_marker(bs, fcode - 1)) {                          if (check_resync_marker(bs, fcode - 1))
894                            {
895                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
896                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
897                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 834  Line 899 
899                          }                          }
900                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
901    
902                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
903    
904                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
905                                  uint32_t mcbpc, cbpc, cbpy, cbp;                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
906                                  uint32_t intra, acpred_flag = 0;                          {
907                                    uint32_t mcbpc;
908                                    uint32_t cbpc;
909                                    uint32_t acpred_flag;
910                                    uint32_t cbpy;
911                                    uint32_t cbp;
912                                    uint32_t intra;
913                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
914    
915                                  cp_mb++;                                  cp_mb++;
# Line 846  Line 917 
917                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
918                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
919    
920                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
921                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
922                                    acpred_flag = 0;
923    
924                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
925    
926                                    if (intra) {
927                                            acpred_flag = BitstreamGetBit(bs);
928                                    }
929    
930                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
931                                    {
932                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
933                                  else if (intra)                                  }
                                         acpred_flag = BitstreamGetBit(bs);  
934    
935                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
936                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
937    
938                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
939    
940                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
941                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
942                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
943                                          quant += dquant;                                          quant += dquant;
944                                          if (quant > 31) {                                          if (quant > 31) {
945                                                  quant = 31;                                                  quant = 31;
946                                          } else if (quant < 1) {                                          } else if (quant < 1) {
947                                                  quant = 1;                                                  quant = 1;
948                                          }                                          }
949                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);                                          DPRINTF(DPRINTF_MB, "quant %i", quant);
950                                  }                                  }
951                                  mb->quant = quant;                                  mb->quant = quant;
952    
953                                  if (dec->interlacing) {                                  if (dec->interlacing) {
954                                          if (cbp || intra) {                                          if (cbp || intra) {
955                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
956                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
957                                          }                                          }
958    
959                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
960                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
961                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
962    
963                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
964                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
965                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
966                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
967                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
968                                                  }                                                  }
969                                          }                                          }
970                                  }                                  }
971    
972                                  if (mcsel) {                                  if (mcsel) {
973                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
974                                                                    rounding, reduced_resolution);
975                                          continue;                                          continue;
976    
977                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
978    
979                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
980                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
981                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);                                                                                    fcode, bound);
982                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
983                                                                                      fcode, bound);
984                                          } else {                                          } else {
985                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
986                                                                                      fcode, bound);
987                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
988                                          }                                          }
989                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
990    
991                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
992                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
993                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
994                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
995                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
996                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  {
997                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
998                                                    0;
999                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1000                                                    0;
1001                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1002                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound, reduced_resolution);
1003                                          continue;                                          continue;
1004                                  }                                  }
1005    
1006                                  decoder_mbinter(dec, mb, x, y, cbp, bs,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1007                                                                  rounding, reduced_resolution, 0);                                                                  rounding, reduced_resolution);
1008    
1009                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          }
1010                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1011                            {
1012                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
1013                                  mb->quant = quant;  
1014                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);                                  start_timer();
1015    
1016                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1017                                                                    rounding, reduced_resolution);
1018    
1019                                    stop_transfer_timer();
1020    
1021                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1022                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1023                                          cp_mb = 0;                                          cp_mb = 0;
1024                                  }                                  }
1025                                  st_mb = x+1;                                  st_mb = x+1;
1026                          } else {        /* not coded P_VOP macroblock */                          }
1027                            else    /* not coded P_VOP macroblock */
1028                            {
1029                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
                                 mb->quant = quant;  
1030    
1031                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1032                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1033                                    /* copy macroblock directly from ref to cur */
1034    
1035                                  decoder_mbinter(dec, mb, x, y, 0, bs,                                  start_timer();
1036                                                                  rounding, reduced_resolution, 0);  
1037                                    if (reduced_resolution)
1038                                    {
1039                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1040                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1041                                                                             dec->edged_width);
1042    
1043                                            transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1044                                                                            dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1045                                                                            dec->edged_width/2);
1046    
1047                                            transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1048                                                                             dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1049                                                                             dec->edged_width/2);
1050                                    }
1051                                    else
1052                                    {
1053                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1054                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1055                                                                             dec->edged_width);
1056    
1057                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1058                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1059                                                                            dec->edged_width/2);
1060    
1061                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1062                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1063                                                                             dec->edged_width/2);
1064                                    }
1065    
1066                                    stop_transfer_timer();
1067    
1068                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1069                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
# Line 949  Line 1072 
1072                                  st_mb = x+1;                                  st_mb = x+1;
1073                          }                          }
1074                  }                  }
   
1075                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
1076                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                          output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1077          }          }
# Line 957  Line 1079 
1079    
1080    
1081  /* decode B-frame motion vector */  /* decode B-frame motion vector */
1082  static void  void
1083  get_b_motion_vector(Bitstream * bs,  get_b_motion_vector(DECODER * dec,
1084                                            Bitstream * bs,
1085                                            int x,
1086                                            int y,
1087                                          VECTOR * mv,                                          VECTOR * mv,
1088                                          int fcode,                                          int fcode,
1089                                          const VECTOR pmv,                                          const VECTOR pmv)
1090                                          const DECODER * const dec,  {
1091                                          const int x, const int y)          int scale_fac = 1 << (fcode - 1);
1092  {          int high = (32 * scale_fac) - 1;
1093          const int scale_fac = 1 << (fcode - 1);          int low = ((-32) * scale_fac);
1094          const int high = (32 * scale_fac) - 1;          int range = (64 * scale_fac);
1095          const int low = ((-32) * scale_fac);  
1096          const int range = (64 * scale_fac);          int mv_x, mv_y;
1097            int pmv_x, pmv_y;
1098    
1099          int mv_x = get_mv(bs, fcode);          pmv_x = pmv.x;
1100          int mv_y = get_mv(bs, fcode);          pmv_y = pmv.y;
1101    
1102          mv_x += pmv.x;          mv_x = get_mv(bs, fcode);
1103          mv_y += pmv.y;          mv_y = get_mv(bs, fcode);
1104    
1105          if (mv_x < low)          mv_x += pmv_x;
1106            mv_y += pmv_y;
1107    
1108            if (mv_x < low) {
1109                  mv_x += range;                  mv_x += range;
1110          else if (mv_x > high)          } else if (mv_x > high) {
1111                  mv_x -= range;                  mv_x -= range;
1112            }
1113    
1114          if (mv_y < low)          if (mv_y < low) {
1115                  mv_y += range;                  mv_y += range;
1116          else if (mv_y > high)          } else if (mv_y > high) {
1117                  mv_y -= range;                  mv_y -= range;
1118            }
1119    
1120            mv->x = mv_x;
1121            mv->y = mv_y;
1122    }
1123    
1124    
1125    /* decode an B-frame forward & backward inter macroblock */
1126    void
1127    decoder_bf_mbinter(DECODER * dec,
1128                                       const MACROBLOCK * pMB,
1129                                       const uint32_t x_pos,
1130                                       const uint32_t y_pos,
1131                                       const uint32_t cbp,
1132                                       Bitstream * bs,
1133                                       const uint32_t quant,
1134                                       const uint8_t ref)
1135    {
1136    
1137            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1138            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1139    
1140          /* clip to valid range */          uint32_t stride = dec->edged_width;
1141          if (mv_x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) )          uint32_t stride2 = stride / 2;
1142                  mv_x = (int)(dec->mb_width - x) << (5 + dec->quarterpel);          uint32_t next_block = stride * 8;
1143            uint32_t i;
1144            uint32_t iQuant = pMB->quant;
1145            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1146            int uv_dx, uv_dy;
1147    
1148          else if (mv_x < (int)(-x-1) << (5 + dec->quarterpel))          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1149                  mv_x = (int)(-x-1) << (5 + dec->quarterpel);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1150            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1151    
         if (mv_y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) )  
                 mv_y = (int)(dec->mb_height - y) << (5 + dec->quarterpel);  
1152    
1153          else if (mv_y < ((int)(-y-1)) << (5 + dec->quarterpel) )          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1154                  mv_y = (int)(-y-1) << (5 + dec->quarterpel);                  uv_dx = pMB->mvs[0].x;
1155                    uv_dy = pMB->mvs[0].y;
1156    
1157          mv->x = mv_x;                  if (dec->quarterpel)
1158          mv->y = mv_y;                  {
1159                            uv_dx /= 2;
1160                            uv_dy /= 2;
1161  }  }
1162    
1163  /* decode an B-frame direct & interpolate macroblock */                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1164  static void                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1165            } else {
1166                    int sum;
1167    
1168                    if(dec->quarterpel)
1169                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1170                    else
1171                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1172    
1173                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1174    
1175                    if(dec->quarterpel)
1176                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1177                    else
1178                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1179    
1180                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1181            }
1182    
1183            start_timer();
1184            if(dec->quarterpel) {
1185                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1186                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1187                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1188            }
1189            else {
1190                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1191                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1192                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1193                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1194                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1195                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1196                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1197                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1198            }
1199    
1200            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1201                                                      uv_dx, uv_dy, stride2, 0);
1202            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1203                                                      uv_dx, uv_dy, stride2, 0);
1204            stop_comp_timer();
1205    
1206            for (i = 0; i < 6; i++) {
1207                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1208    
1209                    if (cbp & (1 << (5 - i)))       /* coded */
1210                    {
1211                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1212    
1213                            start_timer();
1214                            get_inter_block(bs, &block[i * 64], direction);
1215                            stop_coding_timer();
1216    
1217                            start_timer();
1218                            if (dec->quant_type == 0) {
1219                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1220                            } else {
1221                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1222                            }
1223                            stop_iquant_timer();
1224    
1225                            start_timer();
1226                            idct(&data[i * 64]);
1227                            stop_idct_timer();
1228                    }
1229            }
1230    
1231            if (dec->interlacing && pMB->field_dct) {
1232                    next_block = stride;
1233                    stride *= 2;
1234            }
1235    
1236            start_timer();
1237            if (cbp & 32)
1238                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1239            if (cbp & 16)
1240                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1241            if (cbp & 8)
1242                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1243            if (cbp & 4)
1244                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1245            if (cbp & 2)
1246                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1247            if (cbp & 1)
1248                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1249            stop_transfer_timer();
1250    }
1251    
1252    /* decode an B-frame direct &  inter macroblock */
1253    void
1254  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1255                                                                  IMAGE forward,                                                                  IMAGE forward,
1256                                                                  IMAGE backward,                                                                  IMAGE backward,
1257                                                                  const MACROBLOCK * pMB,                                                                  const MACROBLOCK * pMB,
1258                                                                  const uint32_t x_pos,                                                                  const uint32_t x_pos,
1259                                                                  const uint32_t y_pos,                                                                  const uint32_t y_pos,
1260                                                                  Bitstream * bs,                                                             Bitstream * bs)
                                                                 const int direct)  
1261  {  {
1262    
1263            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1264            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1265    
1266          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
1267          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
1268            uint32_t next_block = stride * 8;
1269            uint32_t iQuant = pMB->quant;
1270          int uv_dx, uv_dy;          int uv_dx, uv_dy;
1271          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
1272            uint32_t i;
1273          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1274          const uint32_t cbp = pMB->cbp;          const uint32_t cbp = pMB->cbp;
1275    
# Line 1026  Line 1277 
1277          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1278          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1279    
1280          if (!direct) {  
1281            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1282                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1283                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1284    
1285                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1286                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1287    
1288                  if (dec->quarterpel) {                  if (dec->quarterpel)
1289                    {
1290                          uv_dx /= 2;                          uv_dx /= 2;
1291                          uv_dy /= 2;                          uv_dy /= 2;
1292    
1293                          b_uv_dx /= 2;                          b_uv_dx /= 2;
1294                          b_uv_dy /= 2;                          b_uv_dy /= 2;
1295                  }                  }
# Line 1045  Line 1299 
1299    
1300                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1301                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
   
1302          } else {          } else {
1303                  if(dec->quarterpel) {                  int sum;
1304                          uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
1305                          uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);                  if(dec->quarterpel)
1306                          b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1307                          b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);                  else
1308                  } else {                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1309                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
1310                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1311                          b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;  
1312                          b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                  if(dec->quarterpel)
1313                  }                          sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1314                    else
1315                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1316    
1317                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1318    
1319    
1320                    if(dec->quarterpel)
1321                            sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1322                    else
1323                            sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1324    
1325                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1326    
1327                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  if(dec->quarterpel)
1328                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];                          sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1329                  b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];                  else
1330                  b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1331    
1332                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1333          }          }
1334    
1335    
1336          start_timer();          start_timer();
1337          if(dec->quarterpel) {          if(dec->quarterpel) {
1338                  if(!direct) {                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1339                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1340                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1341                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1342                  } else {                  else {
1343                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1344                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1345                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
# Line 1085  Line 1353 
1353                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1354                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1355                  }                  }
1356          } else {          }
1357            else {
1358                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1359                                                          pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                          pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1360                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1361                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1362                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1363                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1364                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1365                                                          pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1366                                                              0);
1367          }          }
1368    
1369          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1103  Line 1373 
1373    
1374    
1375          if(dec->quarterpel) {          if(dec->quarterpel) {
1376                  if(!direct) {                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1377                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1378                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1379                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1380                  } else {                  else {
1381                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1382                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1383                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
# Line 1121  Line 1391 
1391                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1392                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1393                  }                  }
1394          } else {          }
1395            else {
1396                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1397                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1398                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1399                                                          16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1400                                                              0);
1401                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1402                                                          16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1403                                                              stride, 0);
1404                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1405                                                          16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1406                                                              stride, 0);
1407          }          }
1408    
1409          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
# Line 1169  Line 1443 
1443    
1444          stop_comp_timer();          stop_comp_timer();
1445    
1446          if (cbp)          for (i = 0; i < 6; i++) {
1447                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1448    
1449                    if (cbp & (1 << (5 - i)))       /* coded */
1450                    {
1451                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1452    
1453                            start_timer();
1454                            get_inter_block(bs, &block[i * 64], direction);
1455                            stop_coding_timer();
1456    
1457                            start_timer();
1458                            if (dec->quant_type == 0) {
1459                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1460                            } else {
1461                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1462                            }
1463                            stop_iquant_timer();
1464    
1465                            start_timer();
1466                            idct(&data[i * 64]);
1467                            stop_idct_timer();
1468                    }
1469            }
1470    
1471            if (dec->interlacing && pMB->field_dct) {
1472                    next_block = stride;
1473                    stride *= 2;
1474            }
1475    
1476            start_timer();
1477            if (cbp & 32)
1478                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1479            if (cbp & 16)
1480                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1481            if (cbp & 8)
1482                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1483            if (cbp & 4)
1484                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1485            if (cbp & 2)
1486                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1487            if (cbp & 1)
1488                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1489            stop_transfer_timer();
1490  }  }
1491    
1492    
1493  /* for decode B-frame dbquant */  /* for decode B-frame dbquant */
1494  static __inline int32_t  int32_t __inline
1495  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1496  {  {
1497          if (!BitstreamGetBit(bs))               /*  '0' */          if (!BitstreamGetBit(bs))               /*  '0' */
# Line 1186  Line 1503 
1503  }  }
1504    
1505  /*  /*
1506   * decode B-frame mb_type   * For decode B-frame mb_type
1507   * bit          ret_value   * bit          ret_value
1508   * 1            0   * 1            0
1509   * 01           1   * 01           1
1510   * 001          2   * 001          2
1511   * 0001         3   * 0001         3
1512   */   */
1513  static int32_t __inline  int32_t __inline
1514  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1515  {  {
1516          int32_t mb_type;          int32_t mb_type;
1517    
1518          for (mb_type = 0; mb_type <= 3; mb_type++)          for (mb_type = 0; mb_type <= 3; mb_type++) {
1519                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
1520                          return (mb_type);                          break;
1521            }
1522    
1523          return -1;          if (mb_type <= 3)
1524                    return (mb_type);
1525            else
1526                    return (-1);
1527  }  }
1528    
1529  static void  void
1530  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1531                                  Bitstream * bs,                                  Bitstream * bs,
1532                                  int quant,                                  int quant,
# Line 1215  Line 1536 
1536          uint32_t x, y;          uint32_t x, y;
1537          VECTOR mv;          VECTOR mv;
1538          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1539          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  #ifdef BFRAMES_DEC_DEBUG
1540          int i;          FILE *fp;
1541            static char first=0;
1542    #define BFRAME_DEBUG    if (!first && fp){ \
1543                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1544            }
1545    #endif
1546    
1547          start_timer();          start_timer();
1548          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1549                                          dec->width, dec->height, dec->bs_version);                                     dec->width, dec->height);
1550          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1551                                          dec->width, dec->height, dec->bs_version);                                     dec->width, dec->height);
1552          stop_edges_timer();          stop_edges_timer();
1553    
1554    #ifdef BFRAMES_DEC_DEBUG
1555            if (!first){
1556                    fp=fopen("C:\\XVIDDBG.TXT","w");
1557            }
1558    #endif
1559    
1560          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1561                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
1562                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1563                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1564                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1565                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
                         const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;  
                         uint32_t intra_dc_threshold; /* fake variable */  
   
                         if (check_resync_marker(bs, fcode_max  - 1)) {  
                                 int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,  
                                                                                                          &fcode_forward, &fcode_backward, &intra_dc_threshold);  
                                 x = bound % dec->mb_width;  
                                 y = bound / dec->mb_width;  
                                 /* reset predicted macroblocks */  
                                 dec->p_fmv = dec->p_bmv = zeromv;  
                         }  
1566    
1567                          mv =                          mv =
1568                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1569                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
                         mb->quant = quant;  
1570    
1571                          /*                          /*
1572                           * skip if the co-located P_VOP macroblock is not coded                           * skip if the co-located P_VOP macroblock is not coded
# Line 1255  Line 1575 
1575                           */                           */
1576    
1577                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1578                                    /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1579                                  mb->cbp = 0;                                  mb->cbp = 0;
1580                                  mb->mode = MODE_FORWARD;  #ifdef BFRAMES_DEC_DEBUG
1581                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  mb->mb_type = MODE_NOT_CODED;
1582            BFRAME_DEBUG
1583    #endif
1584                                    mb->mb_type = MODE_FORWARD;
1585                                    mb->quant = last_mb->quant;
1586                                    /*
1587                                      mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1588                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1589                                    */
1590    
1591                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1592                                  continue;                                  continue;
1593                          }                          }
1594    
1595                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1596                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1597    
1598                                  mb->mode = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1599    
1600                                  if (!modb2)             /* modb=='00' */                                  if (!modb2) {   /* modb=='00' */
1601                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1602                                  else                                  } else {
1603                                          mb->cbp = 0;                                          mb->cbp = 0;
1604                                    }
1605                                  if (mb->mode && mb->cbp) {                                  if (mb->mb_type && mb->cbp) {
1606                                          quant += get_dbquant(bs);                                          quant += get_dbquant(bs);
1607                                          if (quant > 31)  
1608                                            if (quant > 31) {
1609                                                  quant = 31;                                                  quant = 31;
1610                                          else if (quant < 1)                                          } else if (quant < 1) {
1611                                                  quant = 1;                                                  quant = 1;
1612                                  }                                  }
                                 mb->quant = quant;  
   
                                 if (dec->interlacing) {  
                                         if (mb->cbp) {  
                                                 mb->field_dct = BitstreamGetBit(bs);  
                                                 DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);  
                                         }  
   
                                         if (mb->mode) {  
                                                 mb->field_pred = BitstreamGetBit(bs);  
                                                 DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);  
   
                                                 if (mb->field_pred) {  
                                                         mb->field_for_top = BitstreamGetBit(bs);  
                                                         DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);  
                                                         mb->field_for_bot = BitstreamGetBit(bs);  
                                                         DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);  
                                                 }  
1613                                          }                                          }
                                 }  
   
1614                          } else {                          } else {
1615                                  mb->mode = MODE_DIRECT_NONE_MV;                                  mb->mb_type = MODE_DIRECT_NONE_MV;
1616                                  mb->cbp = 0;                                  mb->cbp = 0;
1617                          }                          }
1618    
1619                          switch (mb->mode) {                          mb->quant = quant;
1620                            mb->mode = MODE_INTER4V;
1621                            /* DEBUG1("Switch bm_type=",mb->mb_type); */
1622    
1623    #ifdef BFRAMES_DEC_DEBUG
1624            BFRAME_DEBUG
1625    #endif
1626    
1627                            switch (mb->mb_type) {
1628                          case MODE_DIRECT:                          case MODE_DIRECT:
1629                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1630    
1631                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1632                                    {
1633                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1634                                            int i;
1635    
1636                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
1637                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1638                                                                          / TRD + mv.x);
1639                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1640                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)
1641                                                                                      / TRD
1642                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);
1643                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1644                                                                          / TRD + mv.y);
1645                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1646                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)
1647                                                                                      / TRD
1648                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);
1649                                  }                                  }
1650                                            /* DEBUG("B-frame Direct!\n"); */
1651                                    }
1652                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1653                                                                                                  mb, x, y, bs, 1);                                                                                             mb, x, y, bs);
1654                                  break;                                  break;
1655    
1656                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1657                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1658                                                                            dec->p_fmv);
1659                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1660    
1661                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1662                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];                                                                          fcode_backward, dec->p_bmv);
1663                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1664                                            mb->b_mvs[3] = mb->b_mvs[0];
1665    
1666                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1667                                                                                          mb, x, y, bs, 0);                                                                                             mb, x, y, bs);
1668                                    /* DEBUG("B-frame Bidir!\n"); */
1669                                  break;                                  break;
1670    
1671                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1672                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1673                                                                            dec->p_bmv);
1674                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1675    
1676                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);                                  mb->mode = MODE_INTER;
1677                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1678                                    /* DEBUG("B-frame Backward!\n"); */
1679                                  break;                                  break;
1680    
1681                          case MODE_FORWARD:                          case MODE_FORWARD:
1682                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1683                                                                            dec->p_fmv);
1684                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1685    
1686                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  mb->mode = MODE_INTER;
1687                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1688                                    /* DEBUG("B-frame Forward!\n"); */
1689                                  break;                                  break;
1690    
1691                          default:                          default:
1692                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1693                          }                          }
1694                  } /* End of for */                  } /* End of for */
1695          }          }
1696    
1697    #ifdef BFRAMES_DEC_DEBUG
1698            if (!first){
1699                    first=1;
1700                    if (fp)
1701                            fclose(fp);
1702            }
1703    #endif
1704  }  }
1705    
1706    /* swap two MACROBLOCK array */
1707    void
1708    mb_swap(MACROBLOCK ** mb1,
1709                    MACROBLOCK ** mb2)
1710    {
1711            MACROBLOCK *temp = *mb1;
1712    
1713            *mb1 = *mb2;
1714            *mb2 = temp;
1715    }
1716    
1717    
1718  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1719  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1720                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,                                          const XVID_DEC_FRAME * frame, int pp_disable)
                                         int coding_type, int quant)  
1721  {  {
         if (dec->cartoon_mode)  
                 frame->general &= ~XVID_FILMEFFECT;  
1722    
1723          if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL)     /* post process */          if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1724          {          {
1725                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1726                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1727                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,                  image_deblock(&dec->tmp, dec->edged_width,
1728                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1729                                             frame->general, dec->frames, (coding_type == B_VOP));                                            frame->general);
1730                  img = &dec->tmp;                  img = &dec->tmp;
1731          }          }
1732    
1733          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1734                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,                                   dec->edged_width, frame->image, frame->stride,
1735                                   frame->output.csp, dec->interlacing);                                   frame->colorspace, dec->interlacing);
   
         if (stats) {  
                 stats->type = coding2type(coding_type);  
                 stats->data.vop.time_base = (int)dec->time_base;  
                 stats->data.vop.time_increment = 0;     /* XXX: todo */  
                 stats->data.vop.qscale_stride = dec->mb_width;  
                 stats->data.vop.qscale = dec->qscale;  
                 if (stats->data.vop.qscale != NULL && mbs != NULL) {  
                         int i;  
                         for (i = 0; i < dec->mb_width*dec->mb_height; i++)  
                                 stats->data.vop.qscale[i] = mbs[i].quant;  
                 } else  
                         stats->data.vop.qscale = NULL;  
         }  
1736  }  }
1737    
1738    
1739  int  int
1740  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1741                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1742  {  {
1743    
1744          Bitstream bs;          Bitstream bs;
1745          uint32_t rounding;          uint32_t rounding;
1746          uint32_t reduced_resolution;          uint32_t reduced_resolution;
1747          uint32_t quant = 2;          uint32_t quant;
1748          uint32_t fcode_forward;          uint32_t fcode_forward;
1749          uint32_t fcode_backward;          uint32_t fcode_backward;
1750          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1751          WARPPOINTS gmc_warp;          WARPPOINTS gmc_warp;
1752          int coding_type;          int vop_type;
1753          int success, output, seen_something;          int success = 0;
1754            int output = 0;
1755          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */          int seen_something = 0;
                 return XVID_ERR_VERSION;  
1756    
1757          start_global_timer();          start_global_timer();
1758    
1759          dec->low_delay_default = (frame->general & XVID_LOWDELAY);          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1760          if ((frame->general & XVID_DISCONTINUITY))          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1761    
1762            if ((frame->general & XVID_DEC_DISCONTINUITY))
1763                  dec->frames = 0;                  dec->frames = 0;
         dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;  
1764    
1765          if (frame->length < 0) {        /* decoder flush */          if (frame->length < 0)  /* decoder flush */
1766                  int ret;          {
1767                  /* if not decoding "low_delay/packed", and this isn't low_delay and                  /* if not decoding "low_delay/packed", and this isn't low_delay and
1768                          we have a reference frame, then outout the reference frame */                          we have a reference frame, then outout the reference frame */
1769                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1770                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                  {
1771                          dec->frames = 0;                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1772                          ret = 0;                          output = 1;
1773                  } else {                  }
1774                          if (stats) stats->type = XVID_TYPE_NOTHING;  
1775                          ret = XVID_ERR_END;                  frame->length = 0;
1776                    if (stats)
1777                    {
1778                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1779                            stats->data.vop.time_base = (int)dec->time_base;
1780                            stats->data.vop.time_increment = 0;     /* XXX: todo */
1781                  }                  }
1782    
1783                  emms();                  emms();
1784    
1785                  stop_global_timer();                  stop_global_timer();
1786                  return ret;                  return XVID_ERR_OK;
1787          }          }
1788    
1789          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
# Line 1443  Line 1791 
1791          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1792          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1793          {          {
1794                    if (stats)
1795                            stats->notify = XVID_DEC_VOP;
1796                    frame->length = 1;
1797                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1798                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);
                 if (stats) stats->type = XVID_TYPE_NOTHING;  
1799                  emms();                  emms();
1800                  return 1;       /* one byte consumed */                  return XVID_ERR_OK;
1801          }          }
1802    
         success = 0;  
         output = 0;  
         seen_something = 0;  
   
1803  repeat:  repeat:
1804    
1805          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1806                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1807    
1808          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1809                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1810    
1811          if (coding_type == -1) { /* nothing */          if (vop_type == -1)
1812            {
1813                  if (success) goto done;                  if (success) goto done;
                 if (stats) stats->type = XVID_TYPE_NOTHING;  
1814                  emms();                  emms();
1815                  return BitstreamPos(&bs)/8;                  return XVID_ERR_FAIL;
1816          }          }
1817    
1818          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */          if (vop_type == -2 || vop_type == -3)
1819            {
1820                  if (coding_type == -3)                  if (vop_type == -3)
1821                          decoder_resize(dec);                          decoder_resize(dec);
1822    
1823                  if (stats) {                  if (stats)
1824                          stats->type = XVID_TYPE_VOL;                  {
1825                            stats->notify = XVID_DEC_VOL;
1826                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1827                          /*XXX: if (dec->interlacing)                          if (dec->interlacing)
1828                                  stats->data.vol.general |= ++INTERLACING; */                                  stats->data.vol.general |= XVID_INTERLACING;
1829                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1830                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1831                          stats->data.vol.par = dec->aspect_ratio;                          stats->data.vol.aspect_ratio = dec->aspect_ratio;
1832                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1833                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1834                            frame->length = BitstreamPos(&bs) / 8;
1835                          emms();                          emms();
1836                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */                          return XVID_ERR_OK;
1837                  }                  }
1838                  goto repeat;                  goto repeat;
1839          }          }
1840    
1841          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1842    
1843    
1844          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1845          if (dec->packed_mode && coding_type == N_VOP) {          if (dec->packed_mode && vop_type == N_VOP)
1846                  if (dec->low_delay_default && dec->frames > 0) {          {
1847                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                  if (dec->low_delay_default && dec->frames > 0)
1848                    {
1849                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1850                          output = 1;                          output = 1;
1851                  }                  }
1852                  /* ignore otherwise */                  /* ignore otherwise */
1853          } else if (coding_type != B_VOP) {          }
1854                  switch(coding_type) {          else if (vop_type != B_VOP)
1855            {
1856                    switch(vop_type)
1857                    {
1858                  case I_VOP :                  case I_VOP :
1859                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1860                          break;                          break;
# Line 1513  Line 1867 
1867                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1868                          break;                          break;
1869                  case N_VOP :                  case N_VOP :
                         /* XXX: not_coded vops are not used for forward prediction */  
                         /* we should not swap(last_mbs,mbs) */  
1870                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
                         SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */  
1871                          break;                          break;
1872                  }                  }
1873    
1874                  if (reduced_resolution) {                  if (reduced_resolution)
1875                    {
1876                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1877                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1878                                  16, 0);                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1879                  }                  }
1880    
1881                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1882                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode))
1883                          if (dec->low_delay) {                  {
1884                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);                          if (dec->low_delay)
1885                            {
1886                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1887                                  output = 1;                                  output = 1;
1888                          } else if (dec->frames > 0)     { /* is the reference frame valid? */                          }
1889                            else if (dec->frames > 0)       /* is the reference frame valid? */
1890                            {
1891                                  /* output the reference frame */                                  /* output the reference frame */
1892                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1893                                  output = 1;                                  output = 1;
1894                          }                          }
1895                  }                  }
1896    
1897                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1898                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1899                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);                  mb_swap(&dec->mbs, &dec->last_mbs);
1900                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
                 dec->last_coding_type = coding_type;  
1901    
1902                  dec->frames++;                  dec->frames++;
1903                  seen_something = 1;                  seen_something = 1;
1904    
1905          } else {        /* B_VOP */          } else {        /* B_VOP */
1906    
1907                  if (dec->low_delay) {                  if (dec->low_delay)
1908                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");                  {
1909                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1910                          dec->low_delay = 1;                          dec->low_delay = 1;
1911                  }                  }
1912    
1913                  if (dec->frames < 2) {                  if (dec->frames < 2)
1914                    {
1915                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1916                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1917                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1918                  } else if (dec->time_pp <= dec->time_bp) {                  } else if (dec->time_pp <= dec->time_bp) {
1919                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1920                          decoded in vfw. */                          decoded in vfw. */
1921                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1922                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1923                  } else {                  } else {
1924                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
                         decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);  
1925                  }                  }
1926    
1927                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1928                  output = 1;                  output = 1;
1929                  dec->frames++;                  dec->frames++;
1930          }          }
1931    
 #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */  
1932           BitstreamByteAlign(&bs);           BitstreamByteAlign(&bs);
 #endif  
1933    
1934          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1935          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1936            {
1937                  success = 1;                  success = 1;
1938                  goto repeat;                  goto repeat;
1939          }          }
# Line 1588  Line 1942 
1942    
1943          /* low_delay_default mode: if we've gotten here without outputting anything,          /* low_delay_default mode: if we've gotten here without outputting anything,
1944             then output the recently decoded frame, or print an error message  */             then output the recently decoded frame, or print an error message  */
1945          if (dec->low_delay_default && output == 0) {          if (dec->low_delay_default && output == 0)
1946                  if (dec->packed_mode && seen_something) {          {
1947                    if (dec->packed_mode && seen_something)
1948                    {
1949                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1950                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1951                  } else {                          output = 1;
1952                    }
1953                    else
1954                    {
1955                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1956                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1957                                  "warning: nothing to output");                                  "warning: nothing to output");
1958                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1959                                  "bframe decoder lag");                                  "bframe decoder lag");
1960    
1961                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1962                  }                  }
1963          }          }
1964    
1965            frame->length = BitstreamPos(&bs) / 8;
1966    
1967            if (stats)
1968            {
1969                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1970                    stats->data.vop.time_base = (int)dec->time_base;
1971                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1972            }
1973    
1974          emms();          emms();
1975    
1976          stop_global_timer();          stop_global_timer();
1977    
1978          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */          return XVID_ERR_OK;
1979  }  }

Legend:
Removed from v.1.49.2.35  
changed lines
  Added in v.1.49.4.1

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