[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.17, Thu May 9 00:15:51 2002 UTC revision 1.49.2.16, Tue Oct 14 14:22:45 2003 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-2003 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:  
  *  
  *  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    
31    #ifdef BFRAMES_DEC_DEBUG
32            #define BFRAMES_DEC
33    #endif
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"
 #include "quant/quant_mpeg4.h"  
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
47  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
48    #include "image/reduced.h"
49    #include "image/font.h"
50    
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55    #include "motion/motion.h"
56    #include "motion/gmc.h"
57    
58  #include "image/image.h"  #include "image/image.h"
59  #include "image/colorspace.h"  #include "image/colorspace.h"
60  #include "utils/mem_align.h"  #include "utils/mem_align.h"
61    
62  int decoder_create(XVID_DEC_PARAM * param)  int
63    decoder_resize(DECODER * dec)
64  {  {
65          DECODER * dec;          /* free existing */
66            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
67            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71    
72          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
         if (dec == NULL)  
         {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
73    
74          dec->width = param->width;          if (dec->last_mbs)
75          dec->height = param->height;                  xvid_free(dec->last_mbs);
76            if (dec->mbs)
77                    xvid_free(dec->mbs);
78    
79            /* realloc */
80          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
81          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
82    
83          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
84          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
85    
86          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
87                  xvid_free(dec);                  xvid_free(dec);
88                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
89          }          }
90    
91          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
         {  
92                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
93                  xvid_free(dec);                  xvid_free(dec);
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96          // add by chenm001 <chenm001@163.com>  
97          // for support B-frame to reference last 2 frame          /* Support B-frame to reference last 2 frame */
98          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
         {  
99                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
100                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
101                  xvid_free(dec);                  xvid_free(dec);
102                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
103          }          }
104          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height))          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
         {  
105                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
106                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
107                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 127  Line 109 
109                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
110          }          }
111    
112          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
         if (dec->mbs == NULL)  
         {  
113                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
114                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
115                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
116                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
117                  xvid_free(dec);                  xvid_free(dec);
118                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
119          }          }
120          // add by chenm001 <chenm001@163.com>  
121          // for skip MB flag          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
122          dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
123          if (dec->last_mbs == NULL)                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
124          {                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
125                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
127                    xvid_free(dec);
128                    return XVID_ERR_MEMORY;
129            }
130    
131            dec->mbs =
132                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
133                                            CACHE_LINE);
134            if (dec->mbs == NULL) {
135                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
136                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
137                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
138                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
139                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
140                    xvid_free(dec);
141                    return XVID_ERR_MEMORY;
142            }
143            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
144    
145            /* For skip MB flag */
146            dec->last_mbs =
147                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
148                                            CACHE_LINE);
149            if (dec->last_mbs == NULL) {
150                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
156                  xvid_free(dec);                  xvid_free(dec);
157                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
158          }          }
159    
160            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
161    
162            return 0;
163    }
164    
165    
166    int
167    decoder_create(xvid_dec_create_t * create)
168    {
169            DECODER *dec;
170    
171            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
172                    return XVID_ERR_VERSION;
173    
174            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
175            if (dec == NULL) {
176                    return XVID_ERR_MEMORY;
177            }
178            memset(dec, 0, sizeof(DECODER));
179    
180            create->handle = dec;
181    
182            dec->width = create->width;
183            dec->height = create->height;
184    
185            image_null(&dec->cur);
186            image_null(&dec->refn[0]);
187            image_null(&dec->refn[1]);
188            image_null(&dec->tmp);
189            image_null(&dec->qtmp);
190    
191            /* image based GMC */
192            image_null(&dec->gmc);
193    
194    
195            dec->mbs = NULL;
196            dec->last_mbs = NULL;
197    
198          init_timer();          init_timer();
199    
200          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
201          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
202          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
203            dec->low_delay = 0;
204            dec->packed_mode = 0;
205    
206            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
207    
208          return XVID_ERR_OK;          if (dec->fixed_dimensions)
209                    return decoder_resize(dec);
210            else
211                    return 0;
212  }  }
213    
214    
215  int decoder_destroy(DECODER * dec)  int
216    decoder_destroy(DECODER * dec)
217  {  {
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221            /* image based GMC */
222            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
223    
224          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
225          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
226          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
227            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
228          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
229          xvid_free(dec);          xvid_free(dec);
230    
231          write_timer();          write_timer();
232          return XVID_ERR_OK;          return 0;
233  }  }
234    
235    
236    
237  static const int32_t dquant_table[4] =  static const int32_t dquant_table[4] = {
 {  
238          -1, -2, 1, 2          -1, -2, 1, 2
239  };  };
240    
241    
242    
243    
244  // decode an intra macroblock  /* decode an intra macroblock */
245    void
246  void decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
247                       MACROBLOCK * pMB,                       MACROBLOCK * pMB,
248                       const uint32_t x_pos,                       const uint32_t x_pos,
249                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 196  Line 251 
251                       const uint32_t cbp,                       const uint32_t cbp,
252                       Bitstream * bs,                       Bitstream * bs,
253                       const uint32_t quant,                       const uint32_t quant,
254                       const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
255                                    const unsigned int bound,
256                                    const int reduced_resolution)
257  {  {
258    
259          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 209  Line 266 
266          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
267          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
268    
269            if (reduced_resolution) {
270                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
271                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
272                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
273            }else{
274          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
275          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
276          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
277            }
278    
279          memset(block, 0, 6*64*sizeof(int16_t));         // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
280    
281          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
282                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
283                  int16_t predictors[8];                  int16_t predictors[8];
284                  int start_coeff;                  int start_coeff;
285    
286                  start_timer();                  start_timer();
287                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
288                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
289                  {                  if (!acpred_flag) {
290                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
291                  }                  }
292                  stop_prediction_timer();                  stop_prediction_timer();
293    
294                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
295                          int dc_size;                          int dc_size;
296                          int dc_dif;                          int dc_dif;
297    
298                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);
299                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
300    
301                          if (dc_size > 8)                          if (dc_size > 8) {
302                          {                                  BitstreamSkip(bs, 1);   /* marker */
                                 BitstreamSkip(bs, 1);           // marker  
303                          }                          }
304    
305                          block[i*64 + 0] = dc_dif;                          block[i*64 + 0] = dc_dif;
306                          start_coeff = 1;                          start_coeff = 1;
307                  }  
308                  else                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
309                  {                  } else {
310                          start_coeff = 0;                          start_coeff = 0;
311                  }                  }
312    
313                  start_timer();                  start_timer();
314                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5 - i)))       /* coded */
315                  {                  {
316                          get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff);                          int direction = dec->alternate_vertical_scan ?
317                                    2 : pMB->acpred_directions[i];
318    
319                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
320                  }                  }
321                  stop_coding_timer();                  stop_coding_timer();
322    
# Line 262  Line 325 
325                  stop_prediction_timer();                  stop_prediction_timer();
326    
327                  start_timer();                  start_timer();
328                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
329                  {                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
330                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                  } else {
331                  }                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
                 else  
                 {  
                         dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);  
332                  }                  }
333                  stop_iquant_timer();                  stop_iquant_timer();
334    
335                  start_timer();                  start_timer();
336                  idct(&data[i*64]);                  idct(&data[i*64]);
337                  stop_idct_timer();                  stop_idct_timer();
338    
339          }          }
340    
341          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
342                  next_block = stride;                  next_block = stride;
343                  stride *= 2;                  stride *= 2;
344          }          }
345    
346          start_timer();          start_timer();
347    
348            if (reduced_resolution)
349            {
350                    next_block*=2;
351                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
352                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
353                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
354                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
355                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
356                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
357            }else{
358          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);
359          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);
360          transfer_16to8copy(pY_Cur + next_block,     &data[2*64], stride);          transfer_16to8copy(pY_Cur + next_block,     &data[2*64], stride);
361          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride);
362          transfer_16to8copy(pU_Cur,                  &data[4*64], stride2);          transfer_16to8copy(pU_Cur,                  &data[4*64], stride2);
363          transfer_16to8copy(pV_Cur,                  &data[5*64], stride2);          transfer_16to8copy(pV_Cur,                  &data[5*64], stride2);
364            }
365          stop_transfer_timer();          stop_transfer_timer();
366  }  }
367    
368    
369    
370    
371    /* decode an inter macroblock */
372  #define SIGN(X) (((X)>0)?1:-1)  void
373  #define ABS(X) (((X)>0)?(X):-(X))  decoder_mbinter(DECODER * dec,
 static const uint32_t roundtab[16] =  
 { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
   
 // decode an inter macroblock  
   
 void decoder_mbinter(DECODER * dec,  
374                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
375                       const uint32_t x_pos,                       const uint32_t x_pos,
376                       const uint32_t y_pos,                       const uint32_t y_pos,
377                       const uint32_t acpred_flag,                                  const uint32_t fcode,
378                       const uint32_t cbp,                       const uint32_t cbp,
379                       Bitstream * bs,                       Bitstream * bs,
380                       const uint32_t quant,                       const uint32_t quant,
381                       const uint32_t rounding)                                  const uint32_t rounding,
382                                    const int reduced_resolution)
383  {  {
384    
385          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
# Line 321  Line 387 
387    
388          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
389          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
390          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
391          uint32_t i;          uint32_t i;
392          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
393          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
394    
395          int uv_dx, uv_dy;          int uv_dx, uv_dy;
396            VECTOR mv[4];   /* local copy of mvs */
397    
398            if (reduced_resolution) {
399                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
400                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
401                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
402                    for (i = 0; i < 4; i++) {
403                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
404                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
405                    }
406            } else {
407          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
408          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
409          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
410                    for (i = 0; i < 4; i++)
411                            mv[i] = pMB->mvs[i];
412            }
413    
414          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
415    
416                    uv_dx = mv[0].x / (1 + dec->quarterpel);
417                    uv_dy = mv[0].y / (1 + dec->quarterpel);
418    
419                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
420                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
421    
422                    start_timer();
423                    if (reduced_resolution)
424          {          {
425                  uv_dx = pMB->mvs[0].x;                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
426                  uv_dy = pMB->mvs[0].y;                                                                    mv[0].x, mv[0].y, stride,  rounding);
427                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
428                                                                      uv_dx, uv_dy, stride2, rounding);
429                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
430                                                                      uv_dx, uv_dy, stride2, rounding);
431    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
432          }          }
433          else          else
434          {          {
435                  int sum;                          if(dec->quarterpel) {
436                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
437                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
438                                                                                            mv[0].x, mv[0].y, stride, rounding);
439                            }
440                            else {
441                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
442                                                                              mv[0].x, mv[0].y, stride, rounding);
443                            }
444    
445                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
446                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                                                                    uv_dx, uv_dy, stride2, rounding);
447                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
448                                                                      uv_dx, uv_dy, stride2, rounding);
449          }          }
450                    stop_comp_timer();
451    
452            } else {        /* MODE_INTER4V */
453                    int sum;
454    
455                    if(dec->quarterpel)
456                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
457                    else
458                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
459    
460                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
461    
462                    if(dec->quarterpel)
463                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
464                    else
465                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
466    
467                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
468    
469          start_timer();          start_timer();
470          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                  if (reduced_resolution)
471          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                  {
472          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
473          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
474          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
475          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                                                                    mv[1].x, mv[1].y, stride,  rounding);
476          stop_comp_timer();                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
477                                                                      mv[2].x, mv[2].y, stride,  rounding);
478                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
479                                                                      mv[3].x, mv[3].y, stride,  rounding);
480                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
481                                                                      uv_dx, uv_dy, stride2, rounding);
482                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
483                                                                      uv_dx, uv_dy, stride2, rounding);
484    
485          for (i = 0; i < 6; i++)                          /* set_block(pY_Cur, stride, 32, 32, 127); */
486                    }
487                    else
488          {          {
489                  if (cbp & (1 << (5-i)))                 // coded                          if(dec->quarterpel) {
490                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                                      mv[0].x, mv[0].y, stride,  rounding);
493                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
494                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
495                                                                                      mv[1].x, mv[1].y, stride,  rounding);
496                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
497                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
498                                                                                      mv[2].x, mv[2].y, stride,  rounding);
499                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
500                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
501                                                                                      mv[3].x, mv[3].y, stride,  rounding);
502                            }
503                            else {
504                                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
505                                                                              mv[0].x, mv[0].y, stride,  rounding);
506                                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
507                                                                              mv[1].x, mv[1].y, stride,  rounding);
508                                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
509                                                                              mv[2].x, mv[2].y, stride,  rounding);
510                                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
511                                                                              mv[3].x, mv[3].y, stride,  rounding);
512                            }
513    
514                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,
515                                                                      uv_dx, uv_dy, stride2, rounding);
516                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,
517                                                                      uv_dx, uv_dy, stride2, rounding);
518                    }
519                    stop_comp_timer();
520            }
521    
522            for (i = 0; i < 6; i++) {
523                    int direction = dec->alternate_vertical_scan ? 2 : 0;
524    
525                    if (cbp & (1 << (5 - i)))       /* coded */
526                  {                  {
527                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
528    
529                          start_timer();                          start_timer();
530                          get_inter_block(bs, &block[i*64]);                          get_inter_block(bs, &block[i * 64], direction);
531                          stop_coding_timer();                          stop_coding_timer();
532    
533                          start_timer();                          start_timer();
534                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
535                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
536                            } else {
537                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
538                            }
539                            stop_iquant_timer();
540    
541                            start_timer();
542                            idct(&data[i * 64]);
543                            stop_idct_timer();
544                    }
545            }
546    
547            if (dec->interlacing && pMB->field_dct) {
548                    next_block = stride;
549                    stride *= 2;
550            }
551    
552            start_timer();
553            if (reduced_resolution)
554                          {                          {
555                                  dequant_inter(&data[i*64], &block[i*64], iQuant);                  if (cbp & 32)
556                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
557                    if (cbp & 16)
558                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
559                    if (cbp & 8)
560                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
561                    if (cbp & 4)
562                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
563                    if (cbp & 2)
564                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
565                    if (cbp & 1)
566                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
567                          }                          }
568                          else                          else
569                          {                          {
570                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);                  if (cbp & 32)
571                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
572                    if (cbp & 16)
573                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
574                    if (cbp & 8)
575                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
576                    if (cbp & 4)
577                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
578                    if (cbp & 2)
579                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
580                    if (cbp & 1)
581                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
582            }
583            stop_transfer_timer();
584    }
585    
586    
587    static void
588    decoder_mbgmc(DECODER * dec,
589                                    MACROBLOCK * const pMB,
590                                    const uint32_t x_pos,
591                                    const uint32_t y_pos,
592                                    const uint32_t fcode,
593                                    const uint32_t cbp,
594                                    Bitstream * bs,
595                                    const uint32_t quant,
596                                    const uint32_t rounding,
597                                    const int reduced_resolution)   /* no reduced res support */
598    {
599    
600            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
601            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
602    
603            const uint32_t stride = dec->edged_width;
604            const uint32_t stride2 = stride / 2;
605            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
606            uint32_t i;
607            const uint32_t iQuant = pMB->quant;
608            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
609            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
610            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
611    
612            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
613    
614            start_timer();
615    
616    /* this is where the calculations are done */
617    
618            {       NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
619    
620                            gmc_data->predict_16x16(gmc_data,
621                                            dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
622                                            stride, stride, x_pos, y_pos, rounding);
623    
624                            gmc_data->predict_8x8(gmc_data,
625                                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
626                                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
627                                            stride2, stride2, x_pos, y_pos, rounding);
628    
629                            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
663                          }                          }
664                          stop_iquant_timer();                          stop_iquant_timer();
665    
# Line 385  Line 669 
669                  }                  }
670          }          }
671    
672          if (dec->interlacing && pMB->field_dct)  /* interlace + GMC is this possible ??? */
673          {  /*
674      if (dec->interlacing && pMB->field_dct) {
675                  next_block = stride;                  next_block = stride;
676                  stride *= 2;                  stride *= 2;
677          }          }
678    */
679          start_timer();          start_timer();
680          if (cbp & 32)          if (cbp & 32)
681                  transfer_16to8add(pY_Cur,                  &data[0*64], stride);                  transfer_16to8add(pY_Cur,                  &data[0*64], stride);
# Line 408  Line 693 
693  }  }
694    
695    
696  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
697    decoder_iframe(DECODER * dec,
698                               Bitstream * bs,
699                               int reduced_resolution,
700                               int quant,
701                               int intra_dc_threshold)
702  {  {
703            uint32_t bound;
704          uint32_t x, y;          uint32_t x, y;
705            uint32_t mb_width = dec->mb_width;
706            uint32_t mb_height = dec->mb_height;
707    
708          for (y = 0; y < dec->mb_height; y++)          if (reduced_resolution)
709          {          {
710                  for (x = 0; x < dec->mb_width; x++)                  mb_width = (dec->width + 31) / 32;
711                  {                  mb_height = (dec->height + 31) / 32;
712                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];          }
713    
714            bound = 0;
715    
716            for (y = 0; y < mb_height; y++) {
717                    for (x = 0; x < mb_width; x++) {
718                            MACROBLOCK *mb;
719                          uint32_t mcbpc;                          uint32_t mcbpc;
720                          uint32_t cbpc;                          uint32_t cbpc;
721                          uint32_t acpred_flag;                          uint32_t acpred_flag;
722                          uint32_t cbpy;                          uint32_t cbpy;
723                          uint32_t cbp;                          uint32_t cbp;
724    
725                            while (BitstreamShowBits(bs, 9) == 1)
726                                    BitstreamSkip(bs, 9);
727    
728                            if (check_resync_marker(bs, 0))
729                            {
730                                    bound = read_video_packet_header(bs, dec, 0,
731                                                            &quant, NULL, NULL, &intra_dc_threshold);
732                                    x = bound % mb_width;
733                                    y = bound / mb_width;
734                            }
735                            mb = &dec->mbs[y * dec->mb_width + x];
736    
737                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
738    
739                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
740                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
741                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
742    
743                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
744    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
745                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
746                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
747    
748                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
749                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
750                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
751                                          quant = 31;                                          quant = 31;
752                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
753                                          quant = 1;                                          quant = 1;
754                                  }                                  }
755                          }                          }
756                          mb->quant = quant;                          mb->quant = quant;
757                            mb->mvs[0].x = mb->mvs[0].y =
758                            mb->mvs[1].x = mb->mvs[1].y =
759                            mb->mvs[2].x = mb->mvs[2].y =
760                            mb->mvs[3].x = mb->mvs[3].y =0;
761    
762                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
768                                                            intra_dc_threshold, bound, reduced_resolution);
769    
770                  }                  }
771                    if(dec->out_frm)
772                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
773          }          }
774    
775  }  }
776    
777    
778  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
779    get_motion_vector(DECODER * dec,
780                                      Bitstream * bs,
781                                      int x,
782                                      int y,
783                                      int k,
784                                      VECTOR * ret_mv,
785                                      int fcode,
786                                      const int bound)
787  {  {
788    
789          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
# Line 475  Line 791 
791          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
792          int range = (64 * scale_fac);          int range = (64 * scale_fac);
793    
794          VECTOR pmv[4];          VECTOR pmv;
795          int32_t psad[4];          VECTOR mv;
796    
797          int mv_x, mv_y;          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
         int pmv_x, pmv_y;  
   
   
         get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);  
798    
799          pmv_x = pmv[0].x;          mv.x = get_mv(bs, fcode);
800          pmv_y = pmv[0].y;          mv.y = get_mv(bs, fcode);
801    
802          mv_x = get_mv(bs, fcode);          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);
         mv_y = get_mv(bs, fcode);  
803    
804          mv_x += pmv_x;          mv.x += pmv.x;
805          mv_y += pmv_y;          mv.y += pmv.y;
806    
807          if (mv_x < low)          if (mv.x < low) {
808          {                  mv.x += range;
809                  mv_x += range;          } else if (mv.x > high) {
810          }                  mv.x -= range;
         else if (mv_x > high)  
         {  
                 mv_x -= range;  
811          }          }
812    
813          if (mv_y < low)          if (mv.y < low) {
814          {                  mv.y += range;
815                  mv_y += range;          } else if (mv.y > high) {
816                    mv.y -= range;
817          }          }
818          else if (mv_y > high)  
819          {          ret_mv->x = mv.x;
820                  mv_y -= range;          ret_mv->y = mv.y;
821          }          }
822    
         mv->x = mv_x;  
         mv->y = mv_y;  
823    
 }  
824    
825    
826  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  
827    /* for P_VOP set gmc_warp to NULL */
828    void
829    decoder_pframe(DECODER * dec,
830                               Bitstream * bs,
831                               int rounding,
832                               int reduced_resolution,
833                               int quant,
834                               int fcode,
835                               int intra_dc_threshold,
836                               const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840            uint32_t bound;
841            int cp_mb, st_mb;
842            uint32_t mb_width = dec->mb_width;
843            uint32_t mb_height = dec->mb_height;
844    
845            if (reduced_resolution)
846            {
847                    mb_width = (dec->width + 31) / 32;
848                    mb_height = (dec->height + 31) / 32;
849            }
850    
851          start_timer();          start_timer();
852          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
853                                       dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856          for (y = 0; y < dec->mb_height; y++)          if (gmc_warp)
857          {          {
858                  for (x = 0; x < dec->mb_width; x++)  
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860    /*              {
861                            fprintf(stderr,"GMC parameters acc=%d(-> 1/%d), %d pts!!!\n",
862                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
863                                    dec->sprite_warping_points);
864                    }*/
865    
866                    generate_GMCparameters( dec->sprite_warping_points,
867                                    dec->sprite_warping_accuracy, gmc_warp,
868                                    dec->width, dec->height, &dec->new_gmc_data);
869    
870    /* image warping is done block-based  in decoder_mbgmc(), now */
871            }
872    
873            bound = 0;
874    
875            for (y = 0; y < mb_height; y++) {
876                    cp_mb = st_mb = 0;
877                    for (x = 0; x < mb_width; x++) {
878                            MACROBLOCK *mb;
879    
880                            /* skip stuffing */
881                            while (BitstreamShowBits(bs, 10) == 1)
882                                    BitstreamSkip(bs, 10);
883    
884                            if (check_resync_marker(bs, fcode - 1))
885                  {                  {
886                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                                  bound = read_video_packet_header(bs, dec, fcode - 1,
887                                            &quant, &fcode, NULL, &intra_dc_threshold);
888                                    x = bound % mb_width;
889                                    y = bound / mb_width;
890                            }
891                            mb = &dec->mbs[y * dec->mb_width + x];
892    
893                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
894    
895                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))                 // not_coded                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
896                          if (!(BitstreamGetBit(bs)))                     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
897                          {                          {
898                                  uint32_t mcbpc;                                  uint32_t mcbpc;
899                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 541  Line 901 
901                                  uint32_t cbpy;                                  uint32_t cbpy;
902                                  uint32_t cbp;                                  uint32_t cbp;
903                                  uint32_t intra;                                  uint32_t intra;
904                                    int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
905    
906                                    cp_mb++;
907                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
908                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
909                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
910    
911                                    DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
912                                    DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
913                                  acpred_flag = 0;                                  acpred_flag = 0;
914    
915                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
916    
917                                    if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
918                                            mcsel = BitstreamGetBit(bs);
919    
920                                  if (intra)                                  if (intra)
                                 {  
921                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
   
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
922    
923                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
924                                    DPRINTF(XVID_DEBUG_MB, "cbpy %i  mcsel %i \n", cbpy,mcsel);
925    
926                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
927    
928                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
929                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
930                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
931                                          if (quant > 31)                                          quant += dquant;
932                                          {                                          if (quant > 31) {
933                                                  quant = 31;                                                  quant = 31;
934                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
935                                                  quant = 1;                                                  quant = 1;
936                                          }                                          }
937                                            DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
938                                  }                                  }
939                                  mb->quant = quant;                                  mb->quant = quant;
940    
941                                  if (dec->interlacing)                                  if (dec->interlacing) {
942                                  {                                          if ((cbp || intra) && !mcsel) {
943                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
944                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
945                                            }
946    
947                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
948                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
949                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
950    
951                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
952                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
953                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
954                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
955                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
956                                                  }                                                  }
957                                          }                                          }
958                                  }                                  }
959    
960                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mcsel) {
961                                  {                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
962                                          if (dec->interlacing && mb->field_pred)                                                                  rounding, reduced_resolution);
963                                          {                                          continue;
964                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
965                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
966                                          }  
967                                          else                                          if (dec->interlacing && mb->field_pred) {
968                                          {                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
969                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                                                                    fcode, bound);
970                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
971                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                                                                    fcode, bound);
972                                          }                                          } else {
973                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
974                                                                                      fcode, bound);
975                                                    mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
976                                            }
977                                    } else if (mb->mode == MODE_INTER4V ) {
978    
979                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
980                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
981                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
982                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
983                                    } else                  /* MODE_INTRA, MODE_INTRA_Q */
984                                    {
985                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
986                                                    0;
987                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
988                                                    0;
989                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
990                                                                            intra_dc_threshold, bound, reduced_resolution);
991                                            continue;
992                                  }                                  }
993                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
994                                  {                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
995                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                                                  rounding, reduced_resolution);
996                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);  
                                         get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);  
                                         get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
997                                  }                                  }
998                                  else  // MODE_INTRA, MODE_INTRA_Q                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
999                                  {                                  {
1000                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mode = MODE_NOT_CODED_GMC;
                                         mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
                                         decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);  
                                         continue;  
                                 }  
1001    
1002                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                                  start_timer();
1003    
1004                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1005                                                                    rounding, reduced_resolution);
1006    
1007                                    stop_transfer_timer();
1008    
1009                                    if(dec->out_frm && cp_mb > 0) {
1010                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1011                                      cp_mb = 0;
1012                                    }
1013                                    st_mb = x+1;
1014                          }                          }
1015                          else    // not coded                          else    /* not coded P_VOP macroblock */
1016                          {                          {
                                 //DEBUG2("P-frame MB at (X,Y)=",x,y);  
1017                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
1018    
1019                                  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;
1020                                  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;
1021                                    /* copy macroblock directly from ref to cur */
                                 // copy macroblock directly from ref to cur  
1022    
1023                                  start_timer();                                  start_timer();
1024    
1025                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                                  if (reduced_resolution)
1026                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x),                                  {
1027                                                   dec->edged_width);                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1028                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
                                 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
                                                  dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8),  
1029                                                   dec->edged_width);                                                   dec->edged_width);
1030    
1031                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1032                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x),                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1033                                                   dec->edged_width);                                                                          dec->edged_width/2);
1034    
1035                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1036                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1037                                                                             dec->edged_width/2);
1038                                    }
1039                                    else
1040                                    {
1041                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1042                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1043                                                   dec->edged_width);                                                   dec->edged_width);
1044    
1045                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
# Line 662  Line 1049 
1049                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1050                                                   dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),                                                   dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1051                                                   dec->edged_width/2);                                                   dec->edged_width/2);
1052                                    }
1053    
1054                                  stop_transfer_timer();                                  stop_transfer_timer();
1055    
1056                                    if(dec->out_frm && cp_mb > 0) {
1057                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1058                                      cp_mb = 0;
1059                          }                          }
1060                                    st_mb = x+1;
1061                  }                  }
1062          }          }
1063                    if(dec->out_frm && cp_mb > 0)
1064                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1065            }
1066  }  }
1067    
1068    
1069  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
1070  // decode B-frame motion vector  void
1071  void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv)  get_b_motion_vector(DECODER * dec,
1072                                            Bitstream * bs,
1073                                            int x,
1074                                            int y,
1075                                            VECTOR * mv,
1076                                            int fcode,
1077                                            const VECTOR pmv)
1078  {  {
1079          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
1080          int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
# Line 691  Line 1093 
1093          mv_x += pmv_x;          mv_x += pmv_x;
1094          mv_y += pmv_y;          mv_y += pmv_y;
1095    
1096          if (mv_x < low)          if (mv_x < low) {
         {  
1097                  mv_x += range;                  mv_x += range;
1098          }          } else if (mv_x > high) {
         else if (mv_x > high)  
         {  
1099                  mv_x -= range;                  mv_x -= range;
1100          }          }
1101    
1102          if (mv_y < low)          if (mv_y < low) {
         {  
1103                  mv_y += range;                  mv_y += range;
1104          }          } else if (mv_y > high) {
         else if (mv_y > high)  
         {  
1105                  mv_y -= range;                  mv_y -= range;
1106          }          }
1107    
# Line 714  Line 1110 
1110  }  }
1111    
1112    
1113  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
1114  // decode an B-frame forward & backward inter macroblock  void
1115  void decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1116                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
1117                       const uint32_t x_pos,                       const uint32_t x_pos,
1118                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 741  Line 1137 
1137          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1138          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1139    
1140          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))  
         {  
1141                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1142                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1143    
1144                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;          if (dec->quarterpel) {
1145                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                          uv_dx /= 2;
1146                            uv_dy /= 2;
1147          }          }
         else  
         {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1148    
1149                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;          uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1150                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );          uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
         }  
1151    
1152          start_timer();          start_timer();
1153          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);          if(dec->quarterpel) {
1154          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);                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1155          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);                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1156          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);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1157          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);          } else {
1158          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1159                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1160                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1161                                                          pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1162                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1163                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1164                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1165                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1166            }
1167    
1168            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1169                                                      uv_dx, uv_dy, stride2, 0);
1170            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1171                                                      uv_dx, uv_dy, stride2, 0);
1172          stop_comp_timer();          stop_comp_timer();
1173    
1174          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
1175          {                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1176                  if (cbp & (1 << (5-i)))                 // coded  
1177                  {                  if (cbp & (1 << (5 - i))) {     /* coded */
1178                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear  
1179                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1180    
1181                          start_timer();                          start_timer();
1182                          get_inter_block(bs, &block[i*64]);                          get_inter_block(bs, &block[i * 64], direction);
1183                          stop_coding_timer();                          stop_coding_timer();
1184    
1185                          start_timer();                          start_timer();
1186                          if (dec->quant_type == 0)                          if (dec->quant_type == 0)
1187                          {                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
                                 dequant_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
1188                          else                          else
1189                          {                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
1190                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);  
                         }  
1191                          stop_iquant_timer();                          stop_iquant_timer();
1192    
1193                          start_timer();                          start_timer();
# Line 795  Line 1196 
1196                  }                  }
1197          }          }
1198    
1199          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
1200                  next_block = stride;                  next_block = stride;
1201                  stride *= 2;                  stride *= 2;
1202          }          }
# Line 817  Line 1217 
1217          stop_transfer_timer();          stop_transfer_timer();
1218  }  }
1219    
1220    /* decode an B-frame direct & interpolate macroblock */
1221  // add by MinChen <chenm001@163.com>  void
1222  // decode an B-frame direct &  inter macroblock  decoder_bf_interpolate_mbinter(DECODER * dec,
1223  void decoder_bf_interpolate_mbinter(DECODER * dec,                                                             IMAGE forward,
1224                           IMAGE forward, IMAGE backward,                                                             IMAGE backward,
1225                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
1226                       const uint32_t x_pos,                       const uint32_t x_pos,
1227                       const uint32_t y_pos,                       const uint32_t y_pos,
1228                       const uint32_t cbp,                                                             Bitstream * bs,
1229                       Bitstream * bs )                                                             const int direct)
1230  {  {
1231    
1232          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
# Line 840  Line 1240 
1240          int                     b_uv_dx, b_uv_dy;          int                     b_uv_dx, b_uv_dy;
1241          uint32_t        i;          uint32_t        i;
1242          uint8_t         *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t         *pY_Cur, *pU_Cur, *pV_Cur;
1243        const uint32_t cbp = pMB->cbp;
1244    
1245          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1246          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1247          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1248    
1249          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))  
1250          {          if (!direct) {
1251                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1252                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1253    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1254                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1255                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1256    
1257                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
                 b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
         }  
         else  
1258          {          {
1259                            uv_dx /= 2;
1260                            uv_dy /= 2;
1261    
1262                            b_uv_dx /= 2;
1263                            b_uv_dy /= 2;
1264                    }
1265    
1266                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1267                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1268    
1269                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1270                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1271            } else {
1272                  int sum;                  int sum;
1273    
1274                    if(dec->quarterpel)
1275                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1276                    else
1277                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1278    
1279                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1280    
1281                    if(dec->quarterpel)
1282                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1283                    else
1284                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
                 uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1285    
1286                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1287    
1288    
1289                    if(dec->quarterpel)
1290                            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);
1291                    else
1292                  sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                  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 == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1293    
1294                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1295    
1296                    if(dec->quarterpel)
1297                            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);
1298                    else
1299                  sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                  sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1300                  b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
1301                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1302          }          }
1303    
1304    
1305          start_timer();          start_timer();
1306          interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);          if(dec->quarterpel) {
1307          interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);                  if(!direct)
1308          interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1309          interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  0);                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1310          interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1311          interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);                  else {
1312                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1313                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1314          interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos,     16*y_pos    , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride,  0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1315          interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos    , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,  0);                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1316          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);                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1317          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);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1318          interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1319          interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1320                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1321          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos    , stride);                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1322          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos    , stride);                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1323          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos + 8, stride);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1324          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride);                  }
1325          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos,      8*y_pos,      stride2);          }
1326          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos,      8*y_pos,      stride2);          else {
1327                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1328                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1329                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1330                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1331                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1332                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1333                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1334                                                              16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1335                                                              0);
1336            }
1337    
1338            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1339                                                      uv_dy, stride2, 0);
1340            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1341                                                      uv_dy, stride2, 0);
1342    
1343    
1344            if(dec->quarterpel) {
1345                    if(!direct)
1346                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1347                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1348                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1349                    else {
1350                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1351                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1352                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1353                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1354                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1355                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1356                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1357                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1358                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1359                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1360                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1361                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1362                    }
1363            }
1364            else {
1365                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1366                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1367                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1368                                                              16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1369                                                              0);
1370                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1371                                                              16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1372                                                              stride, 0);
1373                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1374                                                              16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1375                                                              stride, 0);
1376            }
1377    
1378            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1379                                                      b_uv_dx, b_uv_dy, stride2, 0);
1380            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1381                                                      b_uv_dx, b_uv_dy, stride2, 0);
1382    
1383            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1384                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1385                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1386                                                    stride, 1, 8);
1387    
1388            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1389                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1390                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1391                                                    stride, 1, 8);
1392    
1393            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1394                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1395                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1396                                                    stride, 1, 8);
1397    
1398            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1399                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1400                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1401                                                    stride, 1, 8);
1402    
1403            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1404                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1405                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1406                                                    stride2, 1, 8);
1407    
1408            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1409                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1410                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1411                                                    stride2, 1, 8);
1412    
1413          stop_comp_timer();          stop_comp_timer();
1414    
1415          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
1416          {                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1417                  if (cbp & (1 << (5-i)))                 // coded  
1418                  {                  if (cbp & (1 << (5 - i))) {     /* coded */
1419                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1420    
1421                          start_timer();                          start_timer();
1422                          get_inter_block(bs, &block[i*64]);                          get_inter_block(bs, &block[i * 64], direction);
1423                          stop_coding_timer();                          stop_coding_timer();
1424    
1425                          start_timer();                          start_timer();
1426                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
1427                          {                                  dequant_h263_inter(&data[i * 64], &block[i * 64], iQuant);
1428                                  dequant_inter(&data[i*64], &block[i*64], iQuant);                          } else {
1429                          }                                  dequant_mpeg_inter(&data[i * 64], &block[i * 64], iQuant);
                         else  
                         {  
                                 dequant4_inter(&data[i*64], &block[i*64], iQuant);  
1430                          }                          }
1431                          stop_iquant_timer();                          stop_iquant_timer();
1432    
# Line 928  Line 1436 
1436                  }                  }
1437          }          }
1438    
1439          if (dec->interlacing && pMB->field_dct)          if (dec->interlacing && pMB->field_dct) {
         {  
1440                  next_block = stride;                  next_block = stride;
1441                  stride *= 2;                  stride *= 2;
1442          }          }
# Line 951  Line 1458 
1458  }  }
1459    
1460    
1461  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
1462  // for decode B-frame dbquant  int32_t __inline
1463  int32_t __inline get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1464  {  {
1465          if (!BitstreamGetBit(bs))               // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1466                  return(0);                  return(0);
1467          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1468                  return(-2);                  return(-2);
1469          else          else                           /* '11' */
1470                  return(2);                                      // '11'                  return (2);
1471  }  }
1472    
1473  // add by MinChen <chenm001@163.com>  /*
1474  // for decode B-frame mb_type   * For decode B-frame mb_type
1475  // bit   ret_value   * bit   ret_value
1476  // 1        0   * 1        0
1477  // 01       1   * 01       1
1478  // 001      2   * 001      2
1479  // 0001     3   * 0001     3
1480  int32_t __inline get_mbtype(Bitstream * bs)   */
1481    int32_t __inline
1482    get_mbtype(Bitstream * bs)
1483  {  {
1484          int32_t mb_type;          int32_t mb_type;
1485    
1486          for(mb_type=0;mb_type<=3;mb_type++){          for(mb_type=0;mb_type<=3;mb_type++){
1487                  if  (BitstreamGetBit(bs))                  if  (BitstreamGetBit(bs))
1488                          break;                          break;
# Line 984  Line 1494 
1494                  return(-1);                  return(-1);
1495  }  }
1496    
1497  void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward)  void
1498    decoder_bframe(DECODER * dec,
1499                               Bitstream * bs,
1500                               int quant,
1501                               int fcode_forward,
1502                               int fcode_backward)
1503  {  {
   
1504          uint32_t        x, y;          uint32_t        x, y;
1505          VECTOR          mv, zeromv;          VECTOR mv;
1506            const VECTOR zeromv = {0,0};
1507    #ifdef BFRAMES_DEC_DEBUG
1508            FILE *fp;
1509            static char first=0;
1510    #define BFRAME_DEBUG    if (!first && fp){ \
1511                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mode,mb->cbp); \
1512            }
1513    #endif
1514    
1515          start_timer();          start_timer();
1516          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1517          //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1518            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1519                                       dec->width, dec->height);
1520          stop_edges_timer();          stop_edges_timer();
1521    
1522    #ifdef BFRAMES_DEC_DEBUG
1523          for (y = 0; y < dec->mb_height; y++)          if (!first){
1524          {                  fp=fopen("C:\\XVIDDBG.TXT","w");
1525                  // Initialize Pred Motion Vector          }
1526                  dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;  #endif
1527                  for (x = 0; x < dec->mb_width; x++)  
1528                  {          for (y = 0; y < dec->mb_height; y++) {
1529                    /* Initialize Pred Motion Vector */
1530                    dec->p_fmv = dec->p_bmv = zeromv;
1531                    for (x = 0; x < dec->mb_width; x++) {
1532                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
1533                          MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];                          MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];
1534    
1535                          mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0;                          mv =
1536                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1537                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1538    
1539                            /*
1540                             * skip if the co-located P_VOP macroblock is not coded
1541                             * if not codec in co-located S_VOP macroblock is _not_
1542                             * automatically skipped
1543                             */
1544    
                         // the last P_VOP is skip macroblock ?  
1545                          if (last_mb->mode == MODE_NOT_CODED){                          if (last_mb->mode == MODE_NOT_CODED){
1546                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
                                 mb->mb_type = MODE_FORWARD;  
1547                                  mb->cbp = 0;                                  mb->cbp = 0;
1548                                    mb->mode = MODE_FORWARD;
1549                                    mb->quant = last_mb->quant;
1550                                    /*
1551                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1552                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1553                                  mb->quant = 8;                                  */
1554    
1555                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1556                                  continue;                                  continue;
1557                          }                          }
1558    
1559                          //t=BitstreamShowBits(bs,32);                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
   
                         if (!BitstreamGetBit(bs)){      // modb=='0'  
1560                                  const uint8_t modb2=BitstreamGetBit(bs);                                  const uint8_t modb2=BitstreamGetBit(bs);
1561    
1562                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1563    
1564                                  if (!modb2){    // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1565                                          mb->cbp = BitstreamGetBits(bs,6);                                          mb->cbp = BitstreamGetBits(bs,6);
1566                                  } else {                                  } else {
1567                                          mb->cbp = 0;                                          mb->cbp = 0;
1568                                  }                                  }
1569                                  if (mb->mb_type && mb->cbp){                                  if (mb->mode && mb->cbp) {
1570                                          quant += get_dbquant(bs);                                          quant += get_dbquant(bs);
1571    
1572                                          if (quant > 31)                                          if (quant > 31) {
                                         {  
1573                                                  quant = 31;                                                  quant = 31;
1574                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
1575                                                  quant = 1;                                                  quant = 1;
1576                                          }                                          }
                                 } else {  
                                         quant = 8;  
1577                                  }                                  }
1578                                  mb->quant = quant;  
1579                                    if (dec->interlacing) {
1580                                            if (mb->cbp) {
1581                                                    mb->field_dct = BitstreamGetBit(bs);
1582                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1583                                            }
1584    
1585                                            if (mb->mode) {
1586                                                    mb->field_pred = BitstreamGetBit(bs);
1587                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1588    
1589                                                    if (mb->field_pred) {
1590                                                            mb->field_for_top = BitstreamGetBit(bs);
1591                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1592                                                            mb->field_for_bot = BitstreamGetBit(bs);
1593                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1594                                                    }
1595                                            }
1596                                    }
1597    
1598                          } else {                          } else {
1599                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1600                                  mb->cbp = 0;                                  mb->cbp = 0;
1601                          }                          }
1602    
1603                          mb->mode = MODE_INTER;                          mb->quant = quant;
1604                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /* DEBUG1("Switch bm_type=",mb->mode); */
1605    
1606                          switch(mb->mb_type)  #ifdef BFRAMES_DEC_DEBUG
1607                          {          BFRAME_DEBUG
1608    #endif
1609    
1610                            switch (mb->mode) {
1611                          case MODE_DIRECT:                          case MODE_DIRECT:
1612                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1613    
1614                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1615                                  {       // Because this file is a C file not C++ so I use '{' to define var                                  {
1616                                          const int64_t   TRB=dec->time_pp-dec->time_bp,                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
                                                                         TRD=dec->time_pp;  
1617                                          int i;                                          int i;
1618    
1619                                          for(i=0;i<4;i++){                                          for(i=0;i<4;i++){
1620                                                  mb->mvs[i].x    = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x);                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1621                                                  mb->b_mvs[i].x  = (int32_t)((mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x);                                                                        / TRD + mv.x);
1622                                                  mb->mvs[i].y    = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y);                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1623                                                  mb->b_mvs[i].y  = (int32_t)((mb->mvs[0].y==0)?((TRB-TRD)*last_mb->mvs[i].y)/TRD:mb->mvs[i].y-last_mb->mvs[i].y);                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)
1624                                                                                      / TRD
1625                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1626                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1627                                                                          / TRD + mv.y);
1628                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1629                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1630                                                                                      / TRD
1631                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1632                                          }                                          }
1633                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1634                                  }                                  }
1635                                  mb->mode = MODE_INTER4V;                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1636                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);                                                                                             mb, x, y, bs, 1);
1637                                  break;                                  break;
1638    
1639                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1640                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1641                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                                          dec->p_fmv);
1642                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1643    
1644                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1645                                  dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x;                                                                          fcode_backward, dec->p_bmv);
1646                                  dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y;                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1647                                            mb->b_mvs[3] = mb->b_mvs[0];
1648                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);  
1649                                  //DEBUG("B-frame Bidir!\n");                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1650                                                                                               mb, x, y, bs, 0);
1651                                    /* DEBUG("B-frame Bidir!\n"); */
1652                                  break;                                  break;
1653    
1654                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1655                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1656                                  dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                                          dec->p_bmv);
1657                                  dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1658    
1659                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1660                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1661                                  break;                                  break;
1662    
1663                          case MODE_FORWARD:                          case MODE_FORWARD:
1664                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1665                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                                          dec->p_fmv);
1666                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1667    
1668                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1669                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1670                                  break;                                  break;
1671    
1672                          default:                          default:
1673                                  DEBUG1("Not support B-frame mb_type =",mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1674                            }
1675                    } /* End of for */
1676                          }                          }
1677    
1678                  }       // end of FOR  #ifdef BFRAMES_DEC_DEBUG
1679            if (!first){
1680                    first=1;
1681                    if (fp)
1682                            fclose(fp);
1683          }          }
1684    #endif
1685  }  }
1686    
1687  // swap two MACROBLOCK array  
1688  void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2)  
1689    /* perform post processing if necessary, and output the image */
1690    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1691                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1692    {
1693    
1694    
1695            image_output(img, dec->width, dec->height,
1696                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1697                                     frame->output.csp, dec->interlacing);
1698    
1699            if (stats)
1700  {  {
1701          MACROBLOCK *temp=*mb1;                  stats->type = coding2type(coding_type);
1702          *mb1=*mb2;                  stats->data.vop.time_base = (int)dec->time_base;
1703          *mb2=temp;                  stats->data.vop.time_increment = 0;     /* XXX: todo */
1704            }
1705  }  }
1706    
1707  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  
1708    int
1709    decoder_decode(DECODER * dec,
1710                               xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1711  {  {
1712    
1713          Bitstream bs;          Bitstream bs;
1714          uint32_t rounding;          uint32_t rounding;
1715            uint32_t reduced_resolution;
1716          uint32_t quant;          uint32_t quant;
1717          uint32_t fcode_forward;          uint32_t fcode_forward;
1718          uint32_t fcode_backward;          uint32_t fcode_backward;
1719          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1720          uint32_t vop_type;          WARPPOINTS gmc_warp;
1721            int coding_type;
1722            int success, output, seen_something;
1723    
1724            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1725                    return XVID_ERR_VERSION;
1726    
1727          start_global_timer();          start_global_timer();
1728    
1729            dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1730            if ((frame->general & XVID_DISCONTINUITY))
1731                    dec->frames = 0;
1732            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1733    
1734            if (frame->length < 0)  /* decoder flush */
1735            {
1736            int ret;
1737                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1738                        we have a reference frame, then outout the reference frame */
1739                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1740                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1741                dec->frames = 0;
1742                ret = 0;
1743            }else{
1744                if (stats) stats->type = XVID_TYPE_NOTHING;
1745                ret = XVID_ERR_END;
1746            }
1747    
1748                    emms();
1749                    stop_global_timer();
1750                    return ret;
1751            }
1752    
1753          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1754    
1755          // add by chenm001 <chenm001@163.com>          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1756          // for support B-frame to reference last 2 frame          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1757          dec->frames ++;          {
1758          vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold);                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1759                                             (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1760                    if (stats) stats->type = XVID_TYPE_NOTHING;
1761                    emms();
1762                    return 1;   /* one byte consumed */
1763            }
1764    
1765            success = 0;
1766            output = 0;
1767            seen_something = 0;
1768    
1769    repeat:
1770    
1771          dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0;          // init pred vector to 0          coding_type =   BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1772          switch (vop_type)                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1773    
1774            DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1775                                                            coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1776    
1777            if (coding_type == -1) /* nothing */
1778          {          {
1779          case P_VOP :                  if (success) goto done;
1780                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold);          if (stats) stats->type = XVID_TYPE_NOTHING;
1781                  DEBUG1("P_VOP  Time=",dec->time);                  emms();
1782                  break;          return BitstreamPos(&bs)/8;
1783            }
1784    
1785          case I_VOP :          if (coding_type == -2 || coding_type == -3)   /* vol and/or resize */
1786                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);          {
1787                  DEBUG1("I_VOP  Time=",dec->time);                  if (coding_type == -3)
1788                  break;                          decoder_resize(dec);
1789    
1790          case B_VOP :                  if (stats)
1791                  if (dec->time_pp > dec->time_bp){                  {
1792                          DEBUG1("B_VOP  Time=",dec->time);                          stats->type = XVID_TYPE_VOL;
1793                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          stats->data.vol.general = 0;
1794                  } else {                          /*XXX: if (dec->interlacing)
1795                          DEBUG("broken B-frame!");                                  stats->data.vol.general |= ++INTERLACING; */
1796                            stats->data.vol.width = dec->width;
1797                            stats->data.vol.height = dec->height;
1798                            stats->data.vol.par = dec->aspect_ratio;
1799                            stats->data.vol.par_width = dec->par_width;
1800                            stats->data.vol.par_height = dec->par_height;
1801                            emms();
1802                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1803                    }
1804                    goto repeat;
1805                  }                  }
                 break;  
1806    
1807          case N_VOP :    // vop not coded          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
                 break;  
1808    
1809          default :          /* packed_mode: special-N_VOP treament */
1810                  return XVID_ERR_FAIL;          if (dec->packed_mode && coding_type == N_VOP)
1811            {
1812                    if (dec->low_delay_default && dec->frames > 0)
1813                    {
1814                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1815                            output = 1;
1816                    }
1817                    /* ignore otherwise */
1818            }
1819            else if (coding_type != B_VOP)
1820            {
1821                    switch(coding_type)
1822                    {
1823                    case I_VOP :
1824                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1825                            break;
1826                    case P_VOP :
1827                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1828                                                    fcode_forward, intra_dc_threshold, NULL);
1829                            break;
1830                    case S_VOP :
1831                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1832                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1833                            break;
1834                    case N_VOP :
1835                            /* XXX: not_coded vops are not used for forward prediction */
1836                            /* we should not swap(last_mbs,mbs) */
1837                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1838                            break;
1839          }          }
1840    
1841          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution)
1842                    {
1843                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1844                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1845                                    16, 0);
1846                    }
1847    
1848          // test if no B_VOP                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1849          if (dec->low_delay){                  if (!(dec->low_delay_default && dec->packed_mode))
             image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
                                      frame->image, frame->stride, frame->colorspace);  
         } else {  
                 if (dec->frames >= 1){  
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP))  
1850                          {                          {
1851                                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                          if (dec->low_delay)
1852                                                   frame->image, frame->stride, frame->colorspace);                          {
1853                          } else if (vop_type == B_VOP) {                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1854                                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                  output = 1;
                                                  frame->image, frame->stride, frame->colorspace);  
1855                          }                          }
1856                          stop_conv_timer();                          else if (dec->frames > 0)       /* is the reference frame valid? */
1857                            {
1858                                    /* output the reference frame */
1859                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1860                                    output = 1;
1861                  }                  }
1862          }          }
1863          if (vop_type==I_VOP || vop_type==P_VOP){  
1864                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1865                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1866                  // swap MACROBLOCK          SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1867                  if (dec->low_delay && vop_type==P_VOP)                  dec->last_reduced_resolution = reduced_resolution;
1868                          mb_swap(&dec->mbs, &dec->last_mbs);          dec->last_coding_type = coding_type;
1869    
1870                    dec->frames++;
1871                    seen_something = 1;
1872    
1873            }else{  /* B_VOP */
1874    
1875                    if (dec->low_delay)
1876                    {
1877                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1878                            dec->low_delay = 1;
1879          }          }
1880    
1881          emms();                  if (dec->frames < 2)
1882                    {
1883                            /* attemping to decode a bvop without atleast 2 reference frames */
1884                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1885                                                    "broken b-frame, mising ref frames");
1886                    }else if (dec->time_pp <= dec->time_bp) {
1887                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1888                            decoded in vfw. */
1889                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1890                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1891                    }else{
1892                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1893                    }
1894    
1895                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1896                    output = 1;
1897                    dec->frames++;
1898            }
1899    
1900            BitstreamByteAlign(&bs);
1901    
1902            /* low_delay_default mode: repeat in packed_mode */
1903            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1904            {
1905                    success = 1;
1906                    goto repeat;
1907            }
1908    
1909    done :
1910    
1911            /* low_delay_default mode: if we've gotten here without outputting anything,
1912               then output the recently decoded frame, or print an error message  */
1913            if (dec->low_delay_default && output == 0)
1914            {
1915                    if (dec->packed_mode && seen_something)
1916                    {
1917                            /* output the recently decoded frame */
1918                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1919                    }
1920                    else
1921                    {
1922                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1923                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1924                                    "warning: nothing to output");
1925                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1926                                    "bframe decoder lag");
1927    
1928                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1929                            if (stats) stats->type = XVID_TYPE_NOTHING;
1930    
1931                    }
1932            }
1933    
1934            emms();
1935          stop_global_timer();          stop_global_timer();
1936    
1937          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1938  }  }

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.49.2.16

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