[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.37.2.27, Sat Jan 11 14:59:23 2003 UTC revision 1.42, Sat Oct 19 12:20:33 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  -  Decoder main module  -
5   *   *
6     *  Copyright(C) 2002 MinChen <chenm001@163.com>
7     *               2002 Peter Ross <pross@xvid.org>
8     *
9   *  This program is an implementation of a part of one or more MPEG-4   *  This program is an implementation of a part of one or more MPEG-4
10   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
11   *  to use this software module in hardware or software products are   *  to use this software module in hardware or software products are
# Line 33  Line 36 
36   *  History:   *  History:
37   *   *
38   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
39   *              MinChen <chenm001@163.com>  
40   *  10.07.2002  added BFRAMES_DEC_DEBUG support   *  10.07.2002  added BFRAMES_DEC_DEBUG support
41   *              Fix a little bug for low_delay flage   *              Fix a little bug for low_delay flage
42   *              MinChen <chenm001@163.com>   *              MinChen <chenm001@163.com>
# Line 53  Line 56 
56   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
57   *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block   *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block
58   *  22.12.2001  lock based interpolation   *  22.12.2001  lock based interpolation
59   *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *  01.12.2001  inital version; (c)2001 peter ross <pross@xvid.org>
60   *   *
61   *  $Id$   *  $Id$
62   *   *
63   *************************************************************************/   *************************************************************************/
64    
 #include <stdio.h>  
65  #include <stdlib.h>  #include <stdlib.h>
66  #include <string.h>  #include <string.h>
67    
# Line 69  Line 71 
71    
72  #include "xvid.h"  #include "xvid.h"
73  #include "portab.h"  #include "portab.h"
 #include "global.h"  
74    
75  #include "decoder.h"  #include "decoder.h"
76  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 81  Line 82 
82  #include "dct/fdct.h"  #include "dct/fdct.h"
83  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
84  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "image/reduced.h"  
 #include "image/font.h"  
85    
86  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
87  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
88  #include "utils/timer.h"  #include "utils/timer.h"
89  #include "utils/emms.h"  #include "utils/emms.h"
 #include "motion/motion.h"  
90    
91  #include "image/image.h"  #include "image/image.h"
92  #include "image/colorspace.h"  #include "image/colorspace.h"
93  #include "utils/mem_align.h"  #include "utils/mem_align.h"
94    
95  int  int
96  decoder_resize(DECODER * dec)  decoder_create(XVID_DEC_PARAM * param)
97  {  {
98          /* free existing */          DECODER *dec;
   
         image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
         image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
         image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
         image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
         image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
   
         image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);  
99    
100          if (dec->last_mbs)          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
101                  xvid_free(dec->last_mbs);          if (dec == NULL) {
102          if (dec->mbs)                  return XVID_ERR_MEMORY;
103                  xvid_free(dec->mbs);          }
104            param->handle = dec;
105    
106          /* realloc */          dec->width = param->width;
107            dec->height = param->height;
108    
109          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
110          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
111    
112          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
113          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
114            dec->low_delay = 0;
115    
116          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
117                  xvid_free(dec);                  xvid_free(dec);
# Line 130  Line 123 
123                  xvid_free(dec);                  xvid_free(dec);
124                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
125          }          }
   
126          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
127          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
128          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
# Line 139  Line 131 
131                  xvid_free(dec);                  xvid_free(dec);
132                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
133          }          }
134          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {  
135                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
138                  xvid_free(dec);                  xvid_free(dec);
139                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
140          }          }
# Line 173  Line 146 
146                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
147                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
148                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
149                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
150                  xvid_free(dec);                  xvid_free(dec);
151                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
152          }          }
153    
154          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
155    
156          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
# Line 190  Line 163 
163                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
164                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
165                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
166                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
167                  xvid_free(dec);                  xvid_free(dec);
168                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
169          }          }
170    
171          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);
172    
         return XVID_ERR_OK;  
 }  
   
   
 int  
 decoder_create(XVID_DEC_PARAM * param)  
 {  
         DECODER *dec;  
   
         dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);  
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         memset(dec, 0, sizeof(DECODER));  
   
         param->handle = dec;  
   
         dec->width = param->width;  
         dec->height = param->height;  
   
         image_null(&dec->cur);  
         image_null(&dec->refn[0]);  
         image_null(&dec->refn[1]);  
         image_null(&dec->tmp);  
         image_null(&dec->qtmp);  
   
 /* image based GMC */  
         image_null(&dec->gmc);  
   
   
         dec->mbs = NULL;  
         dec->last_mbs = NULL;  
   
173          init_timer();          init_timer();
174    
175          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
176          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
177          dec->frames = 0;          dec->frames = -1;
178          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
         dec->low_delay = 0;  
         dec->packed_mode = 0;  
179    
         dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);  
   
         if (dec->fixed_dimensions)  
                 return decoder_resize(dec);  
         else  
180                  return XVID_ERR_OK;                  return XVID_ERR_OK;
181  }  }
182    
# Line 254  Line 186 
186  {  {
187          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
188          xvid_free(dec->mbs);          xvid_free(dec->mbs);
   
         image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */  
   
189          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
190          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
191          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
         image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
192          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
193          xvid_free(dec);          xvid_free(dec);
194    
# Line 289  Line 217 
217                                  Bitstream * bs,                                  Bitstream * bs,
218                                  const uint32_t quant,                                  const uint32_t quant,
219                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
220                                  const unsigned int bound,                                  const unsigned int bound)
                                 const int reduced_resolution)  
221  {  {
222    
223          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 303  Line 230 
230          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
231          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
232    
         if (reduced_resolution) {  
                 pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);  
         }else{  
233                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
234                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
235                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
         }  
236    
237          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
238    
# Line 350  Line 271 
271                  start_timer();                  start_timer();
272                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
273                  {                  {
274                          int direction = dec->alternate_vertical_scan ?                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
275                                  2 : pMB->acpred_directions[i];                                                          start_coeff);
   
                         get_intra_block(bs, &block[i * 64], direction, start_coeff);  
276                  }                  }
277                  stop_coding_timer();                  stop_coding_timer();
278    
# Line 372  Line 291 
291                  start_timer();                  start_timer();
292                  idct(&data[i * 64]);                  idct(&data[i * 64]);
293                  stop_idct_timer();                  stop_idct_timer();
   
294          }          }
295    
296          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 381  Line 299 
299          }          }
300    
301          start_timer();          start_timer();
   
         if (reduced_resolution)  
         {  
                 next_block*=2;  
                 copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }else{  
302                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
303                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
304                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
305                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
306                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
307                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
         }  
308          stop_transfer_timer();          stop_transfer_timer();
309  }  }
310    
311    
312    
313    
314    
315    #define SIGN(X) (((X)>0)?1:-1)
316    #define ABS(X) (((X)>0)?(X):-(X))
317    static const uint32_t roundtab[16] =
318            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
319    
320    
321  // decode an inter macroblock  // decode an inter macroblock
322    
323  void  void
# Line 416  Line 329 
329                                  const uint32_t cbp,                                  const uint32_t cbp,
330                                  Bitstream * bs,                                  Bitstream * bs,
331                                  const uint32_t quant,                                  const uint32_t quant,
332                                  const uint32_t rounding,                                  const uint32_t rounding)
                                 const int reduced_resolution,  
                                 const int mcsel)  
333  {  {
334    
335          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 426  Line 337 
337    
338          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
339          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
340          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);          uint32_t next_block = stride * 8;
341          uint32_t i;          uint32_t i;
342          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
343          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
         uint8_t *pY_Ref, *pU_Ref, *pV_Ref;              /* ref for GMC is _not_ pRef itself */  
   
344          int uv_dx, uv_dy;          int uv_dx, uv_dy;
         VECTOR mv[4];   /* local copy of mvs */  
345    
         if (reduced_resolution) {  
                 pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);  
                 pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);  
                 pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);  
                 for (i = 0; i < 4; i++) {  
                         mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);  
                         mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);  
                 }  
         } else {  
346                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
347                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
348                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
                 for (i = 0; i < 4; i++)  
                         mv[i] = pMB->mvs[i];  
         }  
   
         if (mcsel) {  
                 mv[0].x = mv[0].y = mv[1].x = mv[1].y = mv[2].x = mv[2].y = mv[3].x = mv[3].y = 0;  
                         /* position in ref is same as the block, set vector to (0,0) */  
                 pY_Ref = dec->gmc.y;  
                 pU_Ref = dec->gmc.u;  
                 pV_Ref = dec->gmc.v;  
                         /* but reference itself isn't. It's warped... */  
                         /* Btw., this is too slow! For GMC it should simply be transfer_16to8add() */  
         } else {  
                 pY_Ref = dec->refn[0].y;  
                 pU_Ref = dec->refn[0].u;  
                 pV_Ref = dec->refn[0].v;  
         }  
349    
350          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
351                    uv_dx = pMB->mvs[0].x;
352                    uv_dy = pMB->mvs[0].y;
353    
354                  uv_dx = mv[0].x / (1 + dec->quarterpel);                  if (dec->quarterpel)
                 uv_dy = mv[0].y / (1 + dec->quarterpel);  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
   
                 start_timer();  
                 if (reduced_resolution)  
                 {  
                         interpolate32x32_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,  
                                                                   mv[0].x, mv[0].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
   
                 }  
                 else  
355                  {                  {
356                          if(dec->quarterpel) {                          uv_dx = (uv_dx >> 1) | (uv_dx & 1);
357                                  interpolate16x16_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,                          uv_dy = (uv_dy >> 1) | (uv_dy & 1);
                                                                                         dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                                         mv[0].x, mv[0].y, stride, rounding);  
                         }  
                         else {  
                                 interpolate16x16_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,  
                                                                           mv[0].x, mv[0].y, stride, rounding);  
358                          }                          }
359    
360                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
361                                                                    uv_dx, uv_dy, stride2, rounding);                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
362                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,          } else {
                                                                   uv_dx, uv_dy, stride2, rounding);  
                 }  
                 stop_comp_timer();  
   
         } else {        /* MODE_INTER4V */  
363                  int sum;                  int sum;
364                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
365    
366                  if(dec->quarterpel)                  if(dec->quarterpel)
367                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);                  {
368                  else                          sum /= 2;
369                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;                  }
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
370    
371                  if(dec->quarterpel)                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
                         sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);  
                 else  
                         sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;  
372    
373                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
374    
375                  start_timer();                  if (dec->quarterpel)
                 if (reduced_resolution)  
376                  {                  {
377                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,                          sum /= 2;
378                                                                    mv[0].x, mv[0].y, stride,  rounding);                  }
                         interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos,  
                                                                   mv[1].x, mv[1].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos + 16,  
                                                                   mv[2].x, mv[2].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos + 16,  
                                                                   mv[3].x, mv[3].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
379    
380                          // set_block(pY_Cur, stride, 32, 32, 127);                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
381                  }                  }
382                  else  
383                  {          start_timer();
384                          if(dec->quarterpel) {                          if(dec->quarterpel) {
385                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,                  DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n");
386                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
387                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
388                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
389                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
390                                                                                    mv[1].x, mv[1].y, stride,  rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
391                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
392                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
393                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
                                 interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,  
                                                                                   dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,  
                                                                                   mv[3].x, mv[3].y, stride,  rounding);  
394                          }                          }
395                          else {                          else {
396                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
397                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
398                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
399                                                                            mv[1].x, mv[1].y, stride,  rounding);                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
400                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos + 8,                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
401                                                                            mv[2].x, mv[2].y, stride,  rounding);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
402                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 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,
403                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
404                          }                          }
405    
406                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
407                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
408                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
409                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
                 }  
410                  stop_comp_timer();                  stop_comp_timer();
         }  
411    
412          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
413                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
414                  {                  {
415                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
416    
417                          start_timer();                          start_timer();
418                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
419                          stop_coding_timer();                          stop_coding_timer();
420    
421                          start_timer();                          start_timer();
# Line 604  Line 438 
438          }          }
439    
440          start_timer();          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  
         {  
441                  if (cbp & 32)                  if (cbp & 32)
442                          transfer_16to8add(pY_Cur, &data[0 * 64], stride);                          transfer_16to8add(pY_Cur, &data[0 * 64], stride);
443                  if (cbp & 16)                  if (cbp & 16)
# Line 633  Line 450 
450                          transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                          transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
451                  if (cbp & 1)                  if (cbp & 1)
452                          transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                          transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
         }  
453          stop_transfer_timer();          stop_transfer_timer();
454  }  }
455    
# Line 641  Line 457 
457  void  void
458  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
459                             Bitstream * bs,                             Bitstream * bs,
                            int reduced_resolution,  
460                             int quant,                             int quant,
461                             int intra_dc_threshold)                             int intra_dc_threshold)
462  {  {
463          uint32_t bound;          uint32_t bound;
464          uint32_t x, y;          uint32_t x, y;
         uint32_t mb_width = dec->mb_width;  
         uint32_t mb_height = dec->mb_height;  
   
         if (reduced_resolution)  
         {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
465    
466          bound = 0;          bound = 0;
467    
468          for (y = 0; y < mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
469                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
470                          MACROBLOCK *mb;                          MACROBLOCK *mb;
471                          uint32_t mcbpc;                          uint32_t mcbpc;
472                          uint32_t cbpc;                          uint32_t cbpc;
# Line 672  Line 479 
479    
480                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
481                          {                          {
482                                  bound = read_video_packet_header(bs, dec, 0,                                  bound = read_video_packet_header(bs, 0, &quant);
483                                                          &quant, NULL, NULL, &intra_dc_threshold);                                  x = bound % dec->mb_width;
484                                  x = bound % mb_width;                                  y = bound / dec->mb_width;
                                 y = bound / mb_width;  
485                          }                          }
486                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
487    
# Line 706  Line 512 
512    
513                          if (dec->interlacing) {                          if (dec->interlacing) {
514                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
515                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);
516                          }                          }
517    
518                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
519                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
   
520                  }                  }
521                  if(dec->out_frm)                  if(dec->out_frm)
522                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);
523    
524          }          }
525    
526  }  }
# Line 726  Line 532 
532                                    int x,                                    int x,
533                                    int y,                                    int y,
534                                    int k,                                    int k,
535                                    VECTOR * ret_mv,                                    VECTOR * mv,
536                                    int fcode,                                    int fcode,
537                                    const int bound)                                    const int bound)
538  {  {
# Line 737  Line 543 
543          int range = (64 * scale_fac);          int range = (64 * scale_fac);
544    
545          VECTOR pmv;          VECTOR pmv;
546          VECTOR mv;          int mv_x, mv_y;
547    
548          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
549    
550          mv.x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
551          mv.y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
   
         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);  
552    
553          mv.x += pmv.x;          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);
         mv.y += pmv.y;  
554    
555          if (mv.x < low) {          mv_x += pmv.x;
556                  mv.x += range;          mv_y += pmv.y;
         } else if (mv.x > high) {  
                 mv.x -= range;  
         }  
557    
558          if (mv.y < low) {          if (mv_x < low) {
559                  mv.y += range;                  mv_x += range;
560          } else if (mv.y > high) {          } else if (mv_x > high) {
561                  mv.y -= range;                  mv_x -= range;
562          }          }
563    
564          ret_mv->x = mv.x;          if (mv_y < low) {
565          ret_mv->y = mv.y;                  mv_y += range;
566            } else if (mv_y > high) {
567                    mv_y -= range;
568  }  }
569    
570            mv->x = mv_x;
571            mv->y = mv_y;
572    
   
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
573  }  }
574    
575    
 /* for P_VOP set gmc_warp to NULL */  
576  void  void
577  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
578                             Bitstream * bs,                             Bitstream * bs,
579                             int rounding,                             int rounding,
                            int reduced_resolution,  
580                             int quant,                             int quant,
581                             int fcode,                             int fcode,
582                             int intra_dc_threshold,                             int intra_dc_threshold)
                            const WARPPOINTS *const gmc_warp)  
583  {  {
584    
585          uint32_t x, y;          uint32_t x, y;
586          uint32_t bound;          uint32_t bound;
587          int cp_mb, st_mb;          int cp_mb, st_mb;
         uint32_t mb_width = dec->mb_width;  
         uint32_t mb_height = dec->mb_height;  
   
         static int framecount=0;  
         if (reduced_resolution)  
         {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
588    
589          start_timer();          start_timer();
590          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
591                                     dec->width, dec->height);                                     dec->width, dec->height);
592          stop_edges_timer();          stop_edges_timer();
593    
         if (gmc_warp)  
         {  
                 char filename[80];  
                 sprintf(filename,"dGMC%05d.pgm",framecount);  
                 // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16  
                 if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )  
                 {  
                         fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",  
                                 dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),  
                                 dec->sprite_warping_points);  
                 }  
   
                 generate_GMCparameters( dec->sprite_warping_points,  
                                 (2 << dec->sprite_warping_accuracy), gmc_warp,  
                                 dec->width, dec->height, &dec->gmc_data);  
   
                 generate_GMCimage(&dec->gmc_data, &dec->refn[0],  
                                         mb_width, mb_height,  
                                         dec->edged_width, dec->edged_width/2,  
                                         fcode, 0, 0,  
                                         rounding, dec->mbs, &dec->gmc);  
   
 /*  
                 sprintf(filename,"dGMC%05d.pgm",framecount);  
                 image_dump_yuvpgm(&dec->gmc,  
                                         dec->edged_width, dec->width, dec->height, filename);  
   
                 sprintf(filename,"dREF%05d.pgm",framecount);  
                 image_dump_yuvpgm(&dec->refn[0],  
                                         dec->edged_width, dec->width, dec->height, filename);  
                 sprintf(filename,"dCUR%05d.pgm",framecount);  
                 image_dump_yuvpgm(&dec->cur,  
                                         dec->edged_width, dec->width, dec->height, filename);  
                 framecount++;  
 */  
         }  
   
594          bound = 0;          bound = 0;
595    
596          for (y = 0; y < mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
597                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
598                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
599                          MACROBLOCK *mb;                          MACROBLOCK *mb;
600    
601                          // skip stuffing                          // skip stuffing
# Line 861  Line 604 
604    
605                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
606                          {                          {
607                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
608                                          &quant, &fcode, NULL, &intra_dc_threshold);                                  x = bound % dec->mb_width;
609                                  x = bound % mb_width;                                  y = bound / dec->mb_width;
                                 y = bound / mb_width;  
610                          }                          }
611                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
612    
613                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
614    
615                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
616                          if (!(BitstreamGetBit(bs)))     // block _is_ coded                          if (!(BitstreamGetBit(bs)))     // not_coded
617                          {                          {
618                                  uint32_t mcbpc;                                  uint32_t mcbpc;
619                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 879  Line 621 
621                                  uint32_t cbpy;                                  uint32_t cbpy;
622                                  uint32_t cbp;                                  uint32_t cbp;
623                                  uint32_t intra;                                  uint32_t intra;
                                 int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC  
624    
625                                  cp_mb++;                                  cp_mb++;
626                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 896  Line 637 
637                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
638                                  }                                  }
639    
                                 if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))  
                                 {  
                                         mcsel = BitstreamGetBit(bs);  
                                 }  
   
640                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
641                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
642    
643                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
644    
# Line 922  Line 658 
658                                  if (dec->interlacing) {                                  if (dec->interlacing) {
659                                          if (cbp || intra) {                                          if (cbp || intra) {
660                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
661                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);
662                                          }                                          }
663    
664                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
665                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
666                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);
667    
668                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
669                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
670                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);
671                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
672                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);
673                                                  }                                                  }
674                                          }                                          }
675                                  }                                  }
676    
677                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
678                                            if (dec->interlacing && mb->field_pred) {
                                         if (mcsel)  
                                         {  
                                                 mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;  
                                            /* already clipped to fcode */  
   
                                         } else if (dec->interlacing && mb->field_pred) {  
679                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
680                                                                                    fcode, bound);                                                                                    fcode, bound);
681                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 953  Line 683 
683                                          } else {                                          } else {
684                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
685                                                                                    fcode, bound);                                                                                    fcode, bound);
686                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
687                                                            mb->mvs[0].x;
688                                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
689                                                            mb->mvs[0].y;
690                                          }                                          }
691                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
692    
# Line 968  Line 701 
701                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
702                                                  0;                                                  0;
703                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
704                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
705                                          continue;                                          continue;
706                                  }                                  }
707    
708                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
709                                                                  rounding, reduced_resolution, mcsel);                                                                  rounding);
710                            } else                          // not coded
                         }  
                         else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */  
711                          {                          {
712                                  mb->mode = MODE_NOT_CODED_GMC;                                  DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);
                                 mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;  
   
                                 start_timer();  
   
                                 transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),  
                                                                  dec->gmc.y + (16*y)*dec->edged_width + (16*x),  
                                                                  dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                 dec->gmc.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                 dec->edged_width/2);  
   
                                 transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                  dec->gmc.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                  dec->edged_width/2);  
713    
                                 stop_transfer_timer();  
   
                                 if(dec->out_frm && cp_mb > 0) {  
                                   output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);  
                                   cp_mb = 0;  
                                 }  
                                 st_mb = x+1;  
                         }  
                         else    /* not coded P_VOP macroblock */  
                         {  
714                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
   
715                                  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;
716                                  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;
717    
718                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
719    
720                                  start_timer();                                  start_timer();
721    
722                                  if (reduced_resolution)                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
723                                  {                                                                   (16 * x),
724                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +
725                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),                                                                   (16 * x), dec->edged_width);
726                                                                           dec->edged_width);  
727                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
728                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),                                                                   (16 * x + 8),
729                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +
730                                                                     (16 * x + 8), dec->edged_width);
731    
732                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
733                                                                     (16 * x),
734                                                                     dec->refn[0].y + (16 * y +
735                                                                                                       8) * dec->edged_width +
736                                                                     (16 * x), dec->edged_width);
737    
738                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
739                                                                     (16 * x + 8),
740                                                                     dec->refn[0].y + (16 * y +
741                                                                                                       8) * dec->edged_width +
742                                                                     (16 * x + 8), dec->edged_width);
743    
744                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
745                                                                     (8 * x),
746                                                                     dec->refn[0].u +
747                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
748                                                                          dec->edged_width/2);                                                                          dec->edged_width/2);
749    
750                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
751                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),                                                                   (8 * x),
752                                                                     dec->refn[0].v +
753                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
754                                                                           dec->edged_width/2);                                                                           dec->edged_width/2);
                                 }  
                                 else  
                                 {  
                                         transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->refn[0].y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->edged_width);  
   
                                         transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->edged_width/2);  
                                 }  
   
755                                  stop_transfer_timer();                                  stop_transfer_timer();
   
756                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
757                                    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);
758                                    cp_mb = 0;                                    cp_mb = 0;
# Line 1135  Line 844 
844                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
845                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
846    
847                  if (dec->quarterpel)                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
848                  {                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
849          } else {          } else {
850                  int sum;                  int sum;
851    
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                 else  
852                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
853                    uv_dx =
854                            (sum ==
855                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
856                                                                      (ABS(sum) / 16) * 2));
857    
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                 else  
858                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
859                    uv_dy =
860                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
861                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
862                                                                      (ABS(sum) / 16) * 2));
863          }          }
864    
865          start_timer();          start_timer();
         if(dec->quarterpel) {  
                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                     dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                     pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
         }  
         else {  
866                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
867                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
868                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
869                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
870                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,
871                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
872                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,                                                    0);
873                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
874          }                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
875                                                      0);
876          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
877                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
878          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
# Line 1185  Line 880 
880          stop_comp_timer();          stop_comp_timer();
881    
882          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
883                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
884                  {                  {
885                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
886    
887                          start_timer();                          start_timer();
888                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
889                          stop_coding_timer();                          stop_coding_timer();
890    
891                          start_timer();                          start_timer();
# Line 1230  Line 923 
923          stop_transfer_timer();          stop_transfer_timer();
924  }  }
925    
926    
927  // add by MinChen <chenm001@163.com>  // add by MinChen <chenm001@163.com>
928  // decode an B-frame direct &  inter macroblock  // decode an B-frame direct &  inter macroblock
929  void  void
# Line 1264  Line 958 
958                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
959                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
960    
961                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
962                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
963    
964                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
965                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
966    
967                  if (dec->quarterpel)                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
968                  {                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
                         uv_dx /= 2;  
                         uv_dy /= 2;  
   
                         b_uv_dx /= 2;  
                         b_uv_dy /= 2;  
                 }  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
   
                 b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];  
                 b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];  
969          } else {          } else {
970                  int sum;                  int sum;
971    
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                 else  
972                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
973                    uv_dx =
974                            (sum ==
975                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
976                                                                      (ABS(sum) / 16) * 2));
977    
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                 else  
978                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
979                    uv_dy =
980                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
981                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
982                                                                      (ABS(sum) / 16) * 2));
983                  if(dec->quarterpel)  
984                          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);                  sum =
985                  else                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
986                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                          pMB->b_mvs[3].x;
987                    b_uv_dx =
988                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
989                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
990                  if(dec->quarterpel)                                                                    (ABS(sum) / 16) * 2));
991                          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);  
992                  else                  sum =
993                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                          pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +
994                            pMB->b_mvs[3].y;
995                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  b_uv_dy =
996                            (sum ==
997                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
998                                                                      (ABS(sum) / 16) * 2));
999          }          }
1000    
1001    
1002          start_timer();          start_timer();
         if(dec->quarterpel) {  
                 if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))  
                         interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                             pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
                 else {  
                         interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                             pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,  
                                                                             pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,  
                                                                             pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,  
                                                                             pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);  
                 }  
         }  
         else {  
1003                  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,
1004                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1005                  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,
# Line 1346  Line 1009 
1009                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1010                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1011                                                            0);                                                            0);
         }  
   
1012          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,
1013                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1014          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1015                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1016    
1017    
1018          if(dec->quarterpel) {          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
                 if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))  
                         interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
1019                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1020                  else {          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
                         interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                             pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,  
                                                                             pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,  
                                                                             pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);  
                         interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,  
                                                                             pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);  
                 }  
         }  
         else {  
                 interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,  
                                                           pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);  
                 interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,  
1021                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1022                                                            0);                                                            0);
1023                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
1024                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1025                                                            stride, 0);                                                            stride, 0);
1026                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1027                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1028                                                            stride, 0);                                                            stride, 0);
1029          }          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
   
         interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,  
1030                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1031          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1032                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1033    
1034          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1035                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                           stride);
1036                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1037                                                  stride, 1, 8);                                           stride);
1038            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1039          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           stride);
1040                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1041                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           16 * y_pos + 8, stride);
1042                                                  stride, 1, 8);          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1043                                             stride2);
1044          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1045                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                           stride2);
                                                 dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 1, 8);  
   
1046          stop_comp_timer();          stop_comp_timer();
1047    
1048          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
1049                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1050                  {                  {
1051                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1052    
1053                          start_timer();                          start_timer();
1054                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
1055                          stop_coding_timer();                          stop_coding_timer();
1056    
1057                          start_timer();                          start_timer();
# Line 1550  Line 1168 
1168                          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] =
1169                          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;
1170    
1171                          // skip if the co-located P_VOP macroblock is not coded                          // the last P_VOP is skip macroblock ?
                         // note: gmc+not_coded isn't skipped  
   
1172                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1173                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1174                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1666  Line 1282 
1282                                  break;                                  break;
1283    
1284                          default:                          default:
1285                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type);
1286                          }                          }
1287    
1288                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1691  Line 1307 
1307          *mb2 = temp;          *mb2 = temp;
1308  }  }
1309    
   
 /* perform post processing if necessary, and output the image */  
 void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  
                                         const XVID_DEC_FRAME * frame, int pp_disable)  
 {  
   
         if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */  
         {  
                 /* note: image is stored to tmp */  
                 image_copy(&dec->tmp, img, dec->edged_width, dec->height);  
                 image_deblock_rrv(&dec->tmp, dec->edged_width,  
                                                 mbs, dec->mb_width, dec->mb_height, dec->mb_width,  
                                                 8, frame->general);  
                 img = &dec->tmp;  
         }  
   
         image_output(img, dec->width, dec->height,  
                                  dec->edged_width, frame->image, frame->stride,  
                                  frame->colorspace, dec->interlacing);  
 }  
   
   
1310  int  int
1311  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1312                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame)
1313  {  {
1314    
1315          Bitstream bs;          Bitstream bs;
1316          uint32_t rounding;          uint32_t rounding;
         uint32_t reduced_resolution;  
1317          uint32_t quant;          uint32_t quant;
1318          uint32_t fcode_forward;          uint32_t fcode_forward;
1319          uint32_t fcode_backward;          uint32_t fcode_backward;
1320          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
         WARPPOINTS gmc_warp;  
1321          uint32_t vop_type;          uint32_t vop_type;
         int success = 0;  
         int output = 0;  
         int seen_something = 0;  
1322    
1323          start_global_timer();          start_global_timer();
1324    
         dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);  
1325          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1326    
         if ((frame->general & XVID_DEC_DISCONTINUITY))  
                 dec->frames = 0;  
   
         if (frame->length < 0)  /* decoder flush */  
         {  
                 /* if  not decoding "low_delay/packed", and this isn't low_delay and  
                     we have a reference frame, then outout the reference frame */  
                 if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)  
                 {  
                         decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);  
                         output = 1;  
                 }  
   
                 frame->length = 0;  
                 if (stats)  
                 {  
                         stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;  
                         stats->data.vop.time_base = (int)dec->time_base;  
                         stats->data.vop.time_increment = 0;     //XXX: todo  
                 }  
   
                 emms();  
   
                 stop_global_timer();  
                 return XVID_ERR_OK;  
         }  
   
1327          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1328    
1329          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          // add by chenm001 <chenm001@163.com>
1330          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          // for support B-frame to reference last 2 frame
1331          {          dec->frames++;
1332                  if (stats)          vop_type =
1333                          stats->notify = XVID_DEC_VOP;                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1334                  frame->length = 1;                                                           &fcode_backward, &intra_dc_threshold);
                 image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,  
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
                 emms();  
                 return XVID_ERR_OK;  
         }  
   
 repeat:  
   
         vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
                         &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);  
   
         DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",  
                                                         vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);  
   
         if (vop_type == - 1)  
         {  
                 if (success) goto done;  
                 emms();  
                 return XVID_ERR_FAIL;  
         }  
   
         if (vop_type == -2 || vop_type == -3)  
         {  
                 if (vop_type == -3)  
                         decoder_resize(dec);  
   
                 if (stats)  
                 {  
                         stats->notify = XVID_DEC_VOL;  
                         stats->data.vol.general = 0;  
                         if (dec->interlacing)  
                                 stats->data.vol.general |= XVID_INTERLACING;  
                         stats->data.vol.width = dec->width;  
                         stats->data.vol.height = dec->height;  
                         stats->data.vol.aspect_ratio = dec->aspect_ratio;  
                         stats->data.vol.par_width = dec->par_width;  
                         stats->data.vol.par_height = dec->par_height;  
                         frame->length = BitstreamPos(&bs) / 8;  
                         emms();  
                         return XVID_ERR_OK;  
                 }  
                 goto repeat;  
         }  
1335    
1336          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
1337    
1338            switch (vop_type) {
1339            case P_VOP:
1340                    decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1341                                               intra_dc_threshold);
1342    #ifdef BFRAMES_DEC
1343                    DPRINTF(DPRINTF_DEBUG, "P_VOP  Time=%d", dec->time);
1344    #endif
1345                    break;
1346    
         /* packed_mode: special-N_VOP treament */  
         if (dec->packed_mode && vop_type == N_VOP)  
         {  
                 if (dec->low_delay_default && dec->frames > 0)  
                 {  
                         decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);  
                         output = 1;  
                 }  
                 /* ignore otherwise */  
         }  
         else if (vop_type != B_VOP)  
         {  
                 switch(vop_type)  
                 {  
1347                  case I_VOP :                  case I_VOP :
1348                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1349                          break;  #ifdef BFRAMES_DEC
1350                  case P_VOP :                  DPRINTF(DPRINTF_DEBUG, "I_VOP  Time=%d", dec->time);
1351                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  #endif
                                                 fcode_forward, intra_dc_threshold, NULL);  
1352                          break;                          break;
1353                  case S_VOP :  
1354                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,          case B_VOP:
1355                                                  fcode_forward, intra_dc_threshold, &gmc_warp);  #ifdef BFRAMES_DEC
1356                    if (dec->time_pp > dec->time_bp) {
1357                            DPRINTF(DPRINTF_DEBUG, "B_VOP  Time=%d", dec->time);
1358                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1359                    } else {
1360                            DPRINTF(DPRINTF_DEBUG, "Broken B_VOP");
1361                    }
1362    #else
1363                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1364    #endif
1365                          break;                          break;
1366                  case N_VOP :  
1367            case N_VOP:                             // vop not coded
1368                    // when low_delay==0, N_VOP's should interpolate between the past and future frames
1369                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1370                          break;                          break;
                 }  
1371    
1372                  if (reduced_resolution)          default:
1373                  {                  return XVID_ERR_FAIL;
                         image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,  
                                 (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,  
                                 16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);  
1374                  }                  }
1375    
1376                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */  #ifdef BFRAMES_DEC_DEBUG
1377                  if (!(dec->low_delay_default && dec->packed_mode))          if (frame->length != BitstreamPos(&bs) / 8){
1378                  {                  DPRINTF(DPRINTF_DEBUG, "InLen: %d / UseLen: %d", frame->length, BitstreamPos(&bs) / 8);
                         if (dec->low_delay)  
                         {  
                                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
                                 output = 1;  
                         }  
                         else if (dec->frames > 0)       /* is the reference frame valid? */  
                         {  
                                 /* output the reference frame */  
                                 decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);  
                                 output = 1;  
                         }  
1379                  }                  }
1380    #endif
1381            frame->length = BitstreamPos(&bs) / 8;
1382    
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
                 mb_swap(&dec->mbs, &dec->last_mbs);  
                 dec->last_reduced_resolution = reduced_resolution;  
   
                 dec->frames++;  
                 seen_something = 1;  
   
         }else{  /* B_VOP */  
1383    
1384                  if (dec->low_delay)  #ifdef BFRAMES_DEC
1385                  {          // test if no B_VOP
1386                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");          if (dec->low_delay || dec->frames == 0) {
1387                          dec->low_delay = 1;  #endif
1388                  }          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1389                                             frame->image, frame->stride, frame->colorspace);
1390    
1391                  if (dec->frames < 2)  #ifdef BFRAMES_DEC
                 {  
                         /* attemping to decode a bvop without atleast 2 reference frames */  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,  
                                                 "broken b-frame, mising ref frames");  
                 }else if (dec->time_pp <= dec->time_bp) {  
                         /* this occurs when dx50_bvop_compatibility==0 sequences are  
                         decoded in vfw. */  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,  
                                                 "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);  
1392                  }else{                  }else{
1393                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                  if (dec->frames >= 1) {
1394                  }                          start_timer();
1395                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1396                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);                                  image_output(&dec->refn[0], dec->width, dec->height,
1397                  output = 1;                                                           dec->edged_width, frame->image, frame->stride,
1398                  dec->frames++;                                                           frame->colorspace);
1399          }                          } else if (vop_type == B_VOP) {
1400                                    image_output(&dec->cur, dec->width, dec->height,
1401          BitstreamByteAlign(&bs);                                                           dec->edged_width, frame->image, frame->stride,
1402                                                             frame->colorspace);
         /* low_delay_default mode: repeat in packed_mode */  
         if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)  
         {  
                 success = 1;  
                 goto repeat;  
         }  
   
 done :  
   
         /* low_delay_default mode: if we've gotten here without outputting anything,  
            then output the recently decoded frame, or print an error message  */  
         if (dec->low_delay_default && output == 0)  
         {  
                 if (dec->packed_mode && seen_something)  
                 {  
                         /* output the recently decoded frame */  
                         decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);  
                         output = 1;  
1403                  }                  }
1404                  else                          stop_conv_timer();
                 {  
                         image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,  
                                 "warning: nothing to output");  
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,  
                                 "bframe decoder lag");  
   
                         decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);  
1405                  }                  }
1406          }          }
1407    #endif
1408    
1409          frame->length = BitstreamPos(&bs) / 8;          if (vop_type == I_VOP || vop_type == P_VOP) {
1410                    image_swap(&dec->refn[0], &dec->refn[1]);
1411                    image_swap(&dec->cur, &dec->refn[0]);
1412    
1413          if (stats)                  // swap MACROBLOCK
1414          {                  // the Divx will not set the low_delay flage some times
1415                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;                  // so follow code will wrong to not swap at that time
1416                  stats->data.vop.time_base = (int)dec->time_base;                  // this will broken bitstream! so I'm change it,
1417                  stats->data.vop.time_increment = 0;     //XXX: todo                  // But that is not the best way! can anyone tell me how
1418                    // to do another way?
1419                    // 18-07-2002   MinChen<chenm001@163.com>
1420                    //if (!dec->low_delay && vop_type == P_VOP)
1421                    if (vop_type == P_VOP)
1422                            mb_swap(&dec->mbs, &dec->last_mbs);
1423          }          }
1424    
1425          emms();          emms();

Legend:
Removed from v.1.37.2.27  
changed lines
  Added in v.1.42

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