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

Diff of /xvidcore/src/decoder.c

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

revision 1.37.2.19, Thu Dec 12 10:37:44 2002 UTC revision 1.70, Tue May 17 21:03:32 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>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
# Line 68  Line 34 
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
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);
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);
71          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
72          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
73          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
74            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
75    
76      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    
         if (dec->last_mbs)  
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->refn[2], dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->refh, 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->refn[2], 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->refn[2], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refh, 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->refn[2], dec->edged_width, dec->edged_height);    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refh, 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]);
173          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
174          image_null(&dec->refn[2]);          image_null(&dec->tmp);
175          image_null(&dec->refh);          image_null(&dec->qtmp);
176    
177            /* image based GMC */
178            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 236  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 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->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
217          image_destroy(&dec->refh, 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 268  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 282  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 315  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 337  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 360  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  #define SIGN(X) (((X)>0)?1:-1)                          /* Add this residual to the predicted block */
403  #define ABS(X) (((X)>0)?(X):-(X))                          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  void  /* decode an inter macroblock */
447    static void
448  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
449                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
450                                  const uint32_t x_pos,                                  const uint32_t x_pos,
451                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t acpred_flag,  
452                                  const uint32_t cbp,                                  const uint32_t cbp,
453                                  Bitstream * bs,                                  Bitstream * bs,
                                 const uint32_t quant,  
454                                  const uint32_t rounding,                                  const uint32_t rounding,
455                                  const int reduced_resolution)                                  const int ref)
456  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
457          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
458          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
459          uint32_t i;          uint32_t i;
460          uint32_t iQuant = pMB->quant;  
461          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
462    
463          int uv_dx, uv_dy;          int uv_dx, uv_dy;
464          VECTOR mv[4];   /* local copy of mvs */          VECTOR mv[4];   /* local copy of mvs */
465    
         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{  
466                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
467                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
468                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
469                  for (i = 0; i < 4; i++)                  for (i = 0; i < 4; i++)
470                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
         }  
471    
472          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          validate_vector(mv, x_pos, y_pos, dec);
473    
474            start_timer();
475    
476            if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
477    
478                  uv_dx = mv[0].x;                  uv_dx = mv[0].x;
479                  uv_dy = mv[0].y;                  uv_dy = mv[0].y;
480                    if (dec->quarterpel) {
                 if (dec->quarterpel)  
                 {  
481                          uv_dx /= 2;                          uv_dx /= 2;
482                          uv_dy /= 2;                          uv_dy /= 2;
483                  }                  }
   
484                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
485                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
486    
487                  start_timer();                  if (dec->quarterpel)
488                  if (reduced_resolution)                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
489                  {                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
                         interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,  
490                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    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);  
   
                 }  
491                  else                  else
492                  {                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
                         if(dec->quarterpel) {  
                                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,  
                                                                                         dec->refh.y + 128, 16*x_pos, 16*y_pos,  
                                                                                         mv[0].x, mv[0].y, stride,  rounding);  
                         }  
                         else {  
                                 interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,  
493                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
                         }  
   
                         interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                 }  
                 stop_comp_timer();  
494    
495          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);  
                 else  
                         sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);  
                 else  
                         sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;  
496    
497                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  if(dec->quarterpel) {
498                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
499                            uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
500                    } else {
501                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
502                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
503                    }
504    
505                  start_timer();                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
506                  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);  
507    
                         // set_block(pY_Cur, stride, 32, 32, 127);  
                 }  
                 else  
                 {  
508                          if(dec->quarterpel) {                          if(dec->quarterpel) {
509                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
510                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
511                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
512                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
513                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
514                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
515                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
516                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
517                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
518                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
519                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
520                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
521                          }                  } else {
                         else {  
522                                  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,
523                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
524                                  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 536  Line 528 
528                                  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,
529                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
530                          }                          }
531            }
532    
533                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          /* chroma */
534            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
535                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
536                          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,
537                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
538                  }  
539                  stop_comp_timer();                  stop_comp_timer();
         }  
540    
541          for (i = 0; i < 6; i++) {          if (cbp)
542                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
543    }
544    
545                  if (cbp & (1 << (5 - i)))       // coded  static void
546    decoder_mbgmc(DECODER * dec,
547                                    MACROBLOCK * const pMB,
548                                    const uint32_t x_pos,
549                                    const uint32_t y_pos,
550                                    const uint32_t fcode,
551                                    const uint32_t cbp,
552                                    Bitstream * bs,
553                                    const uint32_t rounding)
554                  {                  {
555                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear          const uint32_t stride = dec->edged_width;
556            const uint32_t stride2 = stride / 2;
557    
558                          start_timer();          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
559                          get_inter_block(bs, &block[i * 64], direction);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
560                          stop_coding_timer();          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
561    
562                          start_timer();          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
563                          if (dec->quant_type == 0) {  
564                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
565    
566                          start_timer();                          start_timer();
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
567    
568          if (dec->interlacing && pMB->field_dct) {  /* this is where the calculations are done */
569                  next_block = stride;  
570                  stride *= 2;          gmc_data->predict_16x16(gmc_data,
571          }                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
572                            stride, stride, x_pos, y_pos, rounding);
573    
574            gmc_data->predict_8x8(gmc_data,
575                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
576                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
577                            stride2, stride2, x_pos, y_pos, rounding);
578    
579            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
580    
581            pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
582            pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
583    
584            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
585    
         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);  
         }  
586          stop_transfer_timer();          stop_transfer_timer();
587    
588            if (cbp)
589                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, pMB);
590    
591  }  }
592    
593    
594  void  static void
595  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
596                             Bitstream * bs,                             Bitstream * bs,
                            int reduced_resolution,  
597                             int quant,                             int quant,
598                             int intra_dc_threshold)                             int intra_dc_threshold)
599  {  {
600          uint32_t bound;          uint32_t bound;
601          uint32_t x, y;          uint32_t x, y;
602          int mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
603          int 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;  
         }  
604    
605          bound = 0;          bound = 0;
606    
# Line 651  Line 625 
625                          }                          }
626                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
627    
628                          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));
629    
630                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
631                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 678  Line 652 
652    
653                          if (dec->interlacing) {                          if (dec->interlacing) {
654                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
655                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
656                          }                          }
657    
658                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
659                                                          intra_dc_threshold, bound, reduced_resolution);                                                          intra_dc_threshold, bound);
660    
661                  }                  }
662                  if(dec->out_frm)                  if(dec->out_frm)
# Line 692  Line 666 
666  }  }
667    
668    
669  void  static void
670  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
671                                    Bitstream * bs,                                    Bitstream * bs,
672                                    int x,                                    int x,
# Line 703  Line 677 
677                                    const int bound)                                    const int bound)
678  {  {
679    
680          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
681          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
682          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
683          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
684    
685          VECTOR pmv;          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
686          VECTOR mv;          VECTOR mv;
687    
         pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);  
   
688          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
689          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
690    
691          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, 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);
692    
693          mv.x += pmv.x;          mv.x += pmv.x;
694          mv.y += pmv.y;          mv.y += pmv.y;
# Line 737  Line 709 
709          ret_mv->y = mv.y;          ret_mv->y = mv.y;
710  }  }
711    
712    /* for P_VOP set gmc_warp to NULL */
713    static void
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
   
   
 /* for P_VOP set gmc_mv to NULL */  
 void  
714  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
715                             Bitstream * bs,                             Bitstream * bs,
716                             int rounding,                             int rounding,
                            int reduced_resolution,  
717                             int quant,                             int quant,
718                             int fcode,                             int fcode,
719                             int intra_dc_threshold,                             int intra_dc_threshold,
720                             VECTOR * gmc_mv)                                  const WARPPOINTS *const gmc_warp)
721  {  {
   
722          uint32_t x, y;          uint32_t x, y;
723          uint32_t bound;          uint32_t bound;
724          int cp_mb, st_mb;          int cp_mb, st_mb;
725          int mb_width = dec->mb_width;          const uint32_t mb_width = dec->mb_width;
726          int 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;  
         }  
727    
728            if (!dec->is_edged[0]) {
729          start_timer();          start_timer();
730          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
731                                     dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
732                    dec->is_edged[0] = 1;
733          stop_edges_timer();          stop_edges_timer();
734            }
735    
736            if (gmc_warp) {
737                    /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
738                    generate_GMCparameters( dec->sprite_warping_points,
739                                    dec->sprite_warping_accuracy, gmc_warp,
740                                    dec->width, dec->height, &dec->new_gmc_data);
741    
742                    /* image warping is done block-based in decoder_mbgmc(), now */
743            }
744    
745          bound = 0;          bound = 0;
746    
# Line 789  Line 749 
749                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
750                          MACROBLOCK *mb;                          MACROBLOCK *mb;
751    
752                          // skip stuffing                          /* skip stuffing */
753                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
754                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
755    
756                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1)) {
                         {  
757                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
758                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
759                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 802  Line 761 
761                          }                          }
762                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
763    
764                          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));
765    
766                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
767                          if (!(BitstreamGetBit(bs)))     // not_coded                                  uint32_t mcbpc, cbpc, cbpy, cbp;
768                          {                                  uint32_t intra, acpred_flag = 0;
769                                  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  
770    
771                                  cp_mb++;                                  cp_mb++;
772                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
773                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
774                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
775    
776                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
777                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
                                 acpred_flag = 0;  
778    
779                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
780    
781                                  if (intra) {                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
                                         acpred_flag = BitstreamGetBit(bs);  
                                 }  
   
                                 if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))  
                                 {  
782                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
783                                  }                                  else if (intra)
784                                            acpred_flag = BitstreamGetBit(bs);
785    
786                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
787                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
788    
789                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
790    
791                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
792                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
793                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
794                                          quant += dquant;                                          quant += dquant;
795                                          if (quant > 31) {                                          if (quant > 31) {
796                                                  quant = 31;                                                  quant = 31;
797                                          } else if (quant < 1) {                                          } else if (quant < 1) {
798                                                  quant = 1;                                                  quant = 1;
799                                          }                                          }
800                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
801                                  }                                  }
802                                  mb->quant = quant;                                  mb->quant = quant;
803    
804                                  if (dec->interlacing) {                                  if (dec->interlacing) {
805                                          if (cbp || intra) {                                          if (cbp || intra) {
806                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
807                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
808                                          }                                          }
809    
810                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
811                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
812                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
813    
814                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
815                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
816                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
817                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
818                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
819                                                  }                                                  }
820                                          }                                          }
821                                  }                                  }
822    
823                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
824                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
825                                            continue;
826    
827                                          if (mcsel)                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
                                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
                                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
828    
829                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
830                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
831                                                                                    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);  
832                                          } else {                                          } else {
833                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
834                                                                                    fcode, bound);                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                 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;  
835                                          }                                          }
836                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
   
837                                          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);
838                                          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);
839                                          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);
840                                          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);
841                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */
842                                  {                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
843                                          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;  
844                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
845                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound);
846                                          continue;                                          continue;
847                                  }                                  }
848    
849                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0);
                                                                 rounding, reduced_resolution);  
850    
851                            } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
852                                    mb->mode = MODE_NOT_CODED_GMC;
853                                    mb->quant = quant;
854                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
855    
856                                    if(dec->out_frm && cp_mb > 0) {
857                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
858                                            cp_mb = 0;
859                          }                          }
860                          else if (gmc_mv)        /* not coded S_VOP macroblock */                                  st_mb = x+1;
861                          {                          } else {        /* not coded P_VOP macroblock */
                                 mb->mode = MODE_NOT_CODED;  
                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
                                 decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
                         }  
                         else    /* not coded P_VOP macroblock */  
                         {  
862                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
863                                    mb->quant = quant;
864    
865                                  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;
866                                  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  
   
                                 start_timer();  
867    
868                                  if (reduced_resolution)                                  decoder_mbinter(dec, mb, x, y, 0, bs,
869                                  {                                                                  rounding, 0);
                                         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();  
870    
871                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
872                                    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 968  Line 875 
875                                  st_mb = x+1;                                  st_mb = x+1;
876                          }                          }
877                  }                  }
878    
879                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
880                    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);
881          }          }
882  }  }
883    
884    
885  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
886  // decode B-frame motion vector  static void
887  void  get_b_motion_vector(Bitstream * bs,
 get_b_motion_vector(DECODER * dec,  
                                         Bitstream * bs,  
                                         int x,  
                                         int y,  
888                                          VECTOR * mv,                                          VECTOR * mv,
889                                          int fcode,                                          int fcode,
890                                          const VECTOR pmv)                                          const VECTOR pmv,
891  {                                          const DECODER * const dec,
892          int scale_fac = 1 << (fcode - 1);                                          const int x, const int y)
893          int high = (32 * scale_fac) - 1;  {
894          int low = ((-32) * scale_fac);          const int scale_fac = 1 << (fcode - 1);
895          int range = (64 * scale_fac);          const int high = (32 * scale_fac) - 1;
896            const int low = ((-32) * scale_fac);
897          int mv_x, mv_y;          const int range = (64 * scale_fac);
         int pmv_x, pmv_y;  
   
         pmv_x = pmv.x;  
         pmv_y = pmv.y;  
898    
899          mv_x = get_mv(bs, fcode);          int mv_x = get_mv(bs, fcode);
900          mv_y = get_mv(bs, fcode);          int mv_y = get_mv(bs, fcode);
901    
902          mv_x += pmv_x;          mv_x += pmv.x;
903          mv_y += pmv_y;          mv_y += pmv.y;
904    
905          if (mv_x < low) {          if (mv_x < low)
906                  mv_x += range;                  mv_x += range;
907          } else if (mv_x > high) {          else if (mv_x > high)
908                  mv_x -= range;                  mv_x -= range;
         }  
909    
910          if (mv_y < low) {          if (mv_y < low)
911                  mv_y += range;                  mv_y += range;
912          } else if (mv_y > high) {          else if (mv_y > high)
913                  mv_y -= range;                  mv_y -= range;
         }  
914    
915          mv->x = mv_x;          mv->x = mv_x;
916          mv->y = mv_y;          mv->y = mv_y;
917  }  }
918    
919    /* decode an B-frame direct & interpolate macroblock */
920  // 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->refh.y, dec->refh.y + 64,  
                                                                     dec->refh.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  
921  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
922                                                             IMAGE forward,                                                             IMAGE forward,
923                                                             IMAGE backward,                                                             IMAGE backward,
924                                                             const MACROBLOCK * pMB,                                                                  MACROBLOCK * pMB,
925                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
926                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
927                                                             Bitstream * bs)                                                                  Bitstream * bs,
928                                                                    const int direct)
929  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
930          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
931          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
         uint32_t iQuant = pMB->quant;  
932          int uv_dx, uv_dy;          int uv_dx, uv_dy;
933          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
         uint32_t i;  
934          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
935      const uint32_t cbp = pMB->cbp;      const uint32_t cbp = pMB->cbp;
936    
# Line 1176  Line 938 
938          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
939          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
940    
941            validate_vector(pMB->mvs, x_pos, y_pos, dec);
942            validate_vector(pMB->b_mvs, x_pos, y_pos, dec);
943    
944          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!direct) {
945                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
946                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
   
947                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
948                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
949    
950                  if (dec->quarterpel)                  if (dec->quarterpel) {
                 {  
951                          uv_dx /= 2;                          uv_dx /= 2;
952                          uv_dy /= 2;                          uv_dy /= 2;
   
953                          b_uv_dx /= 2;                          b_uv_dx /= 2;
954                          b_uv_dy /= 2;                          b_uv_dy /= 2;
955                  }                  }
956    
957                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
958                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
   
959                  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];
960                  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;  
961    
962                  if(dec->quarterpel)          } else {
963                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);                  uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
964                  else                  uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
965                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
966                    b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
                 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];  
   
   
                 if(dec->quarterpel)  
                         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);  
                 else  
                         sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;  
   
                 b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         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);  
                 else  
                         sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;  
967    
968                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  if (dec->quarterpel) {
969                            uv_dx /= 2;
970                            uv_dy /= 2;
971                            b_uv_dx /= 2;
972                            b_uv_dy /= 2;
973          }          }
974    
975                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
976                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
977                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
978                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
979            }
980    
981          start_timer();          start_timer();
982          if(dec->quarterpel) {          if(dec->quarterpel) {
983                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
984                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
985                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
986                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
987                  else {                  } else {
988                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
989                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
990                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
991                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
992                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
993                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
994                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
995                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
996                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
997                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
998                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
999                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1000                  }                  }
1001          }          } else {
         else {  
1002                  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,
1003                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1004                  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,
1005                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1006                  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,
1007                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1008                  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,
1009                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                          pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                           0);  
1010          }          }
1011    
1012          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1272  Line 1016 
1016    
1017    
1018          if(dec->quarterpel) {          if(dec->quarterpel) {
1019                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1020                          interpolate16x16_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1021                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1022                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1023                  else {                  } else {
1024                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1025                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1026                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1027                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1028                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1029                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1030                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1031                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1032                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1033                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1034                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1035                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1036                  }                  }
1037          }          } else {
1038          else {                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, 16 * y_pos,
                 interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,  
1039                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1040                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1041                                                            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);
1042                                                            0);                  interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos,
1043                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                                  16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1044                                                            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,
1045                                                            stride, 0);                                  16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
                 interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,  
                                                           16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,  
                                                           stride, 0);  
1046          }          }
1047    
1048          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos,
1049                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1050          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos,
1051                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1052    
         interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,  
                                                 dec->refn[2].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->refn[2].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->refn[2].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->refn[2].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->refn[2].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->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,  
                                                 stride2, 1, 8);  
   
1053          stop_comp_timer();          stop_comp_timer();
1054    
1055          for (i = 0; i < 6; i++) {          if (cbp)
1056                  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);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
1057          }          }
1058    
1059          start_timer();  /* for decode B-frame dbquant */
1060          if (cbp & 32)  static __inline int32_t
                 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  
1061  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1062  {  {
1063          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1064                  return (0);                  return (0);
1065          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1066                  return (-2);                  return (-2);
1067          else          else                                                    /* '11' */
1068                  return (2);                             // '11'                  return (2);
1069  }  }
1070    
1071  // add by MinChen <chenm001@163.com>  /*
1072  // for decode B-frame mb_type   * decode B-frame mb_type
1073  // bit   ret_value   * bit          ret_value
1074  // 1        0   * 1            0
1075  // 01       1   * 01           1
1076  // 001      2   * 001          2
1077  // 0001     3   * 0001         3
1078  int32_t __inline   */
1079    static int32_t __inline
1080  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1081  {  {
1082          int32_t mb_type;          int32_t mb_type;
1083    
1084          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1085                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1086                  return (mb_type);                  return (mb_type);
1087          else  
1088                  return (-1);          return -1;
1089  }  }
1090    
1091  void  static void
1092  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1093                             Bitstream * bs,                             Bitstream * bs,
1094                             int quant,                             int quant,
# Line 1435  Line 1098 
1098          uint32_t x, y;          uint32_t x, y;
1099          VECTOR mv;          VECTOR mv;
1100          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1101  #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  
1102    
1103            if (!dec->is_edged[0]) {
1104          start_timer();          start_timer();
1105          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1106                                     dec->width, dec->height);                                                  dec->width, dec->height, dec->bs_version);
1107          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,                  dec->is_edged[0] = 1;
                                    dec->width, dec->height);  
1108          stop_edges_timer();          stop_edges_timer();
1109            }
1110    
1111  #ifdef BFRAMES_DEC_DEBUG          if (!dec->is_edged[1]) {
1112          if (!first){                  start_timer();
1113                  fp=fopen("C:\\XVIDDBG.TXT","w");                  image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1114                                                    dec->width, dec->height, dec->bs_version);
1115                    dec->is_edged[1] = 1;
1116                    stop_edges_timer();
1117          }          }
 #endif  
1118    
1119          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1120                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1121                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1122                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1123                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1124                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1125                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1126                            uint32_t intra_dc_threshold; /* fake variable */
1127    
1128                            if (check_resync_marker(bs, fcode_max  - 1)) {
1129                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1130                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1131                                    x = bound % dec->mb_width;
1132                                    y = bound / dec->mb_width;
1133                                    /* reset predicted macroblocks */
1134                                    dec->p_fmv = dec->p_bmv = zeromv;
1135                            }
1136    
1137                          mv =                          mv =
1138                          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] =
1139                          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;
1140                            mb->quant = quant;
1141    
1142                            /*
1143                             * skip if the co-located P_VOP macroblock is not coded
1144                             * if not codec in co-located S_VOP macroblock is _not_
1145                             * automatically skipped
1146                             */
1147    
                         // the last P_VOP is skip macroblock ?  
1148                          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);  
1149                                  mb->cbp = 0;                                  mb->cbp = 0;
1150  #ifdef BFRAMES_DEC_DEBUG                                  mb->mode = MODE_FORWARD;
1151                                  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);  
1152                                  continue;                                  continue;
1153                          }                          }
1154    
1155                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1156                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1157    
1158                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1159    
1160                                  if (!modb2) {   // modb=='00'                                  if (!modb2)             /* modb=='00' */
1161                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1162                                  } else {                                  else
1163                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1164    
1165                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1166                                            quant += get_dbquant(bs);
1167                                            if (quant > 31)
1168                                                  quant = 31;                                                  quant = 31;
1169                                          } else if (quant < 1) {                                          else if (quant < 1)
1170                                                  quant = 1;                                                  quant = 1;
1171                                          }                                          }
1172                                    mb->quant = quant;
1173    
1174                                    if (dec->interlacing) {
1175                                            if (mb->cbp) {
1176                                                    mb->field_dct = BitstreamGetBit(bs);
1177                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1178                                            }
1179    
1180                                            if (mb->mode) {
1181                                                    mb->field_pred = BitstreamGetBit(bs);
1182                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1183    
1184                                                    if (mb->field_pred) {
1185                                                            mb->field_for_top = BitstreamGetBit(bs);
1186                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1187                                                            mb->field_for_bot = BitstreamGetBit(bs);
1188                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1189                                                    }
1190                                            }
1191                                  }                                  }
1192    
1193                          } else {                          } else {
1194                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1195                                  mb->cbp = 0;                                  mb->cbp = 0;
1196                          }                          }
1197    
1198                          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) {  
1199                          case MODE_DIRECT:                          case MODE_DIRECT:
1200                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1201    
1202                          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;  
   
1203                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1204                                                  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;
1205                                                                        / TRD + mv.x);                                          mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1206                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)  
1207                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)                                          mb->b_mvs[i].x = (mv.x)
1208                                                                                    / TRD                                                  ?  mb->mvs[i].x - last_mb->mvs[i].x
1209                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);                                                  : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1210                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)                                          mb->b_mvs[i].y = (mv.y)
1211                                                                        / TRD + mv.y);                                                  ? mb->mvs[i].y - last_mb->mvs[i].y
1212                                                  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");  
1213                                  }                                  }
1214    
1215                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1216                                                                                             mb, x, y, bs);                                                                                                  mb, x, y, bs, 1);
1217                                  break;                                  break;
1218    
1219                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1220                                  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);  
1221                                  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];
1222    
1223                                  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);
1224                                                                          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];  
1225    
1226                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1227                                                                                             mb, x, y, bs);                                                                                          mb, x, y, bs, 0);
                                 //DEBUG("B-frame Bidir!\n");  
1228                                  break;                                  break;
1229    
1230                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1231                                  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);  
1232                                  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];
1233    
1234                                  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");  
1235                                  break;                                  break;
1236    
1237                          case MODE_FORWARD:                          case MODE_FORWARD:
1238                                  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);  
1239                                  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];
1240    
1241                                  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");  
1242                                  break;                                  break;
1243    
1244                          default:                          default:
1245                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1246                          }                          }
1247                    } /* End of for */
                 }                                               // end of FOR  
         }  
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 first=1;  
                 if (fp)  
                         fclose(fp);  
1248          }          }
 #endif  
1249  }  }
1250    
1251  // swap two MACROBLOCK array  /* perform post processing if necessary, and output the image */
1252  void  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1253  mb_swap(MACROBLOCK ** mb1,                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1254                  MACROBLOCK ** mb2)                                          int coding_type, int quant)
1255  {  {
1256          MACROBLOCK *temp = *mb1;          const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1257    
1258          *mb1 = *mb2;          if (dec->cartoon_mode)
1259          *mb2 = temp;                  frame->general &= ~XVID_FILMEFFECT;
1260    
1261            if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1262                    && mbs != NULL) /* post process */
1263            {
1264                    /* note: image is stored to tmp */
1265                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1266                    image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1267                                               mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1268                                               frame->general, brightness, dec->frames, (coding_type == B_VOP));
1269                    img = &dec->tmp;
1270  }  }
1271    
1272            image_output(img, dec->width, dec->height,
1273                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1274                                     frame->output.csp, dec->interlacing);
1275    
1276            if (stats) {
1277                    stats->type = coding2type(coding_type);
1278                    stats->data.vop.time_base = (int)dec->time_base;
1279                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1280                    stats->data.vop.qscale_stride = dec->mb_width;
1281                    stats->data.vop.qscale = dec->qscale;
1282                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1283                            int i;
1284                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1285                                    stats->data.vop.qscale[i] = mbs[i].quant;
1286                    } else
1287                            stats->data.vop.qscale = NULL;
1288            }
1289    }
1290    
1291  int  int
1292  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1293                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1294  {  {
1295    
1296          Bitstream bs;          Bitstream bs;
1297          uint32_t rounding;          uint32_t rounding;
1298          uint32_t reduced_resolution;          uint32_t quant = 2;
         uint32_t quant;  
1299          uint32_t fcode_forward;          uint32_t fcode_forward;
1300          uint32_t fcode_backward;          uint32_t fcode_backward;
1301          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1302          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1303          uint32_t vop_type;          int coding_type;
1304          int success = 0;          int success, output, seen_something;
1305          int output = 0;  
1306          int seen_something = 0;          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1307                    return XVID_ERR_VERSION;
1308    
1309          start_global_timer();          start_global_timer();
1310    
1311          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1312            if ((frame->general & XVID_DISCONTINUITY))
1313                    dec->frames = 0;
1314            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1315    
1316          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          if (frame->length < 0) {        /* decoder flush */
1317                    int ret;
1318                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1319                            we have a reference frame, then outout the reference frame */
1320                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1321                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1322                            dec->frames = 0;
1323                            ret = 0;
1324                    } else {
1325                            if (stats) stats->type = XVID_TYPE_NOTHING;
1326                            ret = XVID_ERR_END;
1327                    }
1328    
1329                    emms();
1330                    stop_global_timer();
1331                    return ret;
1332            }
1333    
1334          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1335    
1336          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1337          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1338          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1339                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1340                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1341                    if (stats) stats->type = XVID_TYPE_NOTHING;
1342                  emms();                  emms();
1343                  return XVID_ERR_OK;                  return 1;       /* one byte consumed */
1344          }          }
1345    
1346  start:          success = 0;
1347          // add by chenm001 <chenm001@163.com>          output = 0;
1348          // for support B-frame to reference last 2 frame          seen_something = 0;
1349    
1350  xxx:  repeat:
         vop_type =  
                 BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
                         &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);  
1351    
1352          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",          coding_type = BitstreamReadHeaders(&bs, dec, &rounding,
1353                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1354    
1355          if (vop_type == - 1)          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%"
1356          {  #if defined(_MSC_VER)
1357        "I64"
1358    #else
1359        "ll"
1360    #endif
1361        "i,  time_pp=%i,  time_bp=%i\n",
1362                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1363    
1364            if (coding_type == -1) { /* nothing */
1365                  if (success) goto done;                  if (success) goto done;
1366                  return XVID_ERR_FAIL;                  if (stats) stats->type = XVID_TYPE_NOTHING;
1367                    emms();
1368                    return BitstreamPos(&bs)/8;
1369          }          }
1370    
1371          if (vop_type == -2 || vop_type == -3)          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1372          {  
1373                  if (vop_type == -3)                  if (coding_type == -3)
1374                          decoder_resize(dec);                          decoder_resize(dec);
1375    
1376                  if (stats)                  if (stats) {
1377                  {                          stats->type = XVID_TYPE_VOL;
                         stats->notify = XVID_DEC_VOL;  
1378                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1379                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1380                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1381                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1382                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1383                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1384                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1385                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1386                          frame->length = BitstreamPos(&bs) / 8;                          emms();
1387                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1388                  }                  }
1389                  goto xxx;                  goto repeat;
1390          }          }
1391    
1392          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) {
1393                    /* 1st frame is not an i-vop */
1394                    goto repeat;
1395            }
1396    
1397            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1398    
1399          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1400          if (dec->packed_mode && vop_type == N_VOP)          if (dec->packed_mode && coding_type == N_VOP) {
1401          {                  if (dec->low_delay_default && dec->frames > 0) {
1402                  if (dec->low_delay_default)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
                 {  
                         image_output(&dec->refn[0], dec->width, dec->height,  
                                                  dec->edged_width, frame->image, frame->stride,  
                                                  frame->colorspace, dec->interlacing);  
1403                          output = 1;                          output = 1;
1404                  }                  }
1405                  /* ignore otherwise */                  /* ignore otherwise */
1406          }          } else if (coding_type != B_VOP) {
1407          else if (vop_type != B_VOP)                  switch(coding_type) {
         {  
                 switch(vop_type)  
                 {  
1408                  case I_VOP :                  case I_VOP :
1409                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1410                          break;                          break;
1411                  case P_VOP :                  case P_VOP :
1412                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1413                                                  fcode_forward, intra_dc_threshold, NULL);                                                  fcode_forward, intra_dc_threshold, NULL);
1414                          break;                          break;
1415                  case S_VOP :                  case S_VOP :
1416                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, quant,
1417                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1418                          break;                          break;
1419                  case N_VOP :                  case N_VOP :
1420                            /* XXX: not_coded vops are not used for forward prediction */
1421                            /* we should not swap(last_mbs,mbs) */
1422                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1423                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1424                          break;                          break;
1425                  }                  }
1426    
                 if (reduced_resolution)  
                 {  
                         image_deblock_rrv(&dec->refn[0], dec->edged_width, dec->mbs,  
                                 (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width);  
                 }  
   
1427                  /* 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 */
1428                  if (!(dec->low_delay_default && dec->packed_mode))                  if (!(dec->low_delay_default && dec->packed_mode)) {
1429                  {                          if (dec->low_delay) {
1430                          if (dec->low_delay)                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
                         {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1431                                  output = 1;                                  output = 1;
1432                          }                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1433                          else if (dec->frames > 0)       /* is the reference frame valid? */                                  /* output the reference frame */
1434                          {                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1435                                  output = 1;                                  output = 1;
1436                          }                          }
1437                  }                  }
1438    
1439                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1440                    dec->is_edged[1] = dec->is_edged[0];
1441                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1442                  mb_swap(&dec->mbs, &dec->last_mbs);                  dec->is_edged[0] = 0;
1443                    SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1444                    dec->last_coding_type = coding_type;
1445    
1446                  dec->frames++;                  dec->frames++;
1447                  seen_something = 1;                  seen_something = 1;
1448    
1449          }else{  /* B_VOP */          }else{  /* B_VOP */
1450    
1451                  if (dec->low_delay)                  if (dec->low_delay) {
1452                  {                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1453                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");                          dec->low_delay = 0;
                         dec->low_delay = 1;  
1454                  }                  }
1455    
1456                  if (dec->time_pp <= dec->time_bp) {                  if (dec->frames < 2) {
1457                            /* attemping to decode a bvop without atleast 2 reference frames */
1458                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1459                                                    "broken b-frame, mising ref frames");
1460                            if (stats) stats->type = XVID_TYPE_NOTHING;
1461                    } else if (dec->time_pp <= dec->time_bp) {
1462                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1463                          decoded in vfw. */                          decoded in vfw. */
1464                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1465                                                  "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);
1466                            if (stats) stats->type = XVID_TYPE_NOTHING;
1467                  }else{                  }else{
1468                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1469                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1470                  }                  }
1471    
                 image_output(&dec->cur, dec->width, dec->height,  
                                          dec->edged_width, frame->image, frame->stride,  
                                          frame->colorspace, dec->interlacing);  
1472                  output = 1;                  output = 1;
1473                  dec->frames++;                  dec->frames++;
1474          }          }
1475    
1476          emms();  #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
   
1477          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1478    #endif
1479    
1480          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1481          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
         {  
1482                  success = 1;                  success = 1;
1483                  goto start;                  goto repeat;
1484          }          }
1485    
1486  done :  done :
1487    
1488          /* low_delay_default mode: if we've gotten here without outputting anything,    /* if we reach here without outputing anything _and_
1489             then output the recently decoded frame, or print an error message  */       the calling application has specified low_delay_default,
1490          if (dec->low_delay_default && output == 0)       we *must* output something.
1491          {       this always occurs on the first call to decode() call
1492                  if (dec->packed_mode && seen_something)       when bframes are present in the bitstream. it may also
1493                  {       occur if no vops  were seen in the bitstream
1494                          /* output the recently decoded frame */  
1495                          image_output(&dec->refn[0], dec->width, dec->height,       if packed_mode is enabled, then we output the recently
1496                                                           dec->edged_width, frame->image, frame->stride,       decoded frame (the very first ivop). otherwise we have
1497                                                           frame->colorspace, dec->interlacing);       nothing to display, and therefore output a black screen.
1498                  }    */
1499                  else    if (dec->low_delay_default && output == 0) {
1500                  {      if (dec->packed_mode && seen_something) {
1501                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1502                                  "warning: nothing to output");      } else {
1503                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1504                                  "bframe decoder lag");                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1505                            if (stats) stats->type = XVID_TYPE_NOTHING;
                         image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1506                  }                  }
                 output = 1;  
         }  
   
         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  
1507          }          }
1508    
1509          emms();          emms();
   
1510          stop_global_timer();          stop_global_timer();
1511    
1512          return XVID_ERR_OK;          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1513  }  }

Legend:
Removed from v.1.37.2.19  
changed lines
  Added in v.1.70

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