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

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.37.2.26

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