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

Diff of /xvidcore/src/decoder.c

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

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

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

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