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

Diff of /xvidcore/src/decoder.c

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

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

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

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