[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.23, Mon Dec 16 08:54:44 2002 UTC revision 1.45, Sun Dec 15 01:21:12 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   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002 MinChen <chenm001@163.com>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002 Peter Ross <pross@xvid.org>
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
10   *  it under the terms of the GNU General Public License as published by   *
11     *  XviD is free software; you can redistribute it and/or modify it
12     *  under the terms of the GNU General Public License as published by
13   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
14   *  (at your option) any later version.   *  (at your option) any later version.
15   *   *
# Line 26  Line 22 
22   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
23   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
24   *   *
25   *************************************************************************/   *  Under section 8 of the GNU General Public License, the copyright
26     *  holders of XVID explicitly forbid distribution in the following
27  /**************************************************************************   *  countries:
28   *   *
29   *  History:   *    - Japan
30     *    - United States of America
31   *   *
32   *  15.07.2002  fix a bug in B-frame decode at DIRECT mode   *  Linking XviD statically or dynamically with other modules is making a
33   *              MinChen <chenm001@163.com>   *  combined work based on XviD.  Thus, the terms and conditions of the
34   *  10.07.2002  added BFRAMES_DEC_DEBUG support   *  GNU General Public License cover the whole combination.
35   *              Fix a little bug for low_delay flage   *
36   *              MinChen <chenm001@163.com>   *  As a special exception, the copyright holders of XviD give you
37   *  28.06.2002  added basic resync support to iframe/pframe_decode()   *  permission to link XviD with independent modules that communicate with
38   *  22.06.2002  added primative N_VOP support   *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the
39   *                              #define BFRAMES_DEC now enables Minchen's bframe decoder   *  license terms of these independent modules, and to copy and distribute
40   *  08.05.2002  add low_delay support for B_VOP decode   *  the resulting combined work under terms of your choice, provided that
41   *              MinChen <chenm001@163.com>   *  every copy of the combined work is accompanied by a complete copy of
42   *  05.05.2002  fix some B-frame decode problem   *  the source code of XviD (the version of XviD used to produce the
43   *  02.05.2002  add B-frame decode support(have some problem);   *  combined work), being distributed under the terms of the GNU General
44   *              MinChen <chenm001@163.com>   *  Public License plus this exception.  An independent module is a module
45   *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>   *  which is not derived from or based on XviD.
46   *  29.03.2002  interlacing fix - compensated block wasn't being used when   *
47   *              reconstructing blocks, thus artifacts   *  Note that people who make modified versions of XviD are not obligated
48   *              interlacing speedup - used transfers to re-interlace   *  to grant this special exception for their modified versions; it is
49   *              interlaced decoding should be as fast as progressive now   *  their choice whether to do so.  The GNU General Public License gives
50   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  permission to release a modified version without this exception; this
51   *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block   *  exception also makes it possible to release a modified version which
52   *  22.12.2001  lock based interpolation   *  carries forward this exception.
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
53   *   *
54   *  $Id$   *  $Id$
55   *   *
# Line 79  Line 75 
75  #include "dct/fdct.h"  #include "dct/fdct.h"
76  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
77  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "image/reduced.h"  
 #include "image/font.h"  
78    
79  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
80  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
81  #include "utils/timer.h"  #include "utils/timer.h"
82  #include "utils/emms.h"  #include "utils/emms.h"
 #include "motion/motion.h"  
83    
84  #include "image/image.h"  #include "image/image.h"
85  #include "image/colorspace.h"  #include "image/colorspace.h"
86  #include "utils/mem_align.h"  #include "utils/mem_align.h"
87    
88  int  int
89  decoder_resize(DECODER * dec)  decoder_create(XVID_DEC_PARAM * param)
90  {  {
91          /* 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);  
92    
93          if (dec->last_mbs)          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
94                  xvid_free(dec->last_mbs);          if (dec == NULL) {
95          if (dec->mbs)                  return XVID_ERR_MEMORY;
96                  xvid_free(dec->mbs);          }
97            param->handle = dec;
98    
99          /* realloc */          dec->width = param->width;
100            dec->height = param->height;
101    
102          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
103          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
104    
105          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
106          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
107            dec->low_delay = 0;
108    
109          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
110                  xvid_free(dec);                  xvid_free(dec);
# Line 126  Line 116 
116                  xvid_free(dec);                  xvid_free(dec);
117                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
118          }          }
119            /* add by chenm001 <chenm001@163.com> */
120          // add by chenm001 <chenm001@163.com>          /* for support B-frame to reference last 2 frame */
         // for support B-frame to reference last 2 frame  
121          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
122                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
124                  xvid_free(dec);                  xvid_free(dec);
125                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
126          }          }
127          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
128                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
129                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
130                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 143  Line 132 
132                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
133          }          }
134    
         if (image_create(&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);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
135          dec->mbs =          dec->mbs =
136                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
137                                          CACHE_LINE);                                          CACHE_LINE);
# Line 159  Line 139 
139                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
140                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
141                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
142                  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);  
143                  xvid_free(dec);                  xvid_free(dec);
144                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
145          }          }
146    
147          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
148    
149          // add by chenm001 <chenm001@163.com>          /* add by chenm001 <chenm001@163.com> */
150          // for skip MB flag          /* for skip MB flag */
151          dec->last_mbs =          dec->last_mbs =
152                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
153                                          CACHE_LINE);                                          CACHE_LINE);
# Line 176  Line 156 
156                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
157                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
158                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
159                  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);  
160                  xvid_free(dec);                  xvid_free(dec);
161                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
162          }          }
163    
164          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);
165    
         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);  
   
         dec->mbs = NULL;  
         dec->last_mbs = NULL;  
   
166          init_timer();          init_timer();
167    
168          // add by chenm001 <chenm001@163.com>          /* add by chenm001 <chenm001@163.com> */
169          // for support B-frame to save reference frame's time          /* for support B-frame to save reference frame's time */
170          dec->frames = 0;          dec->frames = -1;
171          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;  
   
         dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);  
172    
         if (dec->fixed_dimensions)  
                 return decoder_resize(dec);  
         else  
173                  return XVID_ERR_OK;                  return XVID_ERR_OK;
174  }  }
175    
# Line 238  Line 181 
181          xvid_free(dec->mbs);          xvid_free(dec->mbs);
182          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
183          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
184          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);  
185          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
186          xvid_free(dec);          xvid_free(dec);
187    
# Line 256  Line 198 
198    
199    
200    
201  // decode an intra macroblock  /* decode an intra macroblock */
202    
203  void  void
204  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
# Line 268  Line 210 
210                                  Bitstream * bs,                                  Bitstream * bs,
211                                  const uint32_t quant,                                  const uint32_t quant,
212                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
213                                  const unsigned int bound,                                  const unsigned int bound)
                                 const int reduced_resolution)  
214  {  {
215    
216          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 282  Line 223 
223          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
224          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
225    
         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{  
226                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
227                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
228                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
         }  
229    
230          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
231    
232          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
233                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 315  Line 250 
250                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
251    
252                          if (dc_size > 8) {                          if (dc_size > 8) {
253                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
254                          }                          }
255    
256                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
# Line 327  Line 262 
262                  }                  }
263    
264                  start_timer();                  start_timer();
265                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
266                  {                  {
267                          int direction = dec->alternate_vertical_scan ?                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],
268                                  2 : pMB->acpred_directions[i];                                                          start_coeff);
   
                         get_intra_block(bs, &block[i * 64], direction, start_coeff);  
269                  }                  }
270                  stop_coding_timer();                  stop_coding_timer();
271    
# Line 351  Line 284 
284                  start_timer();                  start_timer();
285                  idct(&data[i * 64]);                  idct(&data[i * 64]);
286                  stop_idct_timer();                  stop_idct_timer();
   
287          }          }
288    
289          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 360  Line 292 
292          }          }
293    
294          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{  
295                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
296                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
297                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
298                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
299                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
300                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
         }  
301          stop_transfer_timer();          stop_transfer_timer();
302  }  }
303    
# Line 387  Line 307 
307    
308  #define SIGN(X) (((X)>0)?1:-1)  #define SIGN(X) (((X)>0)?1:-1)
309  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
310    static const uint32_t roundtab[16] =
311            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
312    
313    
314  // decode an inter macroblock  /* decode an inter macroblock */
315    
316  void  void
317  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
# Line 399  Line 322 
322                                  const uint32_t cbp,                                  const uint32_t cbp,
323                                  Bitstream * bs,                                  Bitstream * bs,
324                                  const uint32_t quant,                                  const uint32_t quant,
325                                  const uint32_t rounding,                                  const uint32_t rounding)
                                 const int reduced_resolution)  
326  {  {
327    
328          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 408  Line 330 
330    
331          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
332          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
333          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);          uint32_t next_block = stride * 8;
334          uint32_t i;          uint32_t i;
335          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
336          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
   
337          int uv_dx, uv_dy;          int uv_dx, uv_dy;
         VECTOR mv[4];   /* local copy of mvs */  
338    
         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{  
339                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
340                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
341                  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];  
         }  
342    
343          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
344                  uv_dx = mv[0].x;                  uv_dx = pMB->mvs[0].x;
345                  uv_dy = mv[0].y;                  uv_dy = pMB->mvs[0].y;
346    
347                  if (dec->quarterpel)                  if (dec->quarterpel)
348                  {                  {
349                          uv_dx /= 2;                          uv_dx = (uv_dx >> 1) | (uv_dx & 1);
350                          uv_dy /= 2;                          uv_dy = (uv_dy >> 1) | (uv_dy & 1);
351                  }                  }
352    
353                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
354                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
355            } else {
356                  start_timer();                  int sum;
357                  if (reduced_resolution)                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 {  
                         interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,  
                                                                   mv[0].x, mv[0].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
358    
359                  }                  if (dec->quarterpel)
                 else  
360                  {                  {
361                          if(dec->quarterpel) {                          sum /= 2;
                                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                                         dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                                         mv[0].x, mv[0].y, stride,  rounding);  
                         }  
                         else {  
                                 interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,  
                                                                           mv[0].x, mv[0].y, stride,  rounding);  
                         }  
   
                         interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
362                  }                  }
                 stop_comp_timer();  
   
         } else {        /* MODE_INTER4V */  
                 int sum;  
363    
364                  if(dec->quarterpel)                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
                         sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);  
                 else  
                         sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
365    
366                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
367    
368                  if(dec->quarterpel)                  if(dec->quarterpel)
                         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;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 start_timer();  
                 if (reduced_resolution)  
369                  {                  {
370                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          sum /= 2;
371                                                                    mv[0].x, mv[0].y, stride,  rounding);                  }
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos,  
                                                                   mv[1].x, mv[1].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16,  
                                                                   mv[2].x, mv[2].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16,  
                                                                   mv[3].x, mv[3].y, stride,  rounding);  
                         interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
372    
373                          // set_block(pY_Cur, stride, 32, 32, 127);                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));
374                  }                  }
375                  else  
376                  {          start_timer();
377                          if(dec->quarterpel) {                          if(dec->quarterpel) {
378                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,                  DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n");
379                                                                                    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,
380                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
381                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
382                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
383                                                                                    mv[1].x, mv[1].y, stride,  rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
384                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
385                                                                                    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,
386                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
                                 interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 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);  
387                          }                          }
388                          else {                          else {
389                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
390                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
391                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
392                                                                            mv[1].x, mv[1].y, stride,  rounding);                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
393                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
394                                                                            mv[2].x, mv[2].y, stride,  rounding);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
395                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
396                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
397                          }                          }
398    
399                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
400                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
401                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
402                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
                 }  
403                  stop_comp_timer();                  stop_comp_timer();
         }  
404    
405          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
406                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  if (cbp & (1 << (5 - i)))       /* coded */
   
                 if (cbp & (1 << (5 - i)))       // coded  
407                  {                  {
408                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
409    
410                          start_timer();                          start_timer();
411                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
412                          stop_coding_timer();                          stop_coding_timer();
413    
414                          start_timer();                          start_timer();
# Line 576  Line 431 
431          }          }
432    
433          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  
         {  
434                  if (cbp & 32)                  if (cbp & 32)
435                          transfer_16to8add(pY_Cur, &data[0 * 64], stride);                          transfer_16to8add(pY_Cur, &data[0 * 64], stride);
436                  if (cbp & 16)                  if (cbp & 16)
# Line 605  Line 443 
443                          transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                          transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
444                  if (cbp & 1)                  if (cbp & 1)
445                          transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                          transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
         }  
446          stop_transfer_timer();          stop_transfer_timer();
447  }  }
448    
# Line 613  Line 450 
450  void  void
451  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
452                             Bitstream * bs,                             Bitstream * bs,
                            int reduced_resolution,  
453                             int quant,                             int quant,
454                             int intra_dc_threshold)                             int intra_dc_threshold)
455  {  {
456          uint32_t bound;          uint32_t bound;
457          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;  
         }  
458    
459          bound = 0;          bound = 0;
460    
461          for (y = 0; y < mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
462                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
463                          MACROBLOCK *mb;                          MACROBLOCK *mb;
464                          uint32_t mcbpc;                          uint32_t mcbpc;
465                          uint32_t cbpc;                          uint32_t cbpc;
# Line 644  Line 472 
472    
473                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
474                          {                          {
475                                  bound = read_video_packet_header(bs, dec, 0,                                  bound = read_video_packet_header(bs, 0, &quant);
476                                                          &quant, NULL, NULL, &intra_dc_threshold);                                  x = bound % dec->mb_width;
477                                  x = bound % mb_width;                                  y = bound / dec->mb_width;
                                 y = bound / mb_width;  
478                          }                          }
479                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
480    
# Line 678  Line 505 
505    
506                          if (dec->interlacing) {                          if (dec->interlacing) {
507                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
508                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);
509                          }                          }
510    
511                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
512                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
   
513                  }                  }
514                  if(dec->out_frm)                  if(dec->out_frm)
515                    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);
516    
517          }          }
518    
519  }  }
# Line 698  Line 525 
525                                    int x,                                    int x,
526                                    int y,                                    int y,
527                                    int k,                                    int k,
528                                    VECTOR * ret_mv,                                    VECTOR * mv,
529                                    int fcode,                                    int fcode,
530                                    const int bound)                                    const int bound)
531  {  {
# Line 709  Line 536 
536          int range = (64 * scale_fac);          int range = (64 * scale_fac);
537    
538          VECTOR pmv;          VECTOR pmv;
539          VECTOR mv;          int mv_x, mv_y;
540    
541          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
542    
543          mv.x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
544          mv.y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
   
         DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);  
545    
546          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;  
547    
548          if (mv.x < low) {          mv_x += pmv.x;
549                  mv.x += range;          mv_y += pmv.y;
         } else if (mv.x > high) {  
                 mv.x -= range;  
         }  
550    
551          if (mv.y < low) {          if (mv_x < low) {
552                  mv.y += range;                  mv_x += range;
553          } else if (mv.y > high) {          } else if (mv_x > high) {
554                  mv.y -= range;                  mv_x -= range;
555          }          }
556    
557          ret_mv->x = mv.x;          if (mv_y < low) {
558          ret_mv->y = mv.y;                  mv_y += range;
559            } else if (mv_y > high) {
560                    mv_y -= range;
561  }  }
562    
563            mv->x = mv_x;
564            mv->y = mv_y;
565    
   
 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;  
566  }  }
567    
568    
 /* for P_VOP set gmc_mv to NULL */  
569  void  void
570  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
571                             Bitstream * bs,                             Bitstream * bs,
572                             int rounding,                             int rounding,
                            int reduced_resolution,  
573                             int quant,                             int quant,
574                             int fcode,                             int fcode,
575                             int intra_dc_threshold,                             int intra_dc_threshold)
                            VECTOR * gmc_mv)  
576  {  {
577    
578          uint32_t x, y;          uint32_t x, y;
579          uint32_t bound;          uint32_t bound;
580          int cp_mb, st_mb;          int cp_mb, st_mb;
         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;  
         }  
581    
582          start_timer();          start_timer();
583          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
# Line 784  Line 586 
586    
587          bound = 0;          bound = 0;
588    
589          for (y = 0; y < mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
590                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
591                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
592                          MACROBLOCK *mb;                          MACROBLOCK *mb;
593    
594                          // skip stuffing                          /* skip stuffing */
595                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
596                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
597    
598                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
599                          {                          {
600                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
601                                          &quant, &fcode, NULL, &intra_dc_threshold);                                  x = bound % dec->mb_width;
602                                  x = bound % mb_width;                                  y = bound / dec->mb_width;
                                 y = bound / mb_width;  
603                          }                          }
604                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
605    
606                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
607    
608                          //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 */
609                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* not_coded */
610                          {                          {
611                                  uint32_t mcbpc;                                  uint32_t mcbpc;
612                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 813  Line 614 
614                                  uint32_t cbpy;                                  uint32_t cbpy;
615                                  uint32_t cbp;                                  uint32_t cbp;
616                                  uint32_t intra;                                  uint32_t intra;
                                 int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC  
617    
618                                  cp_mb++;                                  cp_mb++;
619                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 830  Line 630 
630                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
631                                  }                                  }
632    
                                 if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))  
                                 {  
                                         mcsel = BitstreamGetBit(bs);  
                                 }  
   
633                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
634                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
635    
# Line 856  Line 651 
651                                  if (dec->interlacing) {                                  if (dec->interlacing) {
652                                          if (cbp || intra) {                                          if (cbp || intra) {
653                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
654                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);
655                                          }                                          }
656    
657                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
658                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
659                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);
660    
661                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
662                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
663                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);
664                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
665                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);
666                                                  }                                                  }
667                                          }                                          }
668                                  }                                  }
669    
670                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
671                                            if (dec->interlacing && mb->field_pred) {
                                         if (mcsel)  
                                         {  
                                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
                                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
   
                                         } else if (dec->interlacing && mb->field_pred) {  
672                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
673                                                                                    fcode, bound);                                                                                    fcode, bound);
674                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 898  Line 687 
687                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
688                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
689                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
690                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
691                                  {                                  {
692                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
693                                                  0;                                                  0;
694                                          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 =
695                                                  0;                                                  0;
696                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
697                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
698                                          continue;                                          continue;
699                                  }                                  }
700    
701                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
702                                                                  rounding, reduced_resolution);                                                                  rounding);
703                            } else                          /* not coded */
                         }  
                         else if (gmc_mv)        /* not coded S_VOP macroblock */  
704                          {                          {
705                                  mb->mode = MODE_NOT_CODED;                                  DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);
                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
                                 decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
                         }  
                         else    /* not coded P_VOP macroblock */  
                         {  
                                 mb->mode = MODE_NOT_CODED;  
706    
707                                    mb->mode = MODE_NOT_CODED;
708                                  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;
709                                  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;
                                 // copy macroblock directly from ref to cur  
710    
711                                  start_timer();                                  /* copy macroblock directly from ref to cur */
   
                                 if (reduced_resolution)  
                                 {  
                                         transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->refn[0].y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->edged_width);  
712    
713                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),                                  start_timer();
                                                                         dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          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);  
714    
715                                          transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
716                                                                          dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),                                                                   (16 * x),
717                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
718                                                                     (16 * x), dec->edged_width);
719    
720                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
721                                                                     (16 * x + 8),
722                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
723                                                                     (16 * x + 8), dec->edged_width);
724    
725                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
726                                                                     (16 * x),
727                                                                     dec->refn[0].y + (16 * y +
728                                                                                                       8) * dec->edged_width +
729                                                                     (16 * x), dec->edged_width);
730    
731                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
732                                                                     (16 * x + 8),
733                                                                     dec->refn[0].y + (16 * y +
734                                                                                                       8) * dec->edged_width +
735                                                                     (16 * x + 8), dec->edged_width);
736    
737                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
738                                                                     (8 * x),
739                                                                     dec->refn[0].u +
740                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
741                                                                          dec->edged_width/2);                                                                          dec->edged_width/2);
742    
743                                          transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
744                                                                           dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),                                                                   (8 * x),
745                                                                     dec->refn[0].v +
746                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
747                                                                           dec->edged_width/2);                                                                           dec->edged_width/2);
                                 }  
   
748                                  stop_transfer_timer();                                  stop_transfer_timer();
   
749                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
750                                    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);
751                                    cp_mb = 0;                                    cp_mb = 0;
# Line 974  Line 759 
759  }  }
760    
761    
762  // add by MinChen <chenm001@163.com>  /* add by MinChen <chenm001@163.com> */
763  // decode B-frame motion vector  /* decode B-frame motion vector */
764  void  void
765  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
766                                          Bitstream * bs,                                          Bitstream * bs,
# Line 1019  Line 804 
804  }  }
805    
806    
807  // add by MinChen <chenm001@163.com>  /* add by MinChen <chenm001@163.com> */
808  // decode an B-frame forward & backward inter macroblock  /* decode an B-frame forward & backward inter macroblock */
809  void  void
810  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
811                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 1052  Line 837 
837                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
838                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
839    
840                  if (dec->quarterpel)                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
841                  {                  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];  
842          } else {          } else {
843                  int sum;                  int sum;
844    
                 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  
845                          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;
846                    uv_dx =
847                            (sum ==
848                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
849                                                                      (ABS(sum) / 16) * 2));
850    
                 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  
851                          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;
852                    uv_dy =
853                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
854                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
855                                                                      (ABS(sum) / 16) * 2));
856          }          }
857    
858          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 {  
859                  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,
860                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
861                  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,
862                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
863                  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,
864                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
865                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,                                                    0);
866                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
867          }                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
868                                                      0);
869          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,
870                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
871          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 1102  Line 873 
873          stop_comp_timer();          stop_comp_timer();
874    
875          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
876                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  if (cbp & (1 << (5 - i)))       /* coded */
   
                 if (cbp & (1 << (5 - i)))       // coded  
877                  {                  {
878                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
879    
880                          start_timer();                          start_timer();
881                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
882                          stop_coding_timer();                          stop_coding_timer();
883    
884                          start_timer();                          start_timer();
# Line 1147  Line 916 
916          stop_transfer_timer();          stop_transfer_timer();
917  }  }
918    
919  // add by MinChen <chenm001@163.com>  
920  // decode an B-frame direct &  inter macroblock  /* add by MinChen <chenm001@163.com> */
921    /* decode an B-frame direct &  inter macroblock */
922  void  void
923  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
924                                                             IMAGE forward,                                                             IMAGE forward,
# Line 1181  Line 951 
951                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
952                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
953    
954                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
955                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
956    
957                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
958                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
959    
960                  if (dec->quarterpel)                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
961                  {                  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];  
962          } else {          } else {
963                  int sum;                  int sum;
964    
                 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  
965                          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;
966                    uv_dx =
967                            (sum ==
968                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
969                                                                      (ABS(sum) / 16) * 2));
970    
                 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  
971                          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;
972                    uv_dy =
973                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
974                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
975                                                                      (ABS(sum) / 16) * 2));
976                  if(dec->quarterpel)  
977                          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 =
978                  else                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
979                          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;
980                    b_uv_dx =
981                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                          (sum ==
982                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
983                  if(dec->quarterpel)                                                                    (ABS(sum) / 16) * 2));
984                          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);  
985                  else                  sum =
986                          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 +
987                            pMB->b_mvs[3].y;
988                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  b_uv_dy =
989                            (sum ==
990                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
991                                                                      (ABS(sum) / 16) * 2));
992          }          }
993    
994    
995          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 {  
996                  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,
997                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
998                  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 1263  Line 1002 
1002                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1003                                                            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,
1004                                                            0);                                                            0);
         }  
   
1005          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,
1006                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1007          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,
1008                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1009    
1010    
1011          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,  
                                                                             pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);  
                 else {  
                         interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                             dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
1012                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1013                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
                                                                             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,  
1014                                                            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,
1015                                                            0);                                                            0);
1016                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
1017                                                            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,
1018                                                            stride, 0);                                                            stride, 0);
1019                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1020                                                            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,
1021                                                            stride, 0);                                                            stride, 0);
1022          }          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,  
1023                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1024          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,
1025                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1026    
1027          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,
1028                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                           stride);
1029                                                  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,
1030                                                  stride, 1, 8);                                           stride);
1031            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1032          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           stride);
1033                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1034                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                           16 * y_pos + 8, stride);
1035                                                  stride, 1, 8);          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1036                                             stride2);
1037          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,
1038                                                  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);  
   
1039          stop_comp_timer();          stop_comp_timer();
1040    
1041          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1042                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  if (cbp & (1 << (5 - i)))       /* coded */
   
                 if (cbp & (1 << (5 - i)))       // coded  
1043                  {                  {
1044                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1045    
1046                          start_timer();                          start_timer();
1047                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64]);
1048                          stop_coding_timer();                          stop_coding_timer();
1049    
1050                          start_timer();                          start_timer();
# Line 1389  Line 1083 
1083  }  }
1084    
1085    
1086  // add by MinChen <chenm001@163.com>  /* add by MinChen <chenm001@163.com> */
1087  // for decode B-frame dbquant  /* for decode B-frame dbquant */
1088  int32_t __inline  int32_t __inline
1089  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1090  {  {
1091          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))       /* '0' */
1092                  return (0);                  return (0);
1093          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1094                  return (-2);                  return (-2);
1095          else          else
1096                  return (2);                             // '11'                  return (2);                             /* '11' */
1097  }  }
1098    
1099  // add by MinChen <chenm001@163.com>  /* add by MinChen <chenm001@163.com> */
1100  // for decode B-frame mb_type  /* for decode B-frame mb_type */
1101  // bit   ret_value  /* bit   ret_value */
1102  // 1        0  /* 1        0 */
1103  // 01       1  /* 01       1 */
1104  // 001      2  /* 001      2 */
1105  // 0001     3  /* 0001     3 */
1106  int32_t __inline  int32_t __inline
1107  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1108  {  {
# Line 1457  Line 1151 
1151  #endif  #endif
1152    
1153          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1154                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1155                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1156                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1157                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1467  Line 1161 
1161                          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] =
1162                          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;
1163    
1164                          // the last P_VOP is skip macroblock ?                          /* the last P_VOP is skip macroblock ? */
1165                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1166                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /*DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1167                                  mb->cbp = 0;                                  mb->cbp = 0;
1168  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1169                                  mb->mb_type = MODE_NOT_CODED;                                  mb->mb_type = MODE_NOT_CODED;
# Line 1477  Line 1171 
1171  #endif  #endif
1172                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1173                                  mb->quant = last_mb->quant;                                  mb->quant = last_mb->quant;
1174                                  //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  /*mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; */
1175                                  //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 = mb->mvs[0].y; */
1176    
1177                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1178                                  continue;                                  continue;
1179                          }                          }
1180    
1181                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1182                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = (uint8_t)BitstreamGetBit(bs);
1183    
1184                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1185    
1186                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1187                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1188                                  } else {                                  } else {
1189                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1510  Line 1204 
1204    
1205                          mb->quant = quant;                          mb->quant = quant;
1206                          mb->mode = MODE_INTER4V;                          mb->mode = MODE_INTER4V;
1207                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /*DEBUG1("Switch bm_type=",mb->mb_type); */
1208    
1209  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1210          BFRAME_DEBUG          BFRAME_DEBUG
# Line 1539  Line 1233 
1233                                                                                    / TRD                                                                                    / TRD
1234                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1235                                          }                                          }
1236                                          //DEBUG("B-frame Direct!\n");                                          /*DEBUG("B-frame Direct!\n"); */
1237                                  }                                  }
1238                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1239                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
# Line 1557  Line 1251 
1251    
1252                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1253                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
1254                                  //DEBUG("B-frame Bidir!\n");                                  /*DEBUG("B-frame Bidir!\n"); */
1255                                  break;                                  break;
1256    
1257                          case MODE_BACKWARD:                          case MODE_BACKWARD:
# Line 1567  Line 1261 
1261    
1262                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1263                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1264                                  //DEBUG("B-frame Backward!\n");                                  /*DEBUG("B-frame Backward!\n"); */
1265                                  break;                                  break;
1266    
1267                          case MODE_FORWARD:                          case MODE_FORWARD:
# Line 1577  Line 1271 
1271    
1272                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1273                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1274                                  //DEBUG("B-frame Forward!\n");                                  /*DEBUG("B-frame Forward!\n"); */
1275                                  break;                                  break;
1276    
1277                          default:                          default:
1278                                  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);
1279                          }                          }
1280    
1281                  }                                               // end of FOR                  }                                               /* end of FOR */
1282          }          }
1283  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1284          if (!first){          if (!first){
# Line 1595  Line 1289 
1289  #endif  #endif
1290  }  }
1291    
1292  // swap two MACROBLOCK array  /* swap two MACROBLOCK array */
1293  void  void
1294  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
1295                  MACROBLOCK ** mb2)                  MACROBLOCK ** mb2)
# Line 1606  Line 1300 
1300          *mb2 = temp;          *mb2 = temp;
1301  }  }
1302    
   
 /* 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);  
 }  
   
   
1303  int  int
1304  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1305                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame)
1306  {  {
1307    
1308          Bitstream bs;          Bitstream bs;
1309          uint32_t rounding;          uint32_t rounding;
         uint32_t reduced_resolution;  
1310          uint32_t quant;          uint32_t quant;
1311          uint32_t fcode_forward;          uint32_t fcode_forward;
1312          uint32_t fcode_backward;          uint32_t fcode_backward;
1313          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
         VECTOR gmc_mv[5];  
1314          uint32_t vop_type;          uint32_t vop_type;
         int success = 0;  
         int output = 0;  
         int seen_something = 0;  
1315    
1316          start_global_timer();          start_global_timer();
1317    
         dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);  
1318          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1319    
         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;  
         }  
   
1320          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1321    
1322          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* add by chenm001 <chenm001@163.com> */
1323          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          /* for support B-frame to reference last 2 frame */
1324          {          dec->frames++;
1325                  if (stats)          vop_type =
1326                          stats->notify = XVID_DEC_VOP;                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1327                  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_mv);  
   
         DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  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;  
                 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;  
                         return XVID_ERR_OK;  
                 }  
                 goto repeat;  
         }  
1328    
1329          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 */
1330    
1331            switch (vop_type) {
1332            case P_VOP:
1333                    decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1334                                               intra_dc_threshold);
1335    #ifdef BFRAMES_DEC
1336                    DPRINTF(DPRINTF_DEBUG, "P_VOP  Time=%d", dec->time);
1337    #endif
1338                    break;
1339    
         /* 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)  
                 {  
1340                  case I_VOP :                  case I_VOP :
1341                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1342                          break;  #ifdef BFRAMES_DEC
1343                  case P_VOP :                  DPRINTF(DPRINTF_DEBUG, "I_VOP  Time=%d", dec->time);
1344                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  #endif
                                                 fcode_forward, intra_dc_threshold, NULL);  
1345                          break;                          break;
1346                  case S_VOP :  
1347                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,          case B_VOP:
1348                                                  fcode_forward, intra_dc_threshold, gmc_mv);  #ifdef BFRAMES_DEC
1349                    if (dec->time_pp > dec->time_bp) {
1350                            DPRINTF(DPRINTF_DEBUG, "B_VOP  Time=%d", dec->time);
1351                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1352                    } else {
1353                            DPRINTF(DPRINTF_DEBUG, "Broken B_VOP");
1354                    }
1355    #else
1356                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1357    #endif
1358                          break;                          break;
1359                  case N_VOP :  
1360            case N_VOP:                             /* vop not coded */
1361                    /* when low_delay==0, N_VOP's should interpolate between the past and future frames */
1362                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1363                          break;                          break;
                 }  
1364    
1365                  if (reduced_resolution)          default:
1366                  {                  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);  
1367                  }                  }
1368    
1369                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */  #ifdef BFRAMES_DEC_DEBUG
1370                  if (!(dec->low_delay_default && dec->packed_mode))          if (frame->length != BitstreamPos(&bs) / 8){
1371                  {                  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;  
                         }  
1372                  }                  }
1373    #endif
1374            frame->length = BitstreamPos(&bs) / 8;
1375    
                 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 */  
1376    
1377                  if (dec->low_delay)  #ifdef BFRAMES_DEC
1378                  {          /* test if no B_VOP */
1379                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");          if (dec->low_delay || dec->frames == 0) {
1380                          dec->low_delay = 1;  #endif
1381                  }          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1382                                             frame->image, frame->stride, frame->colorspace);
1383    
1384                  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);  
1385                  }else{                  }else{
1386                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                  if (dec->frames >= 1) {
1387                  }                          start_timer();
1388                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1389                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);                                  image_output(&dec->refn[0], dec->width, dec->height,
1390                  output = 1;                                                           dec->edged_width, frame->image, frame->stride,
1391                  dec->frames++;                                                           frame->colorspace);
1392          }                          } else if (vop_type == B_VOP) {
1393                                    image_output(&dec->cur, dec->width, dec->height,
1394          BitstreamByteAlign(&bs);                                                           dec->edged_width, frame->image, frame->stride,
1395                                                             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;  
1396                  }                  }
1397                  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*/);  
1398                  }                  }
1399          }          }
1400    #endif
1401    
1402          frame->length = BitstreamPos(&bs) / 8;          if (vop_type == I_VOP || vop_type == P_VOP) {
1403                    image_swap(&dec->refn[0], &dec->refn[1]);
1404                    image_swap(&dec->cur, &dec->refn[0]);
1405    
1406          if (stats)                  /* swap MACROBLOCK */
1407          {                  /* the Divx will not set the low_delay flage some times */
1408                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;                  /* so follow code will wrong to not swap at that time */
1409                  stats->data.vop.time_base = (int)dec->time_base;                  /* this will broken bitstream! so I'm change it, */
1410                  stats->data.vop.time_increment = 0;     //XXX: todo                  /* But that is not the best way! can anyone tell me how */
1411                    /* to do another way? */
1412                    /* 18-07-2002   MinChen<chenm001@163.com> */
1413                    /*if (!dec->low_delay && vop_type == P_VOP) */
1414                    if (vop_type == P_VOP)
1415                            mb_swap(&dec->mbs, &dec->last_mbs);
1416          }          }
1417    
1418          emms();          emms();

Legend:
Removed from v.1.37.2.23  
changed lines
  Added in v.1.45

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