[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.4, Wed Mar 20 14:02:59 2002 UTC revision 1.37.2.28, Sat Jan 11 20:37:46 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  -  Decoder main module  -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *      This program is an implementation of a part of one or more MPEG-4
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
# Line 12  Line 12 
12   *      editors and their companies, will have no liability for use of this   *      editors and their companies, will have no liability for use of this
13   *      software or modifications or derivatives thereof.   *      software or modifications or derivatives thereof.
14   *   *
15   *      This program is xvid_free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
16   *      it under the terms of the GNU General Public License as published by   *      it under the terms of the GNU General Public License as published by
17   *      the xvid_free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
18   *      (at your option) any later version.   *      (at your option) any later version.
19   *   *
20   *      This program is distributed in the hope that it will be useful,   *      This program is distributed in the hope that it will be useful,
# Line 23  Line 23 
23   *      GNU General Public License for more details.   *      GNU General Public License for more details.
24   *   *
25   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
26   *      along with this program; if not, write to the xvid_free Software   *  along with this program; if not, write to the Free Software
27   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   *************************************************************************/   *************************************************************************/
30    
# Line 32  Line 32 
32   *   *
33   *      History:   *      History:
34   *   *
35     *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
36     *              MinChen <chenm001@163.com>
37     *  10.07.2002  added BFRAMES_DEC_DEBUG support
38     *              Fix a little bug for low_delay flage
39     *              MinChen <chenm001@163.com>
40     *  28.06.2002  added basic resync support to iframe/pframe_decode()
41     *  22.06.2002  added primative N_VOP support
42     *                              #define BFRAMES_DEC now enables Minchen's bframe decoder
43     *  08.05.2002  add low_delay support for B_VOP decode
44     *              MinChen <chenm001@163.com>
45     *  05.05.2002  fix some B-frame decode problem
46     *  02.05.2002  add B-frame decode support(have some problem);
47     *              MinChen <chenm001@163.com>
48     *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
49     *  29.03.2002  interlacing fix - compensated block wasn't being used when
50     *              reconstructing blocks, thus artifacts
51     *              interlacing speedup - used transfers to re-interlace
52     *              interlaced decoding should be as fast as progressive now
53     *  26.03.2002  interlacing support - moved transfers outside decode loop
54   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block
55   *      22.12.2001      block based interpolation   *  22.12.2001  lock based interpolation
56   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
57   *   *
58     *  $Id$
59     *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  // memset  #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 54  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);
107          param->handle = dec;  
108            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
109    
110            if (dec->last_mbs)
111                    xvid_free(dec->last_mbs);
112            if (dec->mbs)
113                    xvid_free(dec->mbs);
114    
115          dec->width = param->width;          /* realloc */
         dec->height = param->height;  
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 84  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, 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          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          // add by chenm001 <chenm001@163.com>
135          if (dec->mbs == NULL)          // for support B-frame to reference last 2 frame
136          {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
137                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
138                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
139                    xvid_free(dec);
140                    return XVID_ERR_MEMORY;
141            }
142            if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    xvid_free(dec);
147                    return XVID_ERR_MEMORY;
148            }
149    
150            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
151                    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);
174                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
175                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
176                    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);
179                    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>
184            // for skip MB flag
185            dec->last_mbs =
186                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
187                                            CACHE_LINE);
188            if (dec->last_mbs == NULL) {
189                    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);
192                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
193                    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();
         create_vlc_tables();  
235    
236            // add by chenm001 <chenm001@163.com>
237            // for support B-frame to save reference frame's time
238            dec->frames = 0;
239            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);
256          xvid_free(dec->mbs);          xvid_free(dec->mbs);
257          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);  
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);
261            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
262            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    
         destroy_vlc_tables();  
   
267          write_timer();          write_timer();
268          return XVID_ERR_OK;          return XVID_ERR_OK;
269  }  }
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    
277    
278    
279    
280  // decode an intra macroblock  // decode an intra macroblock
281    
282  void decoder_mbintra(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int intra_dc_threshold)  void
283  {  decoder_mbintra(DECODER * dec,
284          uint32_t k;                                  MACROBLOCK * pMB,
285                                    const uint32_t x_pos,
286                                    const uint32_t y_pos,
287                                    const uint32_t acpred_flag,
288                                    const uint32_t cbp,
289                                    Bitstream * bs,
290                                    const uint32_t quant,
291                                    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);
297            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
298    
299            uint32_t stride = dec->edged_width;
300            uint32_t stride2 = stride / 2;
301            uint32_t next_block = stride * 8;
302            uint32_t i;
303            uint32_t iQuant = pMB->quant;
304            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);
312                    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);
314            }
315    
316          for (k = 0; k < 6; k++)          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
317          {  
318                  uint32_t dcscalar;          for (i = 0; i < 6; i++) {
319                  CACHE_ALIGN int16_t block[64];                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
                 CACHE_ALIGN int16_t data[64];  
320                  int16_t predictors[8];                  int16_t predictors[8];
321                  int start_coeff;                  int start_coeff;
322    
                 dcscalar = get_dc_scaler(mb->quant, k < 4);  
   
323                  start_timer();                  start_timer();
324                  predict_acdc(dec->mbs, x, y, dec->mb_width, k, block, mb->quant, dcscalar, 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                          mb->acpred_directions[k] = 0;                          pMB->acpred_directions[i] = 0;
328                  }                  }
329                  stop_prediction_timer();                  stop_prediction_timer();
330    
331                  memset(block, 0, 64*sizeof(int16_t));           // clear                  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 = k < 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[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-k)))                 // coded                  if (cbp & (1 << (5 - i)))       // coded
352                  {                  {
353                          get_intra_block(bs, block, mb->acpred_directions[k], 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    
360                  start_timer();                  start_timer();
361                  add_acdc(mb, k, block, dcscalar, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors);
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);
367                          dequant_intra(data, block, mb->quant, dcscalar);                  } else {
368                  }                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
                 else  
                 {  
                         dequant4_intra(data, block, mb->quant, dcscalar);  
369                  }                  }
370                  stop_iquant_timer();                  stop_iquant_timer();
371    
372                  start_timer();                  start_timer();
373                  idct(data);                  idct(&data[i * 64]);
374                  stop_idct_timer();                  stop_idct_timer();
375    
                 start_timer();  
                 if (k < 4)  
                 {  
                         transfer_16to8copy(dec->cur.y + (16*y*dec->edged_width) + 16*x + (4*(k&2)*dec->edged_width) + 8*(k&1), data, dec->edged_width);  
376                  }                  }
377                  else if (k == 4)  
378                  {          if (dec->interlacing && pMB->field_dct) {
379                          transfer_16to8copy(dec->cur.u+ 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));                  next_block = stride;
380                    stride *= 2;
381                  }                  }
382                  else    // if (k == 5)  
383            start_timer();
384    
385            if (reduced_resolution)
386                  {                  {
387                          transfer_16to8copy(dec->cur.v + 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));                  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);
396                    transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
397                    transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
398                    transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
399                    transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
400                    transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
401                  }                  }
402                  stop_transfer_timer();                  stop_transfer_timer();
403          }          }
 }  
404    
405    
406    
407    
408    // decode an inter macroblock
409    
410  #define SIGN(X) (((X)>0)?1:-1)  void
411  #define ABS(X) (((X)>0)?(X):-(X))  decoder_mbinter(DECODER * dec,
412  static const uint32_t roundtab[16] =                                  const MACROBLOCK * pMB,
413                  { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };                                  const uint32_t x_pos,
414                                    const uint32_t y_pos,
415                                    const uint32_t acpred_flag,
416                                    const uint32_t cbp,
417                                    Bitstream * bs,
418                                    const uint32_t quant,
419                                    const uint32_t rounding,
420                                    const int reduced_resolution)
421    {
422    
423            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
424            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
425    
426            uint32_t stride = dec->edged_width;
427            uint32_t stride2 = stride / 2;
428            uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
429            uint32_t i;
430            uint32_t iQuant = pMB->quant;
431            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
432    
433            int uv_dx, uv_dy;
434            VECTOR mv[4];   /* local copy of mvs */
435    
436  // decode an inter macroblock          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);
446                    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);
448                    for (i = 0; i < 4; i++)
449                            mv[i] = pMB->mvs[i];
450            }
451    
452  void decoder_mbinter(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int rounding)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
453  {  
454          const uint32_t stride = dec->edged_width;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
455          const uint32_t stride2 = dec->edged_width / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         int uv_dx, uv_dy;  
         uint32_t k;  
456    
457          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                  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 = mb->mvs[0].x;                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
464                  uv_dy = mb->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 = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->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                  sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y;                          }
478                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                          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          start_timer();                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
484          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x,     16*y    , mb->mvs[0].x, mb->mvs[0].y, stride,  rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
485          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y    , mb->mvs[1].x, mb->mvs[1].y, stride,  rounding);                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
486          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x,     16*y + 8, mb->mvs[2].x, mb->mvs[2].y, stride,  rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
487          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y + 8, mb->mvs[3].x, mb->mvs[3].y, stride,  rounding);                  }
         interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);  
         interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);  
488          stop_comp_timer();          stop_comp_timer();
489    
490            } else {        /* MODE_INTER4V */
491                    int sum;
492    
493          for (k = 0; k < 6; k++)                  if(dec->quarterpel)
494          {                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
495                  CACHE_ALIGN int16_t block[64];                  else
496                  CACHE_ALIGN int16_t data[64];                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
497    
498                  if (cbp & (1 << (5-k)))                 // coded                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
                 {  
                         memset(block, 0, 64 * sizeof(int16_t));         // clear  
499    
500                          start_timer();                  if(dec->quarterpel)
501                          get_inter_block(bs, block);                          sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
502                          stop_coding_timer();                  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                          if (dec->quant_type == 0)                  if (reduced_resolution)
509                          {                          {
510                                  dequant_inter(data, block, mb->quant);                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
511                                                                      mv[0].x, mv[0].y, stride,  rounding);
512                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
513                                                                      mv[1].x, mv[1].y, stride,  rounding);
514                            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                            // set_block(pY_Cur, stride, 32, 32, 127);
524                          }                          }
525                          else                          else
526                          {                          {
527                                  dequant4_inter(data, block, mb->quant);                          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
564                    {
565                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
566    
567                            start_timer();
568                            get_inter_block(bs, &block[i * 64], direction);
569                            stop_coding_timer();
570    
571                            start_timer();
572                            if (dec->quant_type == 0) {
573                                    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();                          stop_iquant_timer();
578    
579                          start_timer();                          start_timer();
580                          idct(data);                          idct(&data[i * 64]);
581                          stop_idct_timer();                          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();                          start_timer();
591                          if (k < 4)          if (reduced_resolution)
592                          {                          {
593                                  transfer_16to8add(dec->cur.y + (16*y + 4*(k&2))*stride + 16*x + 8*(k&1), data, stride);                  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 if (k == 4)          else
607                          {                          {
608                                  transfer_16to8add(dec->cur.u + 8*y*stride2 + 8*x, data, stride2);                  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                          else // k == 5          stop_transfer_timer();
                         {  
                                 transfer_16to8add(dec->cur.v + 8*y*stride2 + 8*x, data, stride2);  
622                          }                          }
623    
624    
625    static void
626    decoder_mbgmc(DECODER * dec,
627                                    MACROBLOCK * const pMB,
628                                    const uint32_t x_pos,
629                                    const uint32_t y_pos,
630                                    const uint32_t acpred_flag,
631                                    const uint32_t cbp,
632                                    Bitstream * bs,
633                                    const uint32_t quant,
634                                    const uint32_t rounding,
635                                    const int reduced_resolution)   /* no reduced res support */
636    {
637    
638            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
639            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
640    
641            const uint32_t stride = dec->edged_width;
642            const uint32_t stride2 = stride / 2;
643            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
644            uint32_t i;
645            const uint32_t iQuant = pMB->quant;
646            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
647            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
648            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
649    
650            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
651    
652            start_timer();
653            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
654            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
655            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
656                          stop_transfer_timer();                          stop_transfer_timer();
657    
658            if (!cbp) return;
659    
660            for (i = 0; i < 6; i++) {
661                    int direction = dec->alternate_vertical_scan ? 2 : 0;
662    
663                    if (cbp & (1 << (5 - i)))       // coded
664                    {
665                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
666    
667                            start_timer();
668                            get_inter_block(bs, &block[i * 64], direction);
669                            stop_coding_timer();
670    
671                            start_timer();
672                            if (dec->quant_type == 0) {
673                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
674                            } else {
675                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
676                  }                  }
677                            stop_iquant_timer();
678    
679                            start_timer();
680                            idct(&data[i * 64]);
681                            stop_idct_timer();
682          }          }
683  }  }
684    
685    /* interlace + GMC is this possible ??? */
686    /*      if (dec->interlacing && pMB->field_dct) {
687                    next_block = stride;
688                    stride *= 2;
689            }
690    */
691            start_timer();
692            if (cbp & 32)
693                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
694            if (cbp & 16)
695                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
696            if (cbp & 8)
697                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
698            if (cbp & 4)
699                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
700            if (cbp & 2)
701                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
702            if (cbp & 1)
703                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
704            stop_transfer_timer();
705    }
706    
707    
708  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
709    decoder_iframe(DECODER * dec,
710                               Bitstream * bs,
711                               int reduced_resolution,
712                               int quant,
713                               int intra_dc_threshold)
714  {  {
715            uint32_t bound;
716          uint32_t x, y;          uint32_t x, y;
717            uint32_t mb_width = dec->mb_width;
718            uint32_t mb_height = dec->mb_height;
719    
720          for (y = 0; y < dec->mb_height; y++)          if (reduced_resolution)
         {  
                 for (x = 0; x < dec->mb_width; x++)  
721                  {                  {
722                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                  mb_width = (dec->width + 31) / 32;
723                    mb_height = (dec->height + 31) / 32;
724            }
725    
726            bound = 0;
727    
728            for (y = 0; y < mb_height; y++) {
729                    for (x = 0; x < mb_width; x++) {
730                            MACROBLOCK *mb;
731                          uint32_t mcbpc;                          uint32_t mcbpc;
732                          uint32_t cbpc;                          uint32_t cbpc;
733                          uint32_t acpred_flag;                          uint32_t acpred_flag;
734                          uint32_t cbpy;                          uint32_t cbpy;
735                          uint32_t cbp;                          uint32_t cbp;
736    
737                            while (BitstreamShowBits(bs, 9) == 1)
738                                    BitstreamSkip(bs, 9);
739    
740                            if (check_resync_marker(bs, 0))
741                            {
742                                    bound = read_video_packet_header(bs, dec, 0,
743                                                            &quant, NULL, NULL, &intra_dc_threshold);
744                                    x = bound % mb_width;
745                                    y = bound / mb_width;
746                            }
747                            mb = &dec->mbs[y * dec->mb_width + x];
748    
749                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
750    
751                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
752                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
753                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
754    
755                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
756    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
757                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
758                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
759    
760                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
761                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
762                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
763                                          quant = 31;                                          quant = 31;
764                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
765                                          quant = 1;                                          quant = 1;
766                                  }                                  }
767                          }                          }
768                          mb->quant = quant;                          mb->quant = quant;
769                            mb->mvs[0].x = mb->mvs[0].y =
770                            mb->mvs[1].x = mb->mvs[1].y =
771                            mb->mvs[2].x = mb->mvs[2].y =
772                            mb->mvs[3].x = mb->mvs[3].y =0;
773    
774                            if (dec->interlacing) {
775                                    mb->field_dct = BitstreamGetBit(bs);
776                                    DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
777                            }
778    
779                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
780                                                            intra_dc_threshold, bound, reduced_resolution);
781    
                         decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);  
782                  }                  }
783                    if(dec->out_frm)
784                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
785          }          }
786    
787  }  }
788    
789    
790  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
791    get_motion_vector(DECODER * dec,
792                                      Bitstream * bs,
793                                      int x,
794                                      int y,
795                                      int k,
796                                      VECTOR * ret_mv,
797                                      int fcode,
798                                      const int bound)
799  {  {
800    
801          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
802          int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
803          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
804          int range = (64 * scale_fac);          int range = (64 * scale_fac);
805    
806          VECTOR pmv[4];          VECTOR pmv;
807          uint32_t psad[4];          VECTOR mv;
808    
809          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);  
810    
811          pmv_x = pmv[0].x;          mv.x = get_mv(bs, fcode);
812          pmv_y = pmv[0].y;          mv.y = get_mv(bs, fcode);
813    
814          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);  
815    
816          mv_x += pmv_x;          mv.x += pmv.x;
817          mv_y += pmv_y;          mv.y += pmv.y;
818    
819          if (mv_x < low)          if (mv.x < low) {
820          {                  mv.x += range;
821                  mv_x += range;          } else if (mv.x > high) {
822                    mv.x -= range;
823          }          }
824          else if (mv_x > high)  
825          {          if (mv.y < low) {
826                  mv_x -= range;                  mv.y += range;
827            } else if (mv.y > high) {
828                    mv.y -= range;
829          }          }
830    
831          if (mv_y < low)          ret_mv->x = mv.x;
832          {          ret_mv->y = mv.y;
                 mv_y += range;  
833          }          }
834          else if (mv_y > high)  
835    
836    
837    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
838          {          {
839                  mv_y -= range;          int length = 1 << (fcode+4);
         }  
840    
841          mv->x = mv_x;          if (quarterpel) value *= 2;
         mv->y = mv_y;  
842    
843            if (value < -length)
844                    return -length;
845            else if (value >= length)
846                    return length-1;
847            else return value;
848  }  }
849    
850    
851  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  /* for P_VOP set gmc_warp to NULL */
852    void
853    decoder_pframe(DECODER * dec,
854                               Bitstream * bs,
855                               int rounding,
856                               int reduced_resolution,
857                               int quant,
858                               int fcode,
859                               int intra_dc_threshold,
860                               const WARPPOINTS *const gmc_warp)
861  {  {
862    
863          uint32_t x, y;          uint32_t x, y;
864            uint32_t bound;
865            int cp_mb, st_mb;
866            uint32_t mb_width = dec->mb_width;
867            uint32_t mb_height = dec->mb_height;
868    
869          image_swap(&dec->cur, &dec->refn);          static int framecount=0;
870            if (reduced_resolution)
871            {
872                    mb_width = (dec->width + 31) / 32;
873                    mb_height = (dec->height + 31) / 32;
874            }
875    
876          start_timer();          start_timer();
877          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
878                                       dec->width, dec->height);
879          stop_edges_timer();          stop_edges_timer();
880    
881          for (y = 0; y < dec->mb_height; y++)          if (gmc_warp)
882          {          {
883                  for (x = 0; x < dec->mb_width; x++)  
884                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
885                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
886                  {                  {
887                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
888                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
889                                    dec->sprite_warping_points);
890                    }
891    
892                    generate_GMCparameters( dec->sprite_warping_points,
893                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
894                                    dec->width, dec->height, &dec->gmc_data);
895    
896                    generate_GMCimage(&dec->gmc_data, &dec->refn[0],
897                                            mb_width, mb_height,
898                                            dec->edged_width, dec->edged_width/2,
899                                            fcode, dec->quarterpel, 0,
900                                            rounding, dec->mbs, &dec->gmc);
901    
902            }
903    
904                          if (!BitstreamGetBit(bs))                       // not_coded          bound = 0;
905    
906            for (y = 0; y < mb_height; y++) {
907                    cp_mb = st_mb = 0;
908                    for (x = 0; x < mb_width; x++) {
909                            MACROBLOCK *mb;
910    
911                            // skip stuffing
912                            while (BitstreamShowBits(bs, 10) == 1)
913                                    BitstreamSkip(bs, 10);
914    
915                            if (check_resync_marker(bs, fcode - 1))
916                            {
917                                    bound = read_video_packet_header(bs, dec, fcode - 1,
918                                            &quant, &fcode, NULL, &intra_dc_threshold);
919                                    x = bound % mb_width;
920                                    y = bound / mb_width;
921                            }
922                            mb = &dec->mbs[y * dec->mb_width + x];
923    
924                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
925    
926                            //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
927                            if (!(BitstreamGetBit(bs)))     // block _is_ coded
928                          {                          {
929                                  uint32_t mcbpc;                                  uint32_t mcbpc;
930                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 443  Line 932 
932                                  uint32_t cbpy;                                  uint32_t cbpy;
933                                  uint32_t cbp;                                  uint32_t cbp;
934                                  uint32_t intra;                                  uint32_t intra;
935                                    int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC
936    
937                                    cp_mb++;
938                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
939                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
940                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
941    
942                                    DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
943                                    DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
944                                  acpred_flag = 0;                                  acpred_flag = 0;
945    
946                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
947    
948                                  if (intra)                                  if (intra) {
                                 {  
949                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
950                                  }                                  }
951    
952                                  if (mb->mode == MODE_STUFFING)                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
953                                  {                                  {
954                                          DEBUG("-- STUFFING ?");                                          mcsel = BitstreamGetBit(bs);
                                         continue;  
955                                  }                                  }
956    
957                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
958                                    DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
959    
960                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
961    
962                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
963                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
964                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
965                                          if (quant > 31)                                          quant += dquant;
966                                          {                                          if (quant > 31) {
967                                                  quant = 31;                                                  quant = 31;
968                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
969                                                  quant = 1;                                                  quant = 1;
970                                          }                                          }
971                                            DPRINTF(DPRINTF_MB, "quant %i", quant);
972                                  }                                  }
973                                  mb->quant = quant;                                  mb->quant = quant;
974    
975                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (dec->interlacing) {
976                                  {                                          if (cbp || intra) {
977                                                    mb->field_dct = BitstreamGetBit(bs);
978                                                    DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
979                                            }
980    
981                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
982                                          mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                  mb->field_pred = BitstreamGetBit(bs);
983                                          mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
984    
985                                                    if (mb->field_pred) {
986                                                            mb->field_for_top = BitstreamGetBit(bs);
987                                                            DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
988                                                            mb->field_for_bot = BitstreamGetBit(bs);
989                                                            DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
990                                  }                                  }
                                 else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
                                 {  
                                         get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
                                         get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);  
                                         get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);  
                                         get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
991                                  }                                  }
992                                  else  // MODE_INTRA, MODE_INTRA_Q                                  }
993                                  {  
994                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  if (mcsel) {
995                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                          decoder_mbgmc(dec, mb, x, y, 0, cbp, bs, quant,
996                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                                                                  rounding, reduced_resolution);
997                                            continue;
998    
999                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
1000    
1001                                            if (dec->interlacing && mb->field_pred) {
1002                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1003                                                                                      fcode, bound);
1004                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
1005                                                                                      fcode, bound);
1006                                            } else {
1007                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1008                                                                                      fcode, bound);
1009                                                    mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
1010                                            }
1011                                    } else if (mb->mode == MODE_INTER4V ) {
1012    
1013                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
1014                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
1015                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
1016                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
1017                                    } else                  // MODE_INTRA, MODE_INTRA_Q
1018                                    {
1019                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
1020                                                    0;
1021                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
1022                                                    0;
1023                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1024                                                                            intra_dc_threshold, bound, reduced_resolution);
1025                                          continue;                                          continue;
1026                                  }                                  }
1027    
1028                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                                  decoder_mbinter(dec, mb, x, y, 0, cbp, bs, quant,
1029                                                                    rounding, reduced_resolution);
1030    
1031                          }                          }
1032                          else    // not coded                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1033                          {                          {
1034                                    mb->mode = MODE_NOT_CODED_GMC;
1035    
1036                                    start_timer();
1037    
1038                                    decoder_mbgmc(dec, mb, x, y, 0, 0x00, bs, quant,
1039                                                                    rounding, reduced_resolution);
1040    
1041                                    stop_transfer_timer();
1042    
1043                                    if(dec->out_frm && cp_mb > 0) {
1044                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1045                                      cp_mb = 0;
1046                                    }
1047                                    st_mb = x+1;
1048                            }
1049                            else    /* not coded P_VOP macroblock */
1050                            {
1051                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
1052    
1053                                  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;
1054                                  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;
   
1055                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
1056    
1057                                  start_timer();                                  start_timer();
1058    
1059                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                                  if (reduced_resolution)
1060                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x),                                  {
1061                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1062                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1063                                                                  dec->edged_width);                                                                  dec->edged_width);
1064    
1065                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1066                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x+8),                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1067                                                                  dec->edged_width);                                                                          dec->edged_width/2);
   
                                 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),  
                                                                 dec->refn.y + (16*y+8)*dec->edged_width + (16*x),  
                                                                 dec->edged_width);  
1068    
1069                                  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),
1070                                                                  dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1071                                                                             dec->edged_width/2);
1072                                    }
1073                                    else
1074                                    {
1075                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1076                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1077                                                                  dec->edged_width);                                                                  dec->edged_width);
1078    
1079                                  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),
1080                                                                  dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                                                                          dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1081                                                                  dec->edged_width/2);                                                                  dec->edged_width/2);
1082    
1083                                  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),
1084                                                                  dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                                                                           dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1085                                                                  dec->edged_width/2);                                                                  dec->edged_width/2);
1086                                    }
1087    
1088                                    stop_transfer_timer();
1089    
1090                                    if(dec->out_frm && cp_mb > 0) {
1091                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1092                                      cp_mb = 0;
1093                                    }
1094                                    st_mb = x+1;
1095                            }
1096                    }
1097                    if(dec->out_frm && cp_mb > 0)
1098                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1099            }
1100    }
1101    
1102    
1103    // add by MinChen <chenm001@163.com>
1104    // decode B-frame motion vector
1105    void
1106    get_b_motion_vector(DECODER * dec,
1107                                            Bitstream * bs,
1108                                            int x,
1109                                            int y,
1110                                            VECTOR * mv,
1111                                            int fcode,
1112                                            const VECTOR pmv)
1113    {
1114            int scale_fac = 1 << (fcode - 1);
1115            int high = (32 * scale_fac) - 1;
1116            int low = ((-32) * scale_fac);
1117            int range = (64 * scale_fac);
1118    
1119            int mv_x, mv_y;
1120            int pmv_x, pmv_y;
1121    
1122            pmv_x = pmv.x;
1123            pmv_y = pmv.y;
1124    
1125            mv_x = get_mv(bs, fcode);
1126            mv_y = get_mv(bs, fcode);
1127    
1128            mv_x += pmv_x;
1129            mv_y += pmv_y;
1130    
1131            if (mv_x < low) {
1132                    mv_x += range;
1133            } else if (mv_x > high) {
1134                    mv_x -= range;
1135            }
1136    
1137            if (mv_y < low) {
1138                    mv_y += range;
1139            } else if (mv_y > high) {
1140                    mv_y -= range;
1141            }
1142    
1143            mv->x = mv_x;
1144            mv->y = mv_y;
1145    }
1146    
1147    
1148    // add by MinChen <chenm001@163.com>
1149    // decode an B-frame forward & backward inter macroblock
1150    void
1151    decoder_bf_mbinter(DECODER * dec,
1152                                       const MACROBLOCK * pMB,
1153                                       const uint32_t x_pos,
1154                                       const uint32_t y_pos,
1155                                       const uint32_t cbp,
1156                                       Bitstream * bs,
1157                                       const uint32_t quant,
1158                                       const uint8_t ref)
1159    {
1160    
1161            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1162            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1163    
1164            uint32_t stride = dec->edged_width;
1165            uint32_t stride2 = stride / 2;
1166            uint32_t next_block = stride * 8;
1167            uint32_t i;
1168            uint32_t iQuant = pMB->quant;
1169            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1170            int uv_dx, uv_dy;
1171    
1172            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1173            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1174            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1175    
1176    
1177            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1178                    uv_dx = pMB->mvs[0].x;
1179                    uv_dy = pMB->mvs[0].y;
1180    
1181                    if (dec->quarterpel)
1182                    {
1183                            uv_dx /= 2;
1184                            uv_dy /= 2;
1185                    }
1186    
1187                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1188                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1189            } else {
1190                    int sum;
1191    
1192                    if(dec->quarterpel)
1193                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1194                    else
1195                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1196    
1197                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1198    
1199                    if(dec->quarterpel)
1200                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1201                    else
1202                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1203    
1204                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1205            }
1206    
1207            start_timer();
1208            if(dec->quarterpel) {
1209                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1210                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1211                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1212            }
1213            else {
1214                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1215                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1216                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1217                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1218                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1219                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1220                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1221                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1222            }
1223    
1224            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1225                                                      uv_dx, uv_dy, stride2, 0);
1226            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1227                                                      uv_dx, uv_dy, stride2, 0);
1228            stop_comp_timer();
1229    
1230            for (i = 0; i < 6; i++) {
1231                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1232    
1233                    if (cbp & (1 << (5 - i)))       // coded
1234                    {
1235                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1236    
1237                            start_timer();
1238                            get_inter_block(bs, &block[i * 64], direction);
1239                            stop_coding_timer();
1240    
1241                            start_timer();
1242                            if (dec->quant_type == 0) {
1243                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1244                            } else {
1245                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1246                            }
1247                            stop_iquant_timer();
1248    
1249                            start_timer();
1250                            idct(&data[i * 64]);
1251                            stop_idct_timer();
1252                    }
1253            }
1254    
1255            if (dec->interlacing && pMB->field_dct) {
1256                    next_block = stride;
1257                    stride *= 2;
1258            }
1259    
1260            start_timer();
1261            if (cbp & 32)
1262                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1263            if (cbp & 16)
1264                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1265            if (cbp & 8)
1266                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1267            if (cbp & 4)
1268                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1269            if (cbp & 2)
1270                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1271            if (cbp & 1)
1272                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1273                                  stop_transfer_timer();                                  stop_transfer_timer();
1274                          }                          }
1275    
1276    // add by MinChen <chenm001@163.com>
1277    // decode an B-frame direct &  inter macroblock
1278    void
1279    decoder_bf_interpolate_mbinter(DECODER * dec,
1280                                                               IMAGE forward,
1281                                                               IMAGE backward,
1282                                                               const MACROBLOCK * pMB,
1283                                                               const uint32_t x_pos,
1284                                                               const uint32_t y_pos,
1285                                                               Bitstream * bs)
1286    {
1287    
1288            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1289            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1290    
1291            uint32_t stride = dec->edged_width;
1292            uint32_t stride2 = stride / 2;
1293            uint32_t next_block = stride * 8;
1294            uint32_t iQuant = pMB->quant;
1295            int uv_dx, uv_dy;
1296            int b_uv_dx, b_uv_dy;
1297            uint32_t i;
1298            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1299        const uint32_t cbp = pMB->cbp;
1300    
1301            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1302            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1303            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1304    
1305    
1306            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1307                    uv_dx = pMB->mvs[0].x;
1308                    uv_dy = pMB->mvs[0].y;
1309    
1310                    b_uv_dx = pMB->b_mvs[0].x;
1311                    b_uv_dy = pMB->b_mvs[0].y;
1312    
1313                    if (dec->quarterpel)
1314                    {
1315                            uv_dx /= 2;
1316                            uv_dy /= 2;
1317    
1318                            b_uv_dx /= 2;
1319                            b_uv_dy /= 2;
1320                  }                  }
1321    
1322                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1323                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1324    
1325                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1326                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1327            } else {
1328                    int sum;
1329    
1330                    if(dec->quarterpel)
1331                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1332                    else
1333                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1334    
1335                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1336    
1337                    if(dec->quarterpel)
1338                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1339                    else
1340                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1341    
1342                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1343    
1344    
1345                    if(dec->quarterpel)
1346                            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);
1347                    else
1348                            sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1349    
1350                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1351    
1352                    if(dec->quarterpel)
1353                            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);
1354                    else
1355                            sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1356    
1357                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1358          }          }
1359    
1360    
1361            start_timer();
1362            if(dec->quarterpel) {
1363                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1364                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1365                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1366                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1367                    else {
1368                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1369                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1370                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1371                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1372                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1373                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1374                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1375                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1376                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1377                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1378                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1379                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1380                    }
1381            }
1382            else {
1383                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1384                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1385                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1386                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1387                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1388                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1389                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1390                                                              16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1391                                                              0);
1392            }
1393    
1394            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1395                                                      uv_dy, stride2, 0);
1396            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1397                                                      uv_dy, stride2, 0);
1398    
1399    
1400            if(dec->quarterpel) {
1401                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1402                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1403                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1404                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1405                    else {
1406                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1407                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1408                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1409                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1410                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1411                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1412                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1413                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1414                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1415                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1416                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1417                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1418                    }
1419            }
1420            else {
1421                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1422                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1423                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1424                                                              16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1425                                                              0);
1426                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1427                                                              16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1428                                                              stride, 0);
1429                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1430                                                              16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1431                                                              stride, 0);
1432            }
1433    
1434            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1435                                                      b_uv_dx, b_uv_dy, stride2, 0);
1436            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1437                                                      b_uv_dx, b_uv_dy, stride2, 0);
1438    
1439            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1440                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1441                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1442                                                    stride, 1, 8);
1443    
1444            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1445                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1446                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1447                                                    stride, 1, 8);
1448    
1449            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1450                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1451                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1452                                                    stride, 1, 8);
1453    
1454            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1455                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1456                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1457                                                    stride, 1, 8);
1458    
1459            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1460                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1461                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1462                                                    stride2, 1, 8);
1463    
1464            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1465                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1466                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1467                                                    stride2, 1, 8);
1468    
1469            stop_comp_timer();
1470    
1471            for (i = 0; i < 6; i++) {
1472                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1473    
1474                    if (cbp & (1 << (5 - i)))       // coded
1475                    {
1476                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1477    
1478                            start_timer();
1479                            get_inter_block(bs, &block[i * 64], direction);
1480                            stop_coding_timer();
1481    
1482                            start_timer();
1483                            if (dec->quant_type == 0) {
1484                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1485                            } else {
1486                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1487                            }
1488                            stop_iquant_timer();
1489    
1490                            start_timer();
1491                            idct(&data[i * 64]);
1492                            stop_idct_timer();
1493                    }
1494            }
1495    
1496            if (dec->interlacing && pMB->field_dct) {
1497                    next_block = stride;
1498                    stride *= 2;
1499            }
1500    
1501            start_timer();
1502            if (cbp & 32)
1503                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1504            if (cbp & 16)
1505                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1506            if (cbp & 8)
1507                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1508            if (cbp & 4)
1509                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1510            if (cbp & 2)
1511                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1512            if (cbp & 1)
1513                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1514            stop_transfer_timer();
1515    }
1516    
1517    
1518    // add by MinChen <chenm001@163.com>
1519    // for decode B-frame dbquant
1520    int32_t __inline
1521    get_dbquant(Bitstream * bs)
1522    {
1523            if (!BitstreamGetBit(bs))       // '0'
1524                    return (0);
1525            else if (!BitstreamGetBit(bs))  // '10'
1526                    return (-2);
1527            else
1528                    return (2);                             // '11'
1529  }  }
1530    
1531  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  // add by MinChen <chenm001@163.com>
1532    // for decode B-frame mb_type
1533    // bit   ret_value
1534    // 1        0
1535    // 01       1
1536    // 001      2
1537    // 0001     3
1538    int32_t __inline
1539    get_mbtype(Bitstream * bs)
1540  {  {
1541            int32_t mb_type;
1542    
1543            for (mb_type = 0; mb_type <= 3; mb_type++) {
1544                    if (BitstreamGetBit(bs))
1545                            break;
1546            }
1547    
1548            if (mb_type <= 3)
1549                    return (mb_type);
1550            else
1551                    return (-1);
1552    }
1553    
1554    void
1555    decoder_bframe(DECODER * dec,
1556                               Bitstream * bs,
1557                               int quant,
1558                               int fcode_forward,
1559                               int fcode_backward)
1560    {
1561            uint32_t x, y;
1562            VECTOR mv;
1563            const VECTOR zeromv = {0,0};
1564    #ifdef BFRAMES_DEC_DEBUG
1565            FILE *fp;
1566            static char first=0;
1567    #define BFRAME_DEBUG    if (!first && fp){ \
1568                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1569            }
1570    #endif
1571    
1572            start_timer();
1573            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1574                                       dec->width, dec->height);
1575            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1576                                       dec->width, dec->height);
1577            stop_edges_timer();
1578    
1579    #ifdef BFRAMES_DEC_DEBUG
1580            if (!first){
1581                    fp=fopen("C:\\XVIDDBG.TXT","w");
1582            }
1583    #endif
1584    
1585            for (y = 0; y < dec->mb_height; y++) {
1586                    // Initialize Pred Motion Vector
1587                    dec->p_fmv = dec->p_bmv = zeromv;
1588                    for (x = 0; x < dec->mb_width; x++) {
1589                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1590                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1591    
1592                            mv =
1593                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1594                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1595    
1596                            // skip if the co-located P_VOP macroblock is not coded
1597                            // if not codec in co-located S_VOP macroblock is _not_ automatically skipped
1598    
1599                            if (last_mb->mode == MODE_NOT_CODED) {
1600                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1601                                    mb->cbp = 0;
1602    #ifdef BFRAMES_DEC_DEBUG
1603                                    mb->mb_type = MODE_NOT_CODED;
1604            BFRAME_DEBUG
1605    #endif
1606                                    mb->mb_type = MODE_FORWARD;
1607                                    mb->quant = last_mb->quant;
1608                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1609                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1610    
1611                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1612                                    continue;
1613                            }
1614    
1615                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1616                                    const uint8_t modb2 = BitstreamGetBit(bs);
1617    
1618                                    mb->mb_type = get_mbtype(bs);
1619    
1620                                    if (!modb2) {   // modb=='00'
1621                                            mb->cbp = BitstreamGetBits(bs, 6);
1622                                    } else {
1623                                            mb->cbp = 0;
1624                                    }
1625                                    if (mb->mb_type && mb->cbp) {
1626                                            quant += get_dbquant(bs);
1627    
1628                                            if (quant > 31) {
1629                                                    quant = 31;
1630                                            } else if (quant < 1) {
1631                                                    quant = 1;
1632                                            }
1633                                    }
1634                            } else {
1635                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1636                                    mb->cbp = 0;
1637                            }
1638    
1639                            mb->quant = quant;
1640                            mb->mode = MODE_INTER4V;
1641                            //DEBUG1("Switch bm_type=",mb->mb_type);
1642    
1643    #ifdef BFRAMES_DEC_DEBUG
1644            BFRAME_DEBUG
1645    #endif
1646    
1647                            switch (mb->mb_type) {
1648                            case MODE_DIRECT:
1649                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1650    
1651                            case MODE_DIRECT_NONE_MV:
1652                                    {
1653                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1654                                            int i;
1655    
1656                                            for (i = 0; i < 4; i++) {
1657                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1658                                                                          / TRD + mv.x);
1659                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1660                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1661                                                                                      / TRD
1662                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1663                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1664                                                                          / TRD + mv.y);
1665                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1666                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1667                                                                                      / TRD
1668                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1669                                            }
1670                                            //DEBUG("B-frame Direct!\n");
1671                                    }
1672                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1673                                                                                               mb, x, y, bs);
1674                                    break;
1675    
1676                            case MODE_INTERPOLATE:
1677                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1678                                                                            dec->p_fmv);
1679                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1680    
1681                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1682                                                                            fcode_backward, dec->p_bmv);
1683                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1684                                            mb->b_mvs[3] = mb->b_mvs[0];
1685    
1686                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1687                                                                                               mb, x, y, bs);
1688                                    //DEBUG("B-frame Bidir!\n");
1689                                    break;
1690    
1691                            case MODE_BACKWARD:
1692                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1693                                                                            dec->p_bmv);
1694                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1695    
1696                                    mb->mode = MODE_INTER;
1697                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1698                                    //DEBUG("B-frame Backward!\n");
1699                                    break;
1700    
1701                            case MODE_FORWARD:
1702                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1703                                                                            dec->p_fmv);
1704                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1705    
1706                                    mb->mode = MODE_INTER;
1707                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1708                                    //DEBUG("B-frame Forward!\n");
1709                                    break;
1710    
1711                            default:
1712                                    DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1713                            }
1714    
1715                    }                                               // end of FOR
1716            }
1717    #ifdef BFRAMES_DEC_DEBUG
1718            if (!first){
1719                    first=1;
1720                    if (fp)
1721                            fclose(fp);
1722            }
1723    #endif
1724    }
1725    
1726    // swap two MACROBLOCK array
1727    void
1728    mb_swap(MACROBLOCK ** mb1,
1729                    MACROBLOCK ** mb2)
1730    {
1731            MACROBLOCK *temp = *mb1;
1732    
1733            *mb1 = *mb2;
1734            *mb2 = temp;
1735    }
1736    
1737    
1738    /* perform post processing if necessary, and output the image */
1739    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1740                                            const XVID_DEC_FRAME * frame, int pp_disable)
1741    {
1742    
1743            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1744            {
1745                    /* note: image is stored to tmp */
1746                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1747                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1748                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1749                                                    8, frame->general);
1750                    img = &dec->tmp;
1751            }
1752    
1753            image_output(img, dec->width, dec->height,
1754                                     dec->edged_width, frame->image, frame->stride,
1755                                     frame->colorspace, dec->interlacing);
1756    }
1757    
1758    
1759    int
1760    decoder_decode(DECODER * dec,
1761                               XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1762    {
1763    
1764          Bitstream bs;          Bitstream bs;
1765          uint32_t rounding;          uint32_t rounding;
1766            uint32_t reduced_resolution;
1767          uint32_t quant;          uint32_t quant;
1768          uint32_t fcode;          uint32_t fcode_forward;
1769            uint32_t fcode_backward;
1770          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1771            WARPPOINTS gmc_warp;
1772            uint32_t vop_type;
1773            int success = 0;
1774            int output = 0;
1775            int seen_something = 0;
1776    
1777          start_global_timer();          start_global_timer();
1778    
1779            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1780            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1781    
1782            if ((frame->general & XVID_DEC_DISCONTINUITY))
1783                    dec->frames = 0;
1784    
1785            if (frame->length < 0)  /* decoder flush */
1786            {
1787                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1788                        we have a reference frame, then outout the reference frame */
1789                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1790                    {
1791                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1792                            output = 1;
1793                    }
1794    
1795                    frame->length = 0;
1796                    if (stats)
1797                    {
1798                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1799                            stats->data.vop.time_base = (int)dec->time_base;
1800                            stats->data.vop.time_increment = 0;     //XXX: todo
1801                    }
1802    
1803                    emms();
1804    
1805                    stop_global_timer();
1806                    return XVID_ERR_OK;
1807            }
1808    
1809          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1810    
1811          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1812            if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1813          {          {
1814          case P_VOP :                  if (stats)
1815                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                          stats->notify = XVID_DEC_VOP;
1816                  break;                  frame->length = 1;
1817                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1818                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1819                    emms();
1820                    return XVID_ERR_OK;
1821            }
1822    
1823    repeat:
1824    
1825            vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1826                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1827    
1828            DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1829                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1830    
1831            if (vop_type == - 1)
1832            {
1833                    if (success) goto done;
1834                    emms();
1835                    return XVID_ERR_FAIL;
1836            }
1837    
1838            if (vop_type == -2 || vop_type == -3)
1839            {
1840                    if (vop_type == -3)
1841                            decoder_resize(dec);
1842    
1843                    if (stats)
1844                    {
1845                            stats->notify = XVID_DEC_VOL;
1846                            stats->data.vol.general = 0;
1847                            if (dec->interlacing)
1848                                    stats->data.vol.general |= XVID_INTERLACING;
1849                            stats->data.vol.width = dec->width;
1850                            stats->data.vol.height = dec->height;
1851                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1852                            stats->data.vol.par_width = dec->par_width;
1853                            stats->data.vol.par_height = dec->par_height;
1854                            frame->length = BitstreamPos(&bs) / 8;
1855                            emms();
1856                            return XVID_ERR_OK;
1857                    }
1858                    goto repeat;
1859            }
1860    
1861            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1862    
1863    
1864            /* packed_mode: special-N_VOP treament */
1865            if (dec->packed_mode && vop_type == N_VOP)
1866            {
1867                    if (dec->low_delay_default && dec->frames > 0)
1868                    {
1869                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1870                            output = 1;
1871                    }
1872                    /* ignore otherwise */
1873            }
1874            else if (vop_type != B_VOP)
1875            {
1876                    switch(vop_type)
1877                    {
1878          case I_VOP :          case I_VOP :
1879                  //DEBUG1("",intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
                 decoder_iframe(dec, &bs, quant, intra_dc_threshold);  
1880                  break;                  break;
1881                    case P_VOP :
1882          case B_VOP :    // ignore                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1883                                                    fcode_forward, intra_dc_threshold, NULL);
1884                  break;                  break;
1885                    case S_VOP :
1886          case N_VOP :    // vop not coded                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1887                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1888                  break;                  break;
1889                    case N_VOP :
1890                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1891                            break;
1892                    }
1893    
1894          default :                  if (reduced_resolution)
1895                  return XVID_ERR_FAIL;                  {
1896                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1897                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1898                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1899                    }
1900    
1901                    /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1902                    if (!(dec->low_delay_default && dec->packed_mode))
1903                    {
1904                            if (dec->low_delay)
1905                            {
1906                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1907                                    output = 1;
1908                            }
1909                            else if (dec->frames > 0)       /* is the reference frame valid? */
1910                            {
1911                                    /* output the reference frame */
1912                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1913                                    output = 1;
1914                            }
1915                    }
1916    
1917                    image_swap(&dec->refn[0], &dec->refn[1]);
1918                    image_swap(&dec->cur, &dec->refn[0]);
1919                    mb_swap(&dec->mbs, &dec->last_mbs);
1920                    dec->last_reduced_resolution = reduced_resolution;
1921    
1922                    dec->frames++;
1923                    seen_something = 1;
1924    
1925            }else{  /* B_VOP */
1926    
1927                    if (dec->low_delay)
1928                    {
1929                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1930                            dec->low_delay = 1;
1931                    }
1932    
1933                    if (dec->frames < 2)
1934                    {
1935                            /* attemping to decode a bvop without atleast 2 reference frames */
1936                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1937                                                    "broken b-frame, mising ref frames");
1938                    }else if (dec->time_pp <= dec->time_bp) {
1939                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1940                            decoded in vfw. */
1941                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1942                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1943                    }else{
1944                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1945                    }
1946    
1947                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1948                    output = 1;
1949                    dec->frames++;
1950            }
1951    
1952            BitstreamByteAlign(&bs);
1953    
1954            /* low_delay_default mode: repeat in packed_mode */
1955            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1956            {
1957                    success = 1;
1958                    goto repeat;
1959            }
1960    
1961    done :
1962    
1963            /* low_delay_default mode: if we've gotten here without outputting anything,
1964               then output the recently decoded frame, or print an error message  */
1965            if (dec->low_delay_default && output == 0)
1966            {
1967                    if (dec->packed_mode && seen_something)
1968                    {
1969                            /* output the recently decoded frame */
1970                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1971                            output = 1;
1972                    }
1973                    else
1974                    {
1975                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1976                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1977                                    "warning: nothing to output");
1978                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1979                                    "bframe decoder lag");
1980    
1981                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1982                    }
1983          }          }
1984    
1985          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1986    
1987          start_timer();          if (stats)
1988          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          {
1989                                  frame->image, frame->stride, frame->colorspace);                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1990          stop_conv_timer();                  stats->data.vop.time_base = (int)dec->time_base;
1991                    stats->data.vop.time_increment = 0;     //XXX: todo
1992            }
1993    
1994          emms();          emms();
1995    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.37.2.28

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