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

Legend:
Removed from v.1.18  
changed lines
  Added in v.1.49.2.10

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