[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.47, Sat Feb 15 15:22:17 2003 UTC revision 1.72, Mon Aug 1 18:37:46 2005 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder 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-2004 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 program is free software; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  15.07.2002  fix a bug in B-frame decode at DIRECT mode  
  *              MinChen <chenm001@163.com>  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              Fix a little bug for low_delay flage  
  *              MinChen <chenm001@163.com>  
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
  *  22.06.2002  added primative N_VOP support  
  *                              #define BFRAMES_DEC now enables Minchen's bframe decoder  
  *  08.05.2002  add low_delay support for B_VOP decode  
  *              MinChen <chenm001@163.com>  
  *  05.05.2002  fix some B-frame decode problem  
  *  02.05.2002  add B-frame decode support(have some problem);  
  *              MinChen <chenm001@163.com>  
  *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  26.03.2002  interlacing support - moved transfers outside decode loop  
  *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block  
  *  22.12.2001  lock based interpolation  
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27  #include <stdio.h>  #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
# Line 75  Line 40 
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "image/reduced.h"  
49  #include "image/font.h"  #include "image/font.h"
50    #include "image/qpel.h"
51    
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "utils/emms.h"  #include "utils/emms.h"
56  #include "motion/motion.h"  #include "motion/motion.h"
57    #include "motion/gmc.h"
58    
59  #include "image/image.h"  #include "image/image.h"
60  #include "image/colorspace.h"  #include "image/colorspace.h"
61    #include "image/postprocessing.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  int  static int
65  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
66  {  {
67          /* free existing */          /* free existing */
   
68          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
69          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
70          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 107  Line 73 
73    
74          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
75    
76          if (dec->last_mbs)    image_null(&dec->cur);
77      image_null(&dec->refn[0]);
78      image_null(&dec->refn[1]);
79      image_null(&dec->tmp);
80      image_null(&dec->qtmp);
81      image_null(&dec->gmc);
82    
83    
84                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
         if (dec->mbs)  
85                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
86      xvid_free(dec->qscale);
87      dec->last_mbs = NULL;
88      dec->mbs = NULL;
89      dec->qscale = NULL;
90    
91          /* realloc */          /* realloc */
   
92          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
93          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
94    
95          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
96          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
97    
98          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (   image_create(&dec->cur, dec->edged_width, dec->edged_height)
99                  xvid_free(dec);              || image_create(&dec->refn[0], dec->edged_width, dec->edged_height)
100                  return XVID_ERR_MEMORY;              || image_create(&dec->refn[1], dec->edged_width, dec->edged_height)         /* Support B-frame to reference last 2 frame */
101          }              || image_create(&dec->tmp, dec->edged_width, dec->edged_height)
102                || image_create(&dec->qtmp, dec->edged_width, dec->edged_height)
103          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {        || image_create(&dec->gmc, dec->edged_width, dec->edged_height) )
104                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);      goto memory_error;
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         if (image_create(&dec->refn[1], 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);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
         if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {  
                 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;  
         }  
   
         if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
105    
106          dec->mbs =          dec->mbs =
107                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
108                                          CACHE_LINE);                                          CACHE_LINE);
109          if (dec->mbs == NULL) {          if (dec->mbs == NULL)
110                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);            goto memory_error;
                 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);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
111          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
112    
113          // add by chenm001 <chenm001@163.com>          /* For skip MB flag */
         // for skip MB flag  
114          dec->last_mbs =          dec->last_mbs =
115                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
116                                          CACHE_LINE);                                          CACHE_LINE);
117          if (dec->last_mbs == NULL) {          if (dec->last_mbs == NULL)
118              goto memory_error;
119            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
120    
121            /* nothing happens if that fails */
122            dec->qscale =
123                    xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);
124    
125            if (dec->qscale)
126                    memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
127    
128            return 0;
129    
130    memory_error:
131            /* Most structures were deallocated / nullifieded, so it should be safe */
132            /* decoder_destroy(dec) minus the write_timer */
133                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
134                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
135                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
139    
140                  xvid_free(dec);                  xvid_free(dec);
141                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
142          }          }
143    
         memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);  
   
         return XVID_ERR_OK;  
 }  
   
144    
145  int  int
146  decoder_create(XVID_DEC_PARAM * param)  decoder_create(xvid_dec_create_t * create)
147  {  {
148          DECODER *dec;          DECODER *dec;
149    
150            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
151                    return XVID_ERR_VERSION;
152    
153          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
154          if (dec == NULL) {          if (dec == NULL) {
155                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
156          }          }
157    
158          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
159    
160          param->handle = dec;          dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
161            if (dec->mpeg_quant_matrices == NULL) {
162                    xvid_free(dec);
163                    return XVID_ERR_MEMORY;
164            }
165    
166            create->handle = dec;
167    
168          dec->width = param->width;          dec->width = create->width;
169          dec->height = param->height;          dec->height = create->height;
170    
171          image_null(&dec->cur);          image_null(&dec->cur);
172          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
# Line 227  Line 177 
177  /* image based GMC */  /* image based GMC */
178          image_null(&dec->gmc);          image_null(&dec->gmc);
179    
   
180          dec->mbs = NULL;          dec->mbs = NULL;
181          dec->last_mbs = NULL;          dec->last_mbs = NULL;
182            dec->qscale = NULL;
183    
184          init_timer();          init_timer();
185            init_postproc(&dec->postproc);
186            init_mpeg_matrix(dec->mpeg_quant_matrices);
187    
188          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
         // for support B-frame to save reference frame's time  
189          dec->frames = 0;          dec->frames = 0;
190          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
191          dec->low_delay = 0;          dec->low_delay = 0;
192          dec->packed_mode = 0;          dec->packed_mode = 0;
193            dec->time_inc_resolution = 1; /* until VOL header says otherwise */
194    
195          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);          dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
196    
197          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
198                  return decoder_resize(dec);                  return decoder_resize(dec);
199          else          else
200                  return XVID_ERR_OK;                  return 0;
201  }  }
202    
203    
# Line 254  Line 206 
206  {  {
207          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
208          xvid_free(dec->mbs);          xvid_free(dec->mbs);
209            xvid_free(dec->qscale);
210    
211          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */          /* image based GMC */
212            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
213    
214          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
215          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
216          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
217          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
218          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
219            xvid_free(dec->mpeg_quant_matrices);
220          xvid_free(dec);          xvid_free(dec);
221    
222          write_timer();          write_timer();
223          return XVID_ERR_OK;          return 0;
224  }  }
225    
   
   
226  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
227          -1, -2, 1, 2          -1, -2, 1, 2
228  };  };
229    
230    /* decode an intra macroblock */
231    static void
   
 // decode an intra macroblock  
   
 void  
232  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
233                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
234                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 289  Line 238 
238                                  Bitstream * bs,                                  Bitstream * bs,
239                                  const uint32_t quant,                                  const uint32_t quant,
240                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
241                                  const unsigned int bound,                                  const unsigned int bound)
                                 const int reduced_resolution)  
242  {  {
243    
244          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 303  Line 251 
251          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
252          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
253    
         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{  
254                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
255                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
256                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
         }  
257    
258          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
259    
260          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
261                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 336  Line 278 
278                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
279    
280                          if (dc_size > 8) {                          if (dc_size > 8) {
281                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
282                          }                          }
283    
284                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
285                          start_coeff = 1;                          start_coeff = 1;
286    
287                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
288                  } else {                  } else {
289                          start_coeff = 0;                          start_coeff = 0;
290                  }                  }
291    
292                  start_timer();                  start_timer();
293                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
294                  {                  {
295                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
296                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 358  Line 300 
300                  stop_coding_timer();                  stop_coding_timer();
301    
302                  start_timer();                  start_timer();
303                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version);
304                  stop_prediction_timer();                  stop_prediction_timer();
305    
306                  start_timer();                  start_timer();
307                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
308                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
309                  } else {                  } else {
310                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
311                  }                  }
312                  stop_iquant_timer();                  stop_iquant_timer();
313    
# Line 381  Line 323 
323          }          }
324    
325          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{  
326                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);                  transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
327                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);                  transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
328                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);                  transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
329                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);                  transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
330                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
331                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
         }  
332          stop_transfer_timer();          stop_transfer_timer();
333  }  }
334    
335    static void
336    decoder_mb_decode(DECODER * dec,
337                                    const uint32_t cbp,
338                                    Bitstream * bs,
339                                    uint8_t * pY_Cur,
340                                    uint8_t * pU_Cur,
341                                    uint8_t * pV_Cur,
342                                    const MACROBLOCK * pMB)
343    {
344            DECLARE_ALIGNED_MATRIX(data, 1, 64, int16_t, CACHE_LINE);
345    
346            int stride = dec->edged_width;
347            int next_block = stride * 8;
348            int i;
349            const uint32_t iQuant = pMB->quant;
350            const int direction = dec->alternate_vertical_scan ? 2 : 0;
351            typedef void (*get_inter_block_function_t)(
352                            Bitstream * bs,
353                            int16_t * block,
354                            int direction,
355                            const int quant,
356                            const uint16_t *matrix);
357            typedef void (*add_residual_function_t)(
358                            uint8_t *predicted_block,
359                            const int16_t *residual,
360                            int stride);
361    
362            const get_inter_block_function_t get_inter_block = (dec->quant_type == 0)
363                    ? (get_inter_block_function_t)get_inter_block_h263
364                    : (get_inter_block_function_t)get_inter_block_mpeg;
365    
366            uint8_t *dst[6];
367            int strides[6];
368    
369    
370            if (dec->interlacing && pMB->field_dct) {
371                    next_block = stride;
372                    stride *= 2;
373            }
374    
375            dst[0] = pY_Cur;
376            dst[2] = pY_Cur + next_block;
377            dst[1] = dst[0] + 8;
378            dst[3] = dst[2] + 8;
379            dst[4] = pU_Cur;
380            dst[5] = pV_Cur;
381            strides[0] = strides[1] = strides[2] = strides[3] = stride;
382            strides[4] = stride/2;
383            strides[5] = stride/2;
384    
385            for (i = 0; i < 6; i++) {
386                    /* Process only coded blocks */
387                    if (cbp & (1 << (5 - i))) {
388    
389                            /* Clear the block */
390                            memset(&data[0], 0, 64*sizeof(int16_t));
391    
392                            /* Decode coeffs and dequantize on the fly */
393                            start_timer();
394                            get_inter_block(bs, &data[0], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices));
395                            stop_coding_timer();
396    
397                            /* iDCT */
398                            start_timer();
399                            idct(&data[0]);
400                            stop_idct_timer();
401    
402                            /* Add this residual to the predicted block */
403                            start_timer();
404                            transfer_16to8add(dst[i], &data[0], strides[i]);
405                            stop_transfer_timer();
406                    }
407            }
408    }
409    
410  // decode an inter macroblock  static void __inline
411    validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec)
412    {
413            /* clip a vector to valid range
414               prevents crashes if bitstream is broken
415            */
416            int shift = 5 + dec->quarterpel;
417            int xborder_high = (int)(dec->mb_width - x_pos) << shift;
418            int xborder_low = (-(int)x_pos-1) << shift;
419            int yborder_high = (int)(dec->mb_height - y_pos) << shift;
420            int yborder_low = (-(int)y_pos-1) << shift;
421    
422    #define CHECK_MV(mv) \
423            do { \
424            if ((mv).x > xborder_high) { \
425                    DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \
426                    (mv).x = xborder_high; \
427            } else if ((mv).x < xborder_low) { \
428                    DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \
429                    (mv).x = xborder_low; \
430            } \
431            if ((mv).y > yborder_high) { \
432                    DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \
433                    (mv).y = yborder_high; \
434            } else if ((mv).y < yborder_low) { \
435                    DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \
436                    (mv).y = yborder_low; \
437            } \
438            } while (0)
439    
440            CHECK_MV(mv[0]);
441            CHECK_MV(mv[1]);
442            CHECK_MV(mv[2]);
443            CHECK_MV(mv[3]);
444    }
445    
446    /* Up to this version, chroma rounding was wrong with qpel.
447     * So we try to be backward compatible to avoid artifacts */
448    #define BS_VERSION_BUGGY_CHROMA_ROUNDING 1
449    
450  void  /* decode an inter macroblock */
451    static void
452  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
453                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
454                                  const uint32_t x_pos,                                  const uint32_t x_pos,
455                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t fcode,  
456                                  const uint32_t cbp,                                  const uint32_t cbp,
457                                  Bitstream * bs,                                  Bitstream * bs,
                                 const uint32_t quant,  
458                                  const uint32_t rounding,                                  const uint32_t rounding,
459                                  const int reduced_resolution)                                  const int ref)
460  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
461          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
462          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
463          uint32_t i;          uint32_t i;
464          uint32_t iQuant = pMB->quant;  
465          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
466    
467          int uv_dx, uv_dy;          int uv_dx, uv_dy;
468          VECTOR mv[4];   /* local copy of mvs */          VECTOR mv[4];   /* local copy of mvs */
469    
         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 {  
470                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
471                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
472                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
473                  for (i = 0; i < 4; i++)                  for (i = 0; i < 4; i++)
474                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
         }  
   
         if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {  
475    
476                  uv_dx = mv[0].x / (1 + dec->quarterpel);          validate_vector(mv, x_pos, y_pos, dec);
                 uv_dy = mv[0].y / (1 + dec->quarterpel);  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
477    
478                  start_timer();                  start_timer();
                 if (reduced_resolution)  
                 {  
                         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);  
479    
480                  }          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
481                  else  
482                  {                  uv_dx = mv[0].x;
483                    uv_dy = mv[0].y;
484                          if(dec->quarterpel) {                          if(dec->quarterpel) {
485                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,                          if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
486                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                  uv_dx = (uv_dx>>1) | (uv_dx&1);
487                                                                                          mv[0].x, mv[0].y, stride, rounding);                                  uv_dy = (uv_dy>>1) | (uv_dy&1);
488                          }                          }
489                          else {                          else {
490                                  interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  uv_dx /= 2;
491                                                                            mv[0].x, mv[0].y, stride, rounding);                                  uv_dy /= 2;
492                          }                          }
   
                         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);  
493                  }                  }
494                  stop_comp_timer();                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
495                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
         } else {        /* MODE_INTER4V */  
                 int sum;  
496    
497                  if(dec->quarterpel)                  if(dec->quarterpel)
498                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
499                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
500                                                                                            mv[0].x, mv[0].y, stride, rounding);
501                  else                  else
502                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
503                                                                            mv[0].x, mv[0].y, stride, rounding);
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
504    
505                  if(dec->quarterpel)          } else {        /* MODE_INTER4V */
                         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;  
506    
507                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  if(dec->quarterpel) {
508                            if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
509                                    int z;
510                                    uv_dx = 0; uv_dy = 0;
511                                    for (z = 0; z < 4; z++) {
512                                      uv_dx += ((mv[z].x>>1) | (mv[z].x&1));
513                                      uv_dy += ((mv[z].y>>1) | (mv[z].y&1));
514                                    }
515                            }
516                            else {
517                                    uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
518                                    uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
519                            }
520                    } else {
521                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
522                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
523                    }
524    
525                  start_timer();                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
526                  if (reduced_resolution)                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
                 {  
                         interpolate16x16_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.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);  
527    
                         // set_block(pY_Cur, stride, 32, 32, 127);  
                 }  
                 else  
                 {  
528                          if(dec->quarterpel) {                          if(dec->quarterpel) {
529                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
530                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
# Line 537  Line 538 
538                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
539                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
540                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
541                          }                  } else {
                         else {  
542                                  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,
543                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
544                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
# Line 548  Line 548 
548                                  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,
549                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
550                          }                          }
551            }
552    
553                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,          /* chroma */
554            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
555                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
556                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
557                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
                 }  
                 stop_comp_timer();  
         }  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (reduced_resolution)  
         {  
                 if (cbp & 32)  
                         add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }  
         else  
         {  
                 if (cbp & 32)  
                         transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         }  
         stop_transfer_timer();  
 }  
558    
559  static __inline int gmc_sanitize(int value, int quarterpel, int fcode)          stop_comp_timer();
 {  
         int length = 1 << (fcode+4);  
   
 //      if (quarterpel) value *= 2;  
560    
561          if (value < -length)          if (cbp)
562                  return -length;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
         else if (value >= length)  
                 return length-1;  
         else return value;  
563  }  }
564    
   
565  static void  static void
566  decoder_mbgmc(DECODER * dec,  decoder_mbgmc(DECODER * dec,
567                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
# Line 643  Line 570 
570                                  const uint32_t fcode,                                  const uint32_t fcode,
571                                  const uint32_t cbp,                                  const uint32_t cbp,
572                                  Bitstream * bs,                                  Bitstream * bs,
573                                  const uint32_t quant,                                  const uint32_t rounding)
                                 const uint32_t rounding,  
                                 const int reduced_resolution)   /* no reduced res support */  
574  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
575          const uint32_t stride = dec->edged_width;          const uint32_t stride = dec->edged_width;
576          const uint32_t stride2 = stride / 2;          const uint32_t stride2 = stride / 2;
577          const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
         uint32_t i;  
         const uint32_t iQuant = pMB->quant;  
578          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
579          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
580          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
581    
582            NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
583    
584          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
585    
586          start_timer();          start_timer();
587    
588  /* this is where the calculations are done */  /* this is where the calculations are done */
589    
590          {          gmc_data->predict_16x16(gmc_data,
591                  pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
592                                          stride, stride2, dec->quarterpel, rounding, &dec->cur);                          stride, stride, x_pos, y_pos, rounding);
593    
594            gmc_data->predict_8x8(gmc_data,
595                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
596                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
597                            stride2, stride2, x_pos, y_pos, rounding);
598    
599            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
600    
601                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
602                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
         }  
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
 /*      transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);  
         transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);  
         transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);  
 */  
603    
604            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
605    
606          stop_transfer_timer();          stop_transfer_timer();
607    
608          if (!cbp) return;          if (cbp)
609                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
610    
611                  if (cbp & (1 << (5 - i)))       // coded  }
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
612    
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
613    
614                          start_timer();  static void
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
 /* interlace + GMC is this possible ??? */  
 /*      if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
 */  
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
   
 void  
615  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
616                             Bitstream * bs,                             Bitstream * bs,
                            int reduced_resolution,  
617                             int quant,                             int quant,
618                             int intra_dc_threshold)                             int intra_dc_threshold)
619  {  {
620          uint32_t bound;          uint32_t bound;
621          uint32_t x, y;          uint32_t x, y;
622          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
623          uint32_t mb_height = dec->mb_height;          const uint32_t mb_height = dec->mb_height;
   
         if (reduced_resolution)  
         {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
624    
625          bound = 0;          bound = 0;
626    
# Line 774  Line 645 
645                          }                          }
646                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
647    
648                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
649    
650                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
651                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 801  Line 672 
672    
673                          if (dec->interlacing) {                          if (dec->interlacing) {
674                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
675                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
676                          }                          }
677    
678                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
679                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
680    
681                  }                  }
682                  if(dec->out_frm)                  if(dec->out_frm)
# Line 815  Line 686 
686  }  }
687    
688    
689  void  static void
690  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
691                                    Bitstream * bs,                                    Bitstream * bs,
692                                    int x,                                    int x,
# Line 826  Line 697 
697                                    const int bound)                                    const int bound)
698  {  {
699    
700          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
701          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
702          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
703          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
704    
705          VECTOR pmv;          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
706          VECTOR mv;          VECTOR mv;
707    
         pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);  
   
708          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
709          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
710    
711          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
712    
713          mv.x += pmv.x;          mv.x += pmv.x;
714          mv.y += pmv.y;          mv.y += pmv.y;
# Line 860  Line 729 
729          ret_mv->y = mv.y;          ret_mv->y = mv.y;
730  }  }
731    
   
   
   
   
732  /* for P_VOP set gmc_warp to NULL */  /* for P_VOP set gmc_warp to NULL */
733  void  static void
734  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
735                             Bitstream * bs,                             Bitstream * bs,
736                             int rounding,                             int rounding,
                            int reduced_resolution,  
737                             int quant,                             int quant,
738                             int fcode,                             int fcode,
739                             int intra_dc_threshold,                             int intra_dc_threshold,
740                             const WARPPOINTS *const gmc_warp)                             const WARPPOINTS *const gmc_warp)
741  {  {
   
742          uint32_t x, y;          uint32_t x, y;
743          uint32_t bound;          uint32_t bound;
744          int cp_mb, st_mb;          int cp_mb, st_mb;
745          uint32_t mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
746          uint32_t mb_height = dec->mb_height;          const uint32_t mb_height = dec->mb_height;
   
         if (reduced_resolution)  
         {  
                 mb_width = (dec->width + 31) / 32;  
                 mb_height = (dec->height + 31) / 32;  
         }  
747    
748            if (!dec->is_edged[0]) {
749          start_timer();          start_timer();
750          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
751                                     dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
752                    dec->is_edged[0] = 1;
753          stop_edges_timer();          stop_edges_timer();
   
         if (gmc_warp)  
         {  
   
                 // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16  
                 if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )  
                 {  
                         fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",  
                                 dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),  
                                 dec->sprite_warping_points);  
754                  }                  }
755    
756            if (gmc_warp) {
757                    /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
758                  generate_GMCparameters( dec->sprite_warping_points,                  generate_GMCparameters( dec->sprite_warping_points,
759                                  (2 << dec->sprite_warping_accuracy), gmc_warp,                                  dec->sprite_warping_accuracy, gmc_warp,
760                                  dec->width, dec->height, &dec->gmc_data);                                  dec->width, dec->height, &dec->new_gmc_data);
761    
762  /* image warping is done block-based  in decoder_mbgmc(), now */  /* image warping is done block-based  in decoder_mbgmc(), now */
 /*  
         generate_GMCimage(&dec->gmc_data, &dec->refn[0],  
                                         mb_width, mb_height,  
                                         dec->edged_width, dec->edged_width/2,  
                                         fcode, dec->quarterpel, 0,  
                                         rounding, dec->mbs, &dec->gmc);  
 */  
763          }          }
764    
765          bound = 0;          bound = 0;
# Line 925  Line 769 
769                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
770                          MACROBLOCK *mb;                          MACROBLOCK *mb;
771    
772                          // skip stuffing                          /* skip stuffing */
773                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
774                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
775    
776                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1)) {
                         {  
777                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
778                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
779                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 938  Line 781 
781                          }                          }
782                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
783    
784                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
785    
786                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
787                          if (!(BitstreamGetBit(bs)))     // block _is_ coded                                  uint32_t mcbpc, cbpc, cbpy, cbp;
788                          {                                  uint32_t intra, acpred_flag = 0;
789                                  uint32_t mcbpc;                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
                                 uint32_t cbpc;  
                                 uint32_t acpred_flag;  
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
                                 int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC  
790    
791                                  cp_mb++;                                  cp_mb++;
792                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
793                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
794                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
795    
796                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
797                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
                                 acpred_flag = 0;  
798    
799                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
800    
                                 if (intra) {  
                                         acpred_flag = BitstreamGetBit(bs);  
                                 }  
   
801                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
                                 {  
802                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
803                                  }                                  else if (intra)
804                                            acpred_flag = BitstreamGetBit(bs);
805    
806                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
807                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
808    
809                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
810    
811                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
812                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
813                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
814                                          quant += dquant;                                          quant += dquant;
815                                          if (quant > 31) {                                          if (quant > 31) {
816                                                  quant = 31;                                                  quant = 31;
817                                          } else if (quant < 1) {                                          } else if (quant < 1) {
818                                                  quant = 1;                                                  quant = 1;
819                                          }                                          }
820                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
821                                  }                                  }
822                                  mb->quant = quant;                                  mb->quant = quant;
823    
824                                  if (dec->interlacing) {                                  if (dec->interlacing) {
825                                          if (cbp || intra) {                                          if (cbp || intra) {
826                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
827                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
828                                          }                                          }
829    
830                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
831                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
832                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
833    
834                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
835                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
836                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
837                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
838                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
839                                                  }                                                  }
840                                          }                                          }
841                                  }                                  }
842    
843                                  if (mcsel) {                                  if (mcsel) {
844                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
                                                                 rounding, reduced_resolution);  
845                                          continue;                                          continue;
846    
847                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
848    
849                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
850                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
851                                                                                    fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],  
                                                                                   fcode, bound);  
852                                          } else {                                          } else {
853                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
                                                                                   fcode, bound);  
854                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
855                                          }                                          }
856                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
   
857                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
858                                          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);
859                                          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);
860                                          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);
861                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */
862                                  {                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
863                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
                                                 0;  
                                         mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                 0;  
864                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
865                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
866                                          continue;                                          continue;
867                                  }                                  }
868    
869                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0);
                                                                 rounding, reduced_resolution);  
870    
871                          }                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
                         else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */  
                         {  
872                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
873                                    mb->quant = quant;
874                                  start_timer();                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
   
                                 decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,  
                                                                 rounding, reduced_resolution);  
   
                                 stop_transfer_timer();  
875    
876                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
877                                    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);
878                                    cp_mb = 0;                                    cp_mb = 0;
879                                  }                                  }
880                                  st_mb = x+1;                                  st_mb = x+1;
881                          }                          } else {        /* not coded P_VOP macroblock */
                         else    /* not coded P_VOP macroblock */  
                         {  
882                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
883                                    mb->quant = quant;
884    
885                                  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;
886                                  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  
887    
888                                  start_timer();                                  decoder_mbinter(dec, mb, x, y, 0, bs,
889                                                                    rounding, 0);
                                 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);  
   
                                         transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         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);  
   
                                         transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->edged_width/2);  
                                 }  
   
                                 stop_transfer_timer();  
890    
891                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
892                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
# Line 1111  Line 895 
895                                  st_mb = x+1;                                  st_mb = x+1;
896                          }                          }
897                  }                  }
898    
899                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
900                    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);
901          }          }
902  }  }
903    
904    
905  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
906  // decode B-frame motion vector  static void
907  void  get_b_motion_vector(Bitstream * bs,
 get_b_motion_vector(DECODER * dec,  
                                         Bitstream * bs,  
                                         int x,  
                                         int y,  
908                                          VECTOR * mv,                                          VECTOR * mv,
909                                          int fcode,                                          int fcode,
910                                          const VECTOR pmv)                                          const VECTOR pmv,
911  {                                          const DECODER * const dec,
912          int scale_fac = 1 << (fcode - 1);                                          const int x, const int y)
913          int high = (32 * scale_fac) - 1;  {
914          int low = ((-32) * scale_fac);          const int scale_fac = 1 << (fcode - 1);
915          int range = (64 * scale_fac);          const int high = (32 * scale_fac) - 1;
916            const int low = ((-32) * scale_fac);
917          int mv_x, mv_y;          const int range = (64 * scale_fac);
         int pmv_x, pmv_y;  
918    
919          pmv_x = pmv.x;          int mv_x = get_mv(bs, fcode);
920          pmv_y = pmv.y;          int mv_y = get_mv(bs, fcode);
921    
922          mv_x = get_mv(bs, fcode);          mv_x += pmv.x;
923          mv_y = get_mv(bs, fcode);          mv_y += pmv.y;
924    
925          mv_x += pmv_x;          if (mv_x < low)
         mv_y += pmv_y;  
   
         if (mv_x < low) {  
926                  mv_x += range;                  mv_x += range;
927          } else if (mv_x > high) {          else if (mv_x > high)
928                  mv_x -= range;                  mv_x -= range;
         }  
929    
930          if (mv_y < low) {          if (mv_y < low)
931                  mv_y += range;                  mv_y += range;
932          } else if (mv_y > high) {          else if (mv_y > high)
933                  mv_y -= range;                  mv_y -= range;
         }  
934    
935          mv->x = mv_x;          mv->x = mv_x;
936          mv->y = mv_y;          mv->y = mv_y;
937  }  }
938    
939    /* decode an B-frame direct & interpolate macroblock */
940  // add by MinChen <chenm001@163.com>  static void
 // decode an B-frame forward & backward inter macroblock  
 void  
 decoder_bf_mbinter(DECODER * dec,  
                                    const MACROBLOCK * pMB,  
                                    const uint32_t x_pos,  
                                    const uint32_t y_pos,  
                                    const uint32_t cbp,  
                                    Bitstream * bs,  
                                    const uint32_t quant,  
                                    const uint8_t ref)  
 {  
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         uint32_t stride = dec->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         int uv_dx, uv_dy;  
   
         pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);  
         pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
   
         if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
   
                 if (dec->quarterpel)  
                 {  
                         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];  
         } else {  
                 int sum;  
   
                 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  
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
   
                 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  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
         }  
   
         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 {  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,  
                                                           pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,  
                                                       pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,  
                                                           pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                           pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);  
         }  
   
         interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         stop_comp_timer();  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
 // add by MinChen <chenm001@163.com>  
 // decode an B-frame direct &  inter macroblock  
 void  
941  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
942                                                             IMAGE forward,                                                             IMAGE forward,
943                                                             IMAGE backward,                                                             IMAGE backward,
944                                                             const MACROBLOCK * pMB,                                                                  MACROBLOCK * pMB,
945                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
946                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
947                                                             Bitstream * bs)                                                                  Bitstream * bs,
948                                                                    const int direct)
949  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
950          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
951          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
         uint32_t iQuant = pMB->quant;  
952          int uv_dx, uv_dy;          int uv_dx, uv_dy;
953          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
         uint32_t i;  
954          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
955      const uint32_t cbp = pMB->cbp;      const uint32_t cbp = pMB->cbp;
956    
# Line 1319  Line 958 
958          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
959          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
960    
961            validate_vector(pMB->mvs, x_pos, y_pos, dec);
962            validate_vector(pMB->b_mvs, x_pos, y_pos, dec);
963    
964          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!direct) {
965                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
966                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
   
967                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
968                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
969    
970                  if (dec->quarterpel)                  if (dec->quarterpel) {
971                  {                          if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
972                                    uv_dx = (uv_dx>>1) | (uv_dx&1);
973                                    uv_dy = (uv_dy>>1) | (uv_dy&1);
974                                    b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1);
975                                    b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1);
976                            }
977                            else {
978                          uv_dx /= 2;                          uv_dx /= 2;
979                          uv_dy /= 2;                          uv_dy /= 2;
   
980                          b_uv_dx /= 2;                          b_uv_dx /= 2;
981                          b_uv_dy /= 2;                          b_uv_dy /= 2;
982                  }                  }
983                    }
984    
985                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
986                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
   
987                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
988                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
         } else {  
                 int sum;  
   
                 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  
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
   
                 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  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
989    
990            } else {
991                    uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
992                    uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
993                    b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
994                    b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
995    
996                  if(dec->quarterpel)                  if (dec->quarterpel) {
997                          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);                          if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) {
998                  else                                  uv_dx = (uv_dx>>1) | (uv_dx&1);
999                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                                  uv_dy = (uv_dy>>1) | (uv_dy&1);
1000                                    b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1);
1001                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                                  b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1);
1002                            }
1003                  if(dec->quarterpel)                          else {
1004                          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);                                  uv_dx /= 2;
1005                  else                                  uv_dy /= 2;
1006                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                                  b_uv_dx /= 2;
1007                                    b_uv_dy /= 2;
1008                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                          }
1009          }          }
1010    
1011                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1012                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1013                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1014                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1015            }
1016    
1017          start_timer();          start_timer();
1018          if(dec->quarterpel) {          if(dec->quarterpel) {
1019                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1020                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1021                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1022                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1023                  else {                  } else {
1024                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1025                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1026                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
# Line 1395  Line 1034 
1034                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1035                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1036                  }                  }
1037          }          } else {
         else {  
1038                  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,
1039                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1040                  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,
1041                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1042                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1043                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1044                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1045                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                          pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                           0);  
1046          }          }
1047    
1048          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1415  Line 1052 
1052    
1053    
1054          if(dec->quarterpel) {          if(dec->quarterpel) {
1055                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1056                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1057                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1058                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1059                  else {                  } else {
1060                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1061                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1062                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1063                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1064                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1065                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1066                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1067                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1068                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1069                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1070                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1071                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1072                  }                  }
1073          }          } else {
1074          else {                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, 16 * y_pos,
                 interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,  
1075                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1076                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1077                                                            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, 0);
1078                                                            0);                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos,
1079                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                                  16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1080                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1081                                                            stride, 0);                                  16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
                 interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,  
                                                           16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,  
                                                           stride, 0);  
1082          }          }
1083    
1084          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos,
1085                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1086          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos,
1087                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1088    
         interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,  
                                                 stride, 1, 8);  
   
         interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,  
                                                 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);  
   
1089          stop_comp_timer();          stop_comp_timer();
1090    
1091          for (i = 0; i < 6; i++) {          if (cbp)
1092                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
   
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
1093                          }                          }
                         stop_iquant_timer();  
1094    
1095                          start_timer();  /* for decode B-frame dbquant */
1096                          idct(&data[i * 64]);  static __inline int32_t
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
   
 // add by MinChen <chenm001@163.com>  
 // for decode B-frame dbquant  
 int32_t __inline  
1097  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1098  {  {
1099          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1100                  return (0);                  return (0);
1101          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1102                  return (-2);                  return (-2);
1103          else          else                                                    /* '11' */
1104                  return (2);                             // '11'                  return (2);
1105  }  }
1106    
1107  // add by MinChen <chenm001@163.com>  /*
1108  // for decode B-frame mb_type   * decode B-frame mb_type
1109  // bit   ret_value   * bit          ret_value
1110  // 1        0   * 1            0
1111  // 01       1   * 01           1
1112  // 001      2   * 001          2
1113  // 0001     3   * 0001         3
1114  int32_t __inline   */
1115    static int32_t __inline
1116  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1117  {  {
1118          int32_t mb_type;          int32_t mb_type;
1119    
1120          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1121                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1122                  return (mb_type);                  return (mb_type);
1123          else  
1124                  return (-1);          return -1;
1125  }  }
1126    
1127  void  static void
1128  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1129                             Bitstream * bs,                             Bitstream * bs,
1130                             int quant,                             int quant,
# Line 1578  Line 1134 
1134          uint32_t x, y;          uint32_t x, y;
1135          VECTOR mv;          VECTOR mv;
1136          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1137  #ifdef BFRAMES_DEC_DEBUG          int i;
         FILE *fp;  
         static char first=0;  
 #define BFRAME_DEBUG    if (!first && fp){ \  
                 fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \  
         }  
 #endif  
1138    
1139            if (!dec->is_edged[0]) {
1140          start_timer();          start_timer();
1141          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1142                                     dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
1143          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,                  dec->is_edged[0] = 1;
                                    dec->width, dec->height);  
1144          stop_edges_timer();          stop_edges_timer();
1145            }
1146    
1147  #ifdef BFRAMES_DEC_DEBUG          if (!dec->is_edged[1]) {
1148          if (!first){                  start_timer();
1149                  fp=fopen("C:\\XVIDDBG.TXT","w");                  image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1150                                                    dec->width, dec->height, dec->bs_version);
1151                    dec->is_edged[1] = 1;
1152                    stop_edges_timer();
1153          }          }
 #endif  
1154    
1155          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1156                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1157                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1158                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1159                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1160                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1161                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1162                            int32_t intra_dc_threshold; /* fake variable */
1163    
1164                            if (check_resync_marker(bs, fcode_max  - 1)) {
1165                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1166                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1167                                    x = bound % dec->mb_width;
1168                                    y = bound / dec->mb_width;
1169                                    /* reset predicted macroblocks */
1170                                    dec->p_fmv = dec->p_bmv = zeromv;
1171                            }
1172    
1173                          mv =                          mv =
1174                          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] =
1175                          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;
1176                            mb->quant = quant;
1177    
1178                          // skip if the co-located P_VOP macroblock is not coded                          /*
1179                          // if not codec in co-located S_VOP macroblock is _not_ automatically skipped                           * skip if the co-located P_VOP macroblock is not coded
1180                             * if not codec in co-located S_VOP macroblock is _not_
1181                             * automatically skipped
1182                             */
1183    
1184                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
                                 //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);  
1185                                  mb->cbp = 0;                                  mb->cbp = 0;
1186  #ifdef BFRAMES_DEC_DEBUG                                  mb->mode = MODE_FORWARD;
1187                                  mb->mb_type = MODE_NOT_CODED;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
         BFRAME_DEBUG  
 #endif  
                                 mb->mb_type = MODE_FORWARD;  
                                 mb->quant = last_mb->quant;  
                                 //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  
                                 //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
   
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);  
1188                                  continue;                                  continue;
1189                          }                          }
1190    
1191                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1192                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1193    
1194                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1195    
1196                                  if (!modb2) {   // modb=='00'                                  if (!modb2)             /* modb=='00' */
1197                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1198                                  } else {                                  else
1199                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1200    
1201                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1202                                            quant += get_dbquant(bs);
1203                                            if (quant > 31)
1204                                                  quant = 31;                                                  quant = 31;
1205                                          } else if (quant < 1) {                                          else if (quant < 1)
1206                                                  quant = 1;                                                  quant = 1;
1207                                          }                                          }
1208                                    mb->quant = quant;
1209    
1210                                    if (dec->interlacing) {
1211                                            if (mb->cbp) {
1212                                                    mb->field_dct = BitstreamGetBit(bs);
1213                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1214                                  }                                  }
1215    
1216                                            if (mb->mode) {
1217                                                    mb->field_pred = BitstreamGetBit(bs);
1218                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1219    
1220                                                    if (mb->field_pred) {
1221                                                            mb->field_for_top = BitstreamGetBit(bs);
1222                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1223                                                            mb->field_for_bot = BitstreamGetBit(bs);
1224                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1225                                                    }
1226                                            }
1227                                    }
1228    
1229                          } else {                          } else {
1230                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1231                                  mb->cbp = 0;                                  mb->cbp = 0;
1232                          }                          }
1233    
1234                          mb->quant = quant;                          switch (mb->mode) {
                         mb->mode = MODE_INTER4V;  
                         //DEBUG1("Switch bm_type=",mb->mb_type);  
   
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
   
                         switch (mb->mb_type) {  
1235                          case MODE_DIRECT:                          case MODE_DIRECT:
1236                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1237    
1238                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {  
                                         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
                                         int i;  
   
1239                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1240                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)                                          mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x;
1241                                                                        / TRD + mv.x);                                          mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1242                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)  
1243                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)                                          mb->b_mvs[i].x = (mv.x)
1244                                                                                    / TRD                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1245                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1246                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)                                          mb->b_mvs[i].y = (mv.y)
1247                                                                        / TRD + mv.y);                                                  ? mb->mvs[i].y - last_mb->mvs[i].y
1248                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp;
                                                                                 ? ((TRB - TRD) * last_mb->mvs[i].y)  
                                                                                   / TRD  
                                                                             : mb->mvs[i].y - last_mb->mvs[i].y);  
                                         }  
                                         //DEBUG("B-frame Direct!\n");  
1249                                  }                                  }
1250    
1251                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1252                                                                                             mb, x, y, bs);                                                                                                  mb, x, y, bs, 1);
1253                                  break;                                  break;
1254    
1255                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1256                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
                                                                         dec->p_fmv);  
1257                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1258    
1259                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1260                                                                          fcode_backward, dec->p_bmv);                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
                                 dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =  
                                         mb->b_mvs[3] = mb->b_mvs[0];  
1261    
1262                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1263                                                                                             mb, x, y, bs);                                                                                          mb, x, y, bs, 0);
                                 //DEBUG("B-frame Bidir!\n");  
1264                                  break;                                  break;
1265    
1266                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1267                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
                                                                         dec->p_bmv);  
1268                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1269    
1270                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);  
                                 //DEBUG("B-frame Backward!\n");  
1271                                  break;                                  break;
1272    
1273                          case MODE_FORWARD:                          case MODE_FORWARD:
1274                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
                                                                         dec->p_fmv);  
1275                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1276    
1277                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);  
                                 //DEBUG("B-frame Forward!\n");  
1278                                  break;                                  break;
1279    
1280                          default:                          default:
1281                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1282                          }                          }
1283                    } /* End of for */
                 }                                               // end of FOR  
1284          }          }
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 first=1;  
                 if (fp)  
                         fclose(fp);  
         }  
 #endif  
1285  }  }
1286    
 // swap two MACROBLOCK array  
 void  
 mb_swap(MACROBLOCK ** mb1,  
                 MACROBLOCK ** mb2)  
 {  
         MACROBLOCK *temp = *mb1;  
   
         *mb1 = *mb2;  
         *mb2 = temp;  
 }  
   
   
1287  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1288  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1289                                          const XVID_DEC_FRAME * frame, int pp_disable)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1290                                            int coding_type, int quant)
1291  {  {
1292            const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1293    
1294            if (dec->cartoon_mode)
1295                    frame->general &= ~XVID_FILMEFFECT;
1296    
1297          if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */          if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1298                    && mbs != NULL) /* post process */
1299          {          {
1300                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1301                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1302                  image_deblock_rrv(&dec->tmp, dec->edged_width,                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1303                                                  mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                                  mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1304                                                  8, frame->general);                                             frame->general, brightness, dec->frames, (coding_type == B_VOP));
1305                  img = &dec->tmp;                  img = &dec->tmp;
1306          }          }
1307    
1308          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1309                                   dec->edged_width, frame->image, frame->stride,                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1310                                   frame->colorspace, dec->interlacing);                                   frame->output.csp, dec->interlacing);
 }  
1311    
1312            if (stats) {
1313                    stats->type = coding2type(coding_type);
1314                    stats->data.vop.time_base = (int)dec->time_base;
1315                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1316                    stats->data.vop.qscale_stride = dec->mb_width;
1317                    stats->data.vop.qscale = dec->qscale;
1318                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1319                            int i;
1320                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1321                                    stats->data.vop.qscale[i] = mbs[i].quant;
1322                    } else
1323                            stats->data.vop.qscale = NULL;
1324            }
1325    }
1326    
1327  int  int
1328  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1329                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1330  {  {
1331    
1332          Bitstream bs;          Bitstream bs;
1333          uint32_t rounding;          uint32_t rounding;
1334          uint32_t reduced_resolution;          uint32_t quant = 2;
         uint32_t quant;  
1335          uint32_t fcode_forward;          uint32_t fcode_forward;
1336          uint32_t fcode_backward;          uint32_t fcode_backward;
1337          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1338          WARPPOINTS gmc_warp;          WARPPOINTS gmc_warp;
1339          uint32_t vop_type;          int coding_type;
1340          int success = 0;          int success, output, seen_something;
         int output = 0;  
         int seen_something = 0;  
1341    
1342          start_global_timer();          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1343                    return XVID_ERR_VERSION;
1344    
1345          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);          start_global_timer();
         dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;  
1346    
1347          if ((frame->general & XVID_DEC_DISCONTINUITY))          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1348            if ((frame->general & XVID_DISCONTINUITY))
1349                  dec->frames = 0;                  dec->frames = 0;
1350            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1351    
1352          if (frame->length < 0)  /* decoder flush */          if (frame->length < 0) {        /* decoder flush */
1353          {                  int ret;
1354                  /* if  not decoding "low_delay/packed", and this isn't low_delay and                  /* if  not decoding "low_delay/packed", and this isn't low_delay and
1355                      we have a reference frame, then outout the reference frame */                      we have a reference frame, then outout the reference frame */
1356                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1357                  {                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1358                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);                          dec->frames = 0;
1359                          output = 1;                          ret = 0;
1360                  }                  } else {
1361                            if (stats) stats->type = XVID_TYPE_NOTHING;
1362                  frame->length = 0;                          ret = XVID_ERR_END;
                 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  
1363                  }                  }
1364    
1365                  emms();                  emms();
   
1366                  stop_global_timer();                  stop_global_timer();
1367                  return XVID_ERR_OK;                  return ret;
1368          }          }
1369    
1370          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1371    
1372          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1373          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1374          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1375                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1376                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1377                    if (stats) stats->type = XVID_TYPE_NOTHING;
1378                  emms();                  emms();
1379                  return XVID_ERR_OK;                  return 1;       /* one byte consumed */
1380          }          }
1381    
1382            success = 0;
1383            output = 0;
1384            seen_something = 0;
1385    
1386  repeat:  repeat:
1387    
1388          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type = BitstreamReadHeaders(&bs, dec, &rounding,
1389                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1390    
1391          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%"
1392                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);  #if defined(_MSC_VER)
1393        "I64"
1394    #else
1395        "ll"
1396    #endif
1397        "i,  time_pp=%i,  time_bp=%i\n",
1398                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1399    
1400          if (vop_type == - 1)          if (coding_type == -1) { /* nothing */
         {  
1401                  if (success) goto done;                  if (success) goto done;
1402                    if (stats) stats->type = XVID_TYPE_NOTHING;
1403                  emms();                  emms();
1404                  return XVID_ERR_FAIL;                  return BitstreamPos(&bs)/8;
1405          }          }
1406    
1407          if (vop_type == -2 || vop_type == -3)          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1408          {  
1409                  if (vop_type == -3)                  if (coding_type == -3)
1410                          decoder_resize(dec);                          decoder_resize(dec);
1411    
1412                  if (stats)                  if (stats) {
1413                  {                          stats->type = XVID_TYPE_VOL;
                         stats->notify = XVID_DEC_VOL;  
1414                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1415                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1416                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1417                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1418                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1419                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1420                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1421                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
                         frame->length = BitstreamPos(&bs) / 8;  
1422                          emms();                          emms();
1423                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1424                  }                  }
1425                  goto repeat;                  goto repeat;
1426          }          }
1427    
1428          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          if(dec->frames == 0 && coding_type != I_VOP) {
1429                    /* 1st frame is not an i-vop */
1430                    goto repeat;
1431            }
1432    
1433            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1434    
1435          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1436          if (dec->packed_mode && vop_type == N_VOP)          if (dec->packed_mode && coding_type == N_VOP) {
1437          {                  if (dec->low_delay_default && dec->frames > 0) {
1438                  if (dec->low_delay_default && dec->frames > 0)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
                 {  
                         decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);  
1439                          output = 1;                          output = 1;
1440                  }                  }
1441                  /* ignore otherwise */                  /* ignore otherwise */
1442          }          } else if (coding_type != B_VOP) {
1443          else if (vop_type != B_VOP)                  switch(coding_type) {
         {  
                 switch(vop_type)  
                 {  
1444                  case I_VOP :                  case I_VOP :
1445                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1446                          break;                          break;
1447                  case P_VOP :                  case P_VOP :
1448                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1449                                                  fcode_forward, intra_dc_threshold, NULL);                                                  fcode_forward, intra_dc_threshold, NULL);
1450                          break;                          break;
1451                  case S_VOP :                  case S_VOP :
1452                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1453                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1454                          break;                          break;
1455                  case N_VOP :                  case N_VOP :
1456                            /* XXX: not_coded vops are not used for forward prediction */
1457                            /* we should not swap(last_mbs,mbs) */
1458                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1459                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1460                          break;                          break;
1461                  }                  }
1462    
                 if (reduced_resolution)  
                 {  
                         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);  
                 }  
   
1463                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1464                  if (!(dec->low_delay_default && dec->packed_mode))                  if (!(dec->low_delay_default && dec->packed_mode)) {
1465                  {                          if (dec->low_delay) {
1466                          if (dec->low_delay)                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
                         {  
                                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
1467                                  output = 1;                                  output = 1;
1468                          }                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
                         else if (dec->frames > 0)       /* is the reference frame valid? */  
                         {  
1469                                  /* output the reference frame */                                  /* output the reference frame */
1470                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1471                                  output = 1;                                  output = 1;
1472                          }                          }
1473                  }                  }
1474    
1475                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1476                    dec->is_edged[1] = dec->is_edged[0];
1477                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1478                  mb_swap(&dec->mbs, &dec->last_mbs);                  dec->is_edged[0] = 0;
1479                  dec->last_reduced_resolution = reduced_resolution;                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1480                    dec->last_coding_type = coding_type;
1481    
1482                  dec->frames++;                  dec->frames++;
1483                  seen_something = 1;                  seen_something = 1;
1484    
1485          }else{  /* B_VOP */          }else{  /* B_VOP */
1486    
1487                  if (dec->low_delay)                  if (dec->low_delay) {
1488                  {                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1489                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");                          dec->low_delay = 0;
                         dec->low_delay = 1;  
1490                  }                  }
1491    
1492                  if (dec->frames < 2)                  if (dec->frames < 2) {
                 {  
1493                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1494                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1495                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
1496                            if (stats) stats->type = XVID_TYPE_NOTHING;
1497                  }else if (dec->time_pp <= dec->time_bp) {                  }else if (dec->time_pp <= dec->time_bp) {
1498                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1499                          decoded in vfw. */                          decoded in vfw. */
1500                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1501                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1502                            if (stats) stats->type = XVID_TYPE_NOTHING;
1503                  }else{                  }else{
1504                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1505                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1506                  }                  }
1507    
                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
1508                  output = 1;                  output = 1;
1509                  dec->frames++;                  dec->frames++;
1510          }          }
1511    
1512    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1513          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1514    #endif
1515    
1516          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1517          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
         {  
1518                  success = 1;                  success = 1;
1519                  goto repeat;                  goto repeat;
1520          }          }
1521    
1522  done :  done :
1523    
1524          /* low_delay_default mode: if we've gotten here without outputting anything,    /* if we reach here without outputing anything _and_
1525             then output the recently decoded frame, or print an error message  */       the calling application has specified low_delay_default,
1526          if (dec->low_delay_default && output == 0)       we *must* output something.
1527          {       this always occurs on the first call to decode() call
1528                  if (dec->packed_mode && seen_something)       when bframes are present in the bitstream. it may also
1529                  {       occur if no vops  were seen in the bitstream
1530                          /* output the recently decoded frame */  
1531                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);       if packed_mode is enabled, then we output the recently
1532                          output = 1;       decoded frame (the very first ivop). otherwise we have
1533                  }       nothing to display, and therefore output a black screen.
1534                  else    */
1535                  {    if (dec->low_delay_default && output == 0) {
1536        if (dec->packed_mode && seen_something) {
1537                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1538        } else {
1539                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1540                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1541                                  "warning: nothing to output");                          if (stats) stats->type = XVID_TYPE_NOTHING;
                         image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,  
                                 "bframe decoder lag");  
   
                         decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);  
1542                  }                  }
1543          }          }
1544    
         frame->length = BitstreamPos(&bs) / 8;  
   
         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  
         }  
   
1545          emms();          emms();
   
1546          stop_global_timer();          stop_global_timer();
1547    
1548          return XVID_ERR_OK;          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1549  }  }

Legend:
Removed from v.1.47  
changed lines
  Added in v.1.72

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