[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.7, Thu Mar 28 20:57:24 2002 UTC revision 1.37.2.1, Thu Sep 26 01:54:54 2002 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   *  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 <stdlib.h>  #include <stdlib.h>
63  #include <string.h>  // memset  #include <string.h>
64    
65    #ifdef BFRAMES_DEC_DEBUG
66            #define BFRAMES_DEC
67    #endif
68    
69  #include "xvid.h"  #include "xvid.h"
70  #include "portab.h"  #include "portab.h"
# Line 55  Line 79 
79  #include "dct/fdct.h"  #include "dct/fdct.h"
80  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
81  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
 #include "utils/mbfunctions.h"  
82    
83  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
84  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 66  Line 89 
89  #include "image/colorspace.h"  #include "image/colorspace.h"
90  #include "utils/mem_align.h"  #include "utils/mem_align.h"
91    
92  int decoder_create(XVID_DEC_PARAM * param)  int
93    decoder_create(XVID_DEC_PARAM * param)
94  {  {
95          DECODER * dec;          DECODER * dec;
96    
97          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
98          if (dec == NULL)          if (dec == NULL) {
         {  
99                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
100          }          }
101          param->handle = dec;          param->handle = dec;
# Line 85  Line 108 
108    
109          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
110          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
111            dec->low_delay = 0;
112    
113          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
114                  xvid_free(dec);                  xvid_free(dec);
115                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
116          }          }
117    
118          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
119          {                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
120                    xvid_free(dec);
121                    return XVID_ERR_MEMORY;
122            }
123            // add by chenm001 <chenm001@163.com>
124            // for support B-frame to reference last 2 frame
125            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
126                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
127                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
128                    xvid_free(dec);
129                    return XVID_ERR_MEMORY;
130            }
131            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
132                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
133                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
134                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
135                  xvid_free(dec);                  xvid_free(dec);
136                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
137          }          }
138    
139          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          dec->mbs =
140          if (dec->mbs == NULL)                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
141          {                                          CACHE_LINE);
142            if (dec->mbs == NULL) {
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                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
147                    xvid_free(dec);
148                    return XVID_ERR_MEMORY;
149            }
150    
151            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
152    
153            // add by chenm001 <chenm001@163.com>
154            // for skip MB flag
155            dec->last_mbs =
156                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
157                                            CACHE_LINE);
158            if (dec->last_mbs == NULL) {
159                    xvid_free(dec->mbs);
160                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
161                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
162                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
163                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
164                  xvid_free(dec);                  xvid_free(dec);
165                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
166          }          }
167    
168            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
169    
170          init_timer();          init_timer();
171          create_vlc_tables();  
172            // add by chenm001 <chenm001@163.com>
173            // for support B-frame to save reference frame's time
174            dec->frames = -1;
175            dec->time = dec->time_base = dec->last_time_base = 0;
176    
177          return XVID_ERR_OK;          return XVID_ERR_OK;
178  }  }
179    
180    
181  int decoder_destroy(DECODER * dec)  int
182    decoder_destroy(DECODER * dec)
183  {  {
184            xvid_free(dec->last_mbs);
185          xvid_free(dec->mbs);          xvid_free(dec->mbs);
186          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
187            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
188            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
189          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
190          xvid_free(dec);          xvid_free(dec);
191    
         destroy_vlc_tables();  
   
192          write_timer();          write_timer();
193          return XVID_ERR_OK;          return XVID_ERR_OK;
194  }  }
195    
196    
197    
198  static const int32_t dquant_table[4] =  static const int32_t dquant_table[4] = {
 {  
199          -1, -2, 1, 2          -1, -2, 1, 2
200  };  };
201    
202    
203    
204    
205  // decode an intra macroblock  // decode an intra macroblock
206    
207  void decoder_mbintra(DECODER * dec,  void
208    decoder_mbintra(DECODER * dec,
209                       MACROBLOCK * pMB,                       MACROBLOCK * pMB,
210                       const uint32_t x_pos,                       const uint32_t x_pos,
211                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 145  Line 213 
213                       const uint32_t cbp,                       const uint32_t cbp,
214                       Bitstream * bs,                       Bitstream * bs,
215                       const uint32_t quant,                       const uint32_t quant,
216                       const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
217                                    const unsigned int bound)
218  {  {
219    
220          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
221          DECLARE_ALIGNED_MATRIX(data,  6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(data,  6, 64, int16_t, CACHE_LINE);
222    
223          const uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
224            uint32_t stride2 = stride / 2;
225            uint32_t next_block = stride * 8;
226          uint32_t i;          uint32_t i;
227          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
228          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
229    
230          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
231          pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
232          pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
233    
234          memset(block, 0, 6*64*sizeof(int16_t));         // clear          memset(block, 0, 6*64*sizeof(int16_t));         // clear
235    
236          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
         {  
237                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
238                  int16_t predictors[8];                  int16_t predictors[8];
239                  int start_coeff;                  int start_coeff;
240    
241                  start_timer();                  start_timer();
242                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
243                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
244                  {                  if (!acpred_flag) {
245                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
246                  }                  }
247                  stop_prediction_timer();                  stop_prediction_timer();
248    
249                  if (quant < intra_dc_threshold)                  if (quant < intra_dc_threshold) {
                 {  
250                          int dc_size;                          int dc_size;
251                          int dc_dif;                          int dc_dif;
252    
253                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);                          dc_size = i < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);
254                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
255    
256                          if (dc_size > 8)                          if (dc_size > 8) {
                         {  
257                                  BitstreamSkip(bs, 1);           // marker                                  BitstreamSkip(bs, 1);           // marker
258                          }                          }
259    
260                          block[i*64 + 0] = dc_dif;                          block[i*64 + 0] = dc_dif;
261                          start_coeff = 1;                          start_coeff = 1;
262                  }  
263                  else                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
264                  {                  } else {
265                          start_coeff = 0;                          start_coeff = 0;
266                  }                  }
267    
268                  start_timer();                  start_timer();
269                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
270                  {                  {
271                          get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff);                          int direction = dec->alternate_vertical_scan ?
272                                    2 : pMB->acpred_directions[i];
273    
274                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
275                  }                  }
276                  stop_coding_timer();                  stop_coding_timer();
277    
# Line 209  Line 280 
280                  stop_prediction_timer();                  stop_prediction_timer();
281    
282                  start_timer();                  start_timer();
283                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
                 {  
284                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                          dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);
285                  }                  } else {
                 else  
                 {  
286                          dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);                          dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler);
287                  }                  }
288                  stop_iquant_timer();                  stop_iquant_timer();
# Line 224  Line 292 
292                  stop_idct_timer();                  stop_idct_timer();
293          }          }
294    
295          start_timer();          if (dec->interlacing && pMB->field_dct) {
296          if (dec->interlacing && pMB->field_dct)                  next_block = stride;
297          {                  stride *= 2;
                 MBFieldToFrame(data);  
298          }          }
         stop_interlacing_timer();  
299    
300          start_timer();          start_timer();
301          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);          transfer_16to8copy(pY_Cur,                  &data[0*64], stride);
302          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);          transfer_16to8copy(pY_Cur + 8,              &data[1*64], stride);
303          transfer_16to8copy(pY_Cur + 8 * stride,     &data[2*64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
304          transfer_16to8copy(pY_Cur + 8 + 8 * stride, &data[3*64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
305          transfer_16to8copy(pU_Cur,                  &data[4*64], stride / 2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
306          transfer_16to8copy(pV_Cur,                  &data[5*64], stride / 2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
307          stop_transfer_timer();          stop_transfer_timer();
308  }  }
309    
# Line 253  Line 319 
319    
320  // decode an inter macroblock  // decode an inter macroblock
321    
322  void decoder_mbinter(DECODER * dec,  void
323    decoder_mbinter(DECODER * dec,
324                       const MACROBLOCK * pMB,                       const MACROBLOCK * pMB,
325                       const uint32_t x_pos,                       const uint32_t x_pos,
326                       const uint32_t y_pos,                       const uint32_t y_pos,
# Line 267  Line 334 
334          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
335          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
336    
337          const uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
338          const uint32_t stride2 = dec->edged_width / 2;          uint32_t stride2 = stride / 2;
339            uint32_t next_block = stride * 8;
340          uint32_t i;          uint32_t i;
341          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
342          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
343          int uv_dx, uv_dy;          int uv_dx, uv_dy;
344    
345          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
346          pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
347          pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
348    
349          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
         {  
350                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
351                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
352    
353                    if (dec->quarterpel)
354                    {
355                            uv_dx = (uv_dx >> 1) | (uv_dx & 1);
356                            uv_dy = (uv_dy >> 1) | (uv_dy & 1);
357                    }
358    
359                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
360                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
361          }          } else {
         else  
         {  
362                  int sum;                  int sum;
363                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
364    
365                    if (dec->quarterpel)
366                    {
367                            sum /= 2;
368                    }
369    
370                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
371    
372                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
373    
374                    if (dec->quarterpel)
375                    {
376                            sum /= 2;
377                    }
378    
379                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
380          }          }
381    
382          start_timer();          start_timer();
383          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          if(dec->quarterpel) {
384          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                  DEBUG("QUARTERPEL");
385          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
386          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
387          interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
388          interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
389                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
390                                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
391                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
392                                                                      pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
393            }
394            else {
395                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
396                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
397                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
398                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
399                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
400                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
401                    interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
402                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
403            }
404    
405            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
406                                                      uv_dx, uv_dy, stride2, rounding);
407            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
408                                                      uv_dx, uv_dy, stride2, rounding);
409          stop_comp_timer();          stop_comp_timer();
410    
411          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++) {
412          {                  int direction = dec->alternate_vertical_scan ? 2 : 0;
413    
414                  if (cbp & (1 << (5-i)))                 // coded                  if (cbp & (1 << (5-i)))                 // coded
415                  {                  {
416                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear                          memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
417    
418                          start_timer();                          start_timer();
419                          get_inter_block(bs, &block[i*64]);                          get_inter_block(bs, &block[i * 64], direction);
420                          stop_coding_timer();                          stop_coding_timer();
421    
422                          start_timer();                          start_timer();
423                          if (dec->quant_type == 0)                          if (dec->quant_type == 0) {
                         {  
424                                  dequant_inter(&data[i*64], &block[i*64], iQuant);                                  dequant_inter(&data[i*64], &block[i*64], iQuant);
425                          }                          } else {
                         else  
                         {  
426                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);                                  dequant4_inter(&data[i*64], &block[i*64], iQuant);
427                          }                          }
428                          stop_iquant_timer();                          stop_iquant_timer();
# Line 332  Line 433 
433                  }                  }
434          }          }
435    
436          start_timer();          if (dec->interlacing && pMB->field_dct) {
437          if (pMB->field_dct)                  next_block = stride;
438          {                  stride *= 2;
                 MBFieldToFrame(data);  
439          }          }
         stop_interlacing_timer();  
440    
441          start_timer();          start_timer();
442          if (cbp & 32)          if (cbp & 32)
# Line 345  Line 444 
444          if (cbp & 16)          if (cbp & 16)
445                  transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);                  transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);
446          if (cbp & 8)          if (cbp & 8)
447                  transfer_16to8add(pY_Cur + 8 * stride,     &data[2*64], stride);                  transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
448          if (cbp & 4)          if (cbp & 4)
449                  transfer_16to8add(pY_Cur + 8 + 8 * stride, &data[3*64], stride);                  transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
450          if (cbp & 2)          if (cbp & 2)
451                  transfer_16to8add(pU_Cur,                  &data[4*64], stride / 2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
452          if (cbp & 1)          if (cbp & 1)
453                  transfer_16to8add(pV_Cur,                  &data[5*64], stride / 2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
454          stop_transfer_timer();          stop_transfer_timer();
455  }  }
456    
457    
458  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  void
459    decoder_iframe(DECODER * dec,
460                               Bitstream * bs,
461                               int quant,
462                               int intra_dc_threshold)
463  {  {
464            uint32_t bound;
465          uint32_t x, y;          uint32_t x, y;
466    
467          for (y = 0; y < dec->mb_height; y++)          bound = 0;
         {  
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
468    
469            for (y = 0; y < dec->mb_height; y++) {
470                    for (x = 0; x < dec->mb_width; x++) {
471                            MACROBLOCK *mb;
472                          uint32_t mcbpc;                          uint32_t mcbpc;
473                          uint32_t cbpc;                          uint32_t cbpc;
474                          uint32_t acpred_flag;                          uint32_t acpred_flag;
475                          uint32_t cbpy;                          uint32_t cbpy;
476                          uint32_t cbp;                          uint32_t cbp;
477    
478                            while (BitstreamShowBits(bs, 9) == 1)
479                                    BitstreamSkip(bs, 9);
480    
481                            if (check_resync_marker(bs, 0))
482                            {
483                                    bound = read_video_packet_header(bs, 0, &quant);
484                                    x = bound % dec->mb_width;
485                                    y = bound / dec->mb_width;
486                            }
487                            mb = &dec->mbs[y * dec->mb_width + x];
488    
489                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
490    
491                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
492                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
493                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
494    
495                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
496    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
497                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
498                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
499    
500                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
501                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
502                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
503                                          quant = 31;                                          quant = 31;
504                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
505                                          quant = 1;                                          quant = 1;
506                                  }                                  }
507                          }                          }
508                          mb->quant = quant;                          mb->quant = quant;
509                            mb->mvs[0].x = mb->mvs[0].y =
510                            mb->mvs[1].x = mb->mvs[1].y =
511                            mb->mvs[2].x = mb->mvs[2].y =
512                            mb->mvs[3].x = mb->mvs[3].y =0;
513    
514                          if (dec->interlacing)                          if (dec->interlacing) {
                         {  
515                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
516                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DEBUG1("deci: field_dct: ", mb->field_dct);
517                          }                          }
518    
519                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
520                                                            intra_dc_threshold, bound);
521                  }                  }
522                    if(dec->out_frm)
523                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);
524    
525          }          }
526    
527  }  }
528    
529    
530  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  void
531    get_motion_vector(DECODER * dec,
532                                      Bitstream * bs,
533                                      int x,
534                                      int y,
535                                      int k,
536                                      VECTOR * mv,
537                                      int fcode,
538                                      const int bound)
539  {  {
540    
541          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
# Line 423  Line 543 
543          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
544          int range = (64 * scale_fac);          int range = (64 * scale_fac);
545    
546          VECTOR pmv[4];          VECTOR pmv;
         uint32_t psad[4];  
   
547          int mv_x, mv_y;          int mv_x, mv_y;
         int pmv_x, pmv_y;  
   
548    
549          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
   
         pmv_x = pmv[0].x;  
         pmv_y = pmv[0].y;  
550    
551          mv_x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
552          mv_y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
553    
554          mv_x += pmv_x;          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);
         mv_y += pmv_y;  
555    
556          if (mv_x < low)          mv_x += pmv.x;
557          {          mv_y += pmv.y;
558    
559            if (mv_x < low) {
560                  mv_x += range;                  mv_x += range;
561          }          } else if (mv_x > high) {
         else if (mv_x > high)  
         {  
562                  mv_x -= range;                  mv_x -= range;
563          }          }
564    
565          if (mv_y < low)          if (mv_y < low) {
         {  
566                  mv_y += range;                  mv_y += range;
567          }          } else if (mv_y > high) {
         else if (mv_y > high)  
         {  
568                  mv_y -= range;                  mv_y -= range;
569          }          }
570    
# Line 465  Line 574 
574  }  }
575    
576    
577  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)  void
578    decoder_pframe(DECODER * dec,
579                               Bitstream * bs,
580                               int rounding,
581                               int quant,
582                               int fcode,
583                               int intra_dc_threshold)
584  {  {
585    
586          uint32_t x, y;          uint32_t x, y;
587            uint32_t bound;
588          image_swap(&dec->cur, &dec->refn);          int cp_mb, st_mb;
589    
590          start_timer();          start_timer();
591          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
592                                       dec->width, dec->height);
593          stop_edges_timer();          stop_edges_timer();
594    
595          for (y = 0; y < dec->mb_height; y++)          bound = 0;
596          {  
597                  for (x = 0; x < dec->mb_width; x++)          for (y = 0; y < dec->mb_height; y++) {
598                    cp_mb = st_mb = 0;
599                    for (x = 0; x < dec->mb_width; x++) {
600                            MACROBLOCK *mb;
601    
602                            // skip stuffing
603                            while (BitstreamShowBits(bs, 10) == 1)
604                                    BitstreamSkip(bs, 10);
605    
606                            if (check_resync_marker(bs, fcode - 1))
607                  {                  {
608                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                                  bound = read_video_packet_header(bs, fcode - 1, &quant);
609                                    x = bound % dec->mb_width;
610                                    y = bound / dec->mb_width;
611                            }
612                            mb = &dec->mbs[y * dec->mb_width + x];
613    
614                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
615    
616                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
617                            if (!(BitstreamGetBit(bs)))     // not_coded
618                          {                          {
619                                  uint32_t mcbpc;                                  uint32_t mcbpc;
620                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 491  Line 623 
623                                  uint32_t cbp;                                  uint32_t cbp;
624                                  uint32_t intra;                                  uint32_t intra;
625    
626                                    cp_mb++;
627                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
628                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
629                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
630    
631                                    DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
632                                    DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
633                                  acpred_flag = 0;                                  acpred_flag = 0;
634    
635                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
636    
637                                  if (intra)                                  if (intra) {
                                 {  
638                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
639                                  }                                  }
640    
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
   
641                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
642                                    DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
643    
644                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
645    
646                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
647                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
648                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
649                                          if (quant > 31)                                          quant += dquant;
650                                          {                                          if (quant > 31) {
651                                                  quant = 31;                                                  quant = 31;
652                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
653                                                  quant = 1;                                                  quant = 1;
654                                          }                                          }
655                                            DPRINTF(DPRINTF_MB, "quant %i", quant);
656                                  }                                  }
657                                  mb->quant = quant;                                  mb->quant = quant;
658    
659                                  if (dec->interlacing)                                  if (dec->interlacing) {
660                                  {                                          if (cbp || intra) {
661                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
662                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                          DEBUG1("decp: field_dct: ", mb->field_dct);
663                                            }
664    
665                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
666                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
667                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DEBUG1("decp: field_pred: ", mb->field_pred);
668    
669                                                  if (mb->field_pred)                                                  if (mb->field_pred) {
                                                 {  
670                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
671                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);
672                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
# Line 546  Line 675 
675                                          }                                          }
676                                  }                                  }
677    
678                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
679                                  {                                          if (dec->interlacing && mb->field_pred) {
680                                          if (dec->interlacing && mb->field_pred)                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
681                                                                                      fcode, bound);
682                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
683                                                                                      fcode, bound);
684                                            } else {
685                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
686                                                                                      fcode, bound);
687                                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
688                                                            mb->mvs[0].x;
689                                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
690                                                            mb->mvs[0].y;
691                                            }
692                                    } else if (mb->mode == MODE_INTER4V ) {
693    
694                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
695                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
696                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
697                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
698                                    } else                  // MODE_INTRA, MODE_INTRA_Q
699                                    {
700                                            mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
701                                                    0;
702                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
703                                                    0;
704                                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
705                                                                            intra_dc_threshold, bound);
706                                            continue;
707                                    }
708    
709                                    decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
710                                                                    rounding);
711                            } else                          // not coded
712                                          {                                          {
713                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                  DEBUG2("P-frame MB at (X,Y)=",x,y);
714                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode);                                  mb->mode = MODE_NOT_CODED;
715                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
716                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
717    
718                                    // copy macroblock directly from ref to cur
719    
720                                    start_timer();
721    
722                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
723                                                                     (16 * x),
724                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
725                                                                     (16 * x), dec->edged_width);
726    
727                                    transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +
728                                                                     (16 * x + 8),
729                                                                     dec->refn[0].y + (16 * y) * dec->edged_width +
730                                                                     (16 * x + 8), dec->edged_width);
731    
732                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
733                                                                     (16 * x),
734                                                                     dec->refn[0].y + (16 * y +
735                                                                                                       8) * dec->edged_width +
736                                                                     (16 * x), dec->edged_width);
737    
738                                    transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +
739                                                                     (16 * x + 8),
740                                                                     dec->refn[0].y + (16 * y +
741                                                                                                       8) * dec->edged_width +
742                                                                     (16 * x + 8), dec->edged_width);
743    
744                                    transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +
745                                                                     (8 * x),
746                                                                     dec->refn[0].u +
747                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
748                                                                     dec->edged_width / 2);
749    
750                                    transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +
751                                                                     (8 * x),
752                                                                     dec->refn[0].v +
753                                                                     (8 * y) * dec->edged_width / 2 + (8 * x),
754                                                                     dec->edged_width / 2);
755                                    stop_transfer_timer();
756                                    if(dec->out_frm && cp_mb > 0) {
757                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
758                                      cp_mb = 0;
759                                          }                                          }
760                                          else                                  st_mb = x+1;
761                            }
762                    }
763                    if(dec->out_frm && cp_mb > 0)
764                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
765            }
766    }
767    
768    
769    // add by MinChen <chenm001@163.com>
770    // decode B-frame motion vector
771    void
772    get_b_motion_vector(DECODER * dec,
773                                            Bitstream * bs,
774                                            int x,
775                                            int y,
776                                            VECTOR * mv,
777                                            int fcode,
778                                            const VECTOR pmv)
779                                          {                                          {
780                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);          int scale_fac = 1 << (fcode - 1);
781                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;          int high = (32 * scale_fac) - 1;
782                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;          int low = ((-32) * scale_fac);
783            int range = (64 * scale_fac);
784    
785            int mv_x, mv_y;
786            int pmv_x, pmv_y;
787    
788            pmv_x = pmv.x;
789            pmv_y = pmv.y;
790    
791            mv_x = get_mv(bs, fcode);
792            mv_y = get_mv(bs, fcode);
793    
794            mv_x += pmv_x;
795            mv_y += pmv_y;
796    
797            if (mv_x < low) {
798                    mv_x += range;
799            } else if (mv_x > high) {
800                    mv_x -= range;
801            }
802    
803            if (mv_y < low) {
804                    mv_y += range;
805            } else if (mv_y > high) {
806                    mv_y -= range;
807                                          }                                          }
808    
809            mv->x = mv_x;
810            mv->y = mv_y;
811                                  }                                  }
812                                  else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
813    
814    // add by MinChen <chenm001@163.com>
815    // decode an B-frame forward & backward inter macroblock
816    void
817    decoder_bf_mbinter(DECODER * dec,
818                                       const MACROBLOCK * pMB,
819                                       const uint32_t x_pos,
820                                       const uint32_t y_pos,
821                                       const uint32_t cbp,
822                                       Bitstream * bs,
823                                       const uint32_t quant,
824                                       const uint8_t ref)
825                                  {                                  {
826                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
827                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
828                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
829                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
830            uint32_t stride = dec->edged_width;
831            uint32_t stride2 = stride / 2;
832            uint32_t next_block = stride * 8;
833            uint32_t i;
834            uint32_t iQuant = pMB->quant;
835            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
836            int uv_dx, uv_dy;
837    
838            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
839            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
840            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
841    
842    
843            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
844                    uv_dx = pMB->mvs[0].x;
845                    uv_dy = pMB->mvs[0].y;
846    
847                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
848                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
849            } else {
850                    int sum;
851    
852                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
853                    uv_dx =
854                            (sum ==
855                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
856                                                                      (ABS(sum) / 16) * 2));
857    
858                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
859                    uv_dy =
860                            (sum ==
861                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
862                                                                      (ABS(sum) / 16) * 2));
863                                  }                                  }
864                                  else  // MODE_INTRA, MODE_INTRA_Q  
865            start_timer();
866            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
867                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
868            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
869                                                      16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
870            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,
871                                                      16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,
872                                                      0);
873            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,
874                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
875                                                      0);
876            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
877                                                      uv_dx, uv_dy, stride2, 0);
878            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
879                                                      uv_dx, uv_dy, stride2, 0);
880            stop_comp_timer();
881    
882            for (i = 0; i < 6; i++) {
883                    int direction = dec->alternate_vertical_scan ? 2 : 0;
884    
885                    if (cbp & (1 << (5 - i)))       // coded
886                                  {                                  {
887                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
888                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
889                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                          start_timer();
890                                          continue;                          get_inter_block(bs, &block[i * 64], direction);
891                            stop_coding_timer();
892    
893                            start_timer();
894                            if (dec->quant_type == 0) {
895                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
896                            } else {
897                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
898                                  }                                  }
899                            stop_iquant_timer();
900    
901                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                          start_timer();
902                            idct(&data[i * 64]);
903                            stop_idct_timer();
904                          }                          }
905                          else    // not coded          }
906    
907            if (dec->interlacing && pMB->field_dct) {
908                    next_block = stride;
909                    stride *= 2;
910            }
911    
912            start_timer();
913            if (cbp & 32)
914                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
915            if (cbp & 16)
916                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
917            if (cbp & 8)
918                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
919            if (cbp & 4)
920                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
921            if (cbp & 2)
922                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
923            if (cbp & 1)
924                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
925            stop_transfer_timer();
926    }
927    
928    
929    // add by MinChen <chenm001@163.com>
930    // decode an B-frame direct &  inter macroblock
931    void
932    decoder_bf_interpolate_mbinter(DECODER * dec,
933                                                               IMAGE forward,
934                                                               IMAGE backward,
935                                                               const MACROBLOCK * pMB,
936                                                               const uint32_t x_pos,
937                                                               const uint32_t y_pos,
938                                                               Bitstream * bs)
939                          {                          {
940    
941                                  mb->mode = MODE_NOT_CODED;          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
942                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;          DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
943                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
944            uint32_t stride = dec->edged_width;
945            uint32_t stride2 = stride / 2;
946            uint32_t next_block = stride * 8;
947            uint32_t iQuant = pMB->quant;
948            int uv_dx, uv_dy;
949            int b_uv_dx, b_uv_dy;
950            uint32_t i;
951            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
952        const uint32_t cbp = pMB->cbp;
953    
954            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
955            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
956            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
957    
958    
959            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
960                    uv_dx = pMB->mvs[0].x;
961                    uv_dy = pMB->mvs[0].y;
962    
963                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
964                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
965    
966                    b_uv_dx = pMB->b_mvs[0].x;
967                    b_uv_dy = pMB->b_mvs[0].y;
968    
969                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
970                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
971            } else {
972                    int sum;
973    
974                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
975                    uv_dx =
976                            (sum ==
977                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
978                                                                      (ABS(sum) / 16) * 2));
979    
980                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
981                    uv_dy =
982                            (sum ==
983                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
984                                                                      (ABS(sum) / 16) * 2));
985    
986                    sum =
987                            pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +
988                            pMB->b_mvs[3].x;
989                    b_uv_dx =
990                            (sum ==
991                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
992                                                                      (ABS(sum) / 16) * 2));
993    
994                    sum =
995                            pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +
996                            pMB->b_mvs[3].y;
997                    b_uv_dy =
998                            (sum ==
999                             0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +
1000                                                                      (ABS(sum) / 16) * 2));
1001            }
1002    
                                 // copy macroblock directly from ref to cur  
1003    
1004                                  start_timer();                                  start_timer();
1005            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1006                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1007            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1008                                                      pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1009            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1010                                                      pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1011            interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1012                                                      16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1013                                                      0);
1014            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1015                                                      uv_dy, stride2, 0);
1016            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1017                                                      uv_dy, stride2, 0);
1018    
1019    
1020            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
1021                                                      pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1022            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1023                                                      16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1024                                                      0);
1025            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,
1026                                                      16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1027                                                      stride, 0);
1028            interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1029                                                      16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1030                                                      stride, 0);
1031            interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
1032                                                      b_uv_dx, b_uv_dy, stride2, 0);
1033            interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1034                                                      b_uv_dx, b_uv_dy, stride2, 0);
1035    
1036            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,
1037                                             stride);
1038            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,
1039                                             stride);
1040            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,
1041                                             stride);
1042            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,
1043                                             16 * y_pos + 8, stride);
1044            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,
1045                                             stride2);
1046            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,
1047                                             stride2);
1048            stop_comp_timer();
1049    
1050                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),          for (i = 0; i < 6; i++) {
1051                                                   dec->refn.y + (16*y)*dec->edged_width + (16*x),                  int direction = dec->alternate_vertical_scan ? 2 : 0;
                                                  dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),  
                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x+8),  
                                                  dec->edged_width);  
   
                                 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);  
   
                                 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                  dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                  dec->edged_width);  
1052    
1053                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                  if (cbp & (1 << (5 - i)))       // coded
1054                                                   dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                  {
1055                                                   dec->edged_width/2);                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1056    
1057                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                          start_timer();
1058                                                   dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                          get_inter_block(bs, &block[i * 64], direction);
1059                                                   dec->edged_width/2);                          stop_coding_timer();
1060    
1061                            start_timer();
1062                            if (dec->quant_type == 0) {
1063                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1064                            } else {
1065                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1066                            }
1067                            stop_iquant_timer();
1068    
1069                            start_timer();
1070                            idct(&data[i * 64]);
1071                            stop_idct_timer();
1072                    }
1073            }
1074    
1075            if (dec->interlacing && pMB->field_dct) {
1076                    next_block = stride;
1077                    stride *= 2;
1078            }
1079    
1080            start_timer();
1081            if (cbp & 32)
1082                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1083            if (cbp & 16)
1084                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1085            if (cbp & 8)
1086                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1087            if (cbp & 4)
1088                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1089            if (cbp & 2)
1090                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1091            if (cbp & 1)
1092                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1093                                  stop_transfer_timer();                                  stop_transfer_timer();
1094                          }                          }
1095    
1096    
1097    // add by MinChen <chenm001@163.com>
1098    // for decode B-frame dbquant
1099    int32_t __inline
1100    get_dbquant(Bitstream * bs)
1101    {
1102            if (!BitstreamGetBit(bs))       // '0'
1103                    return (0);
1104            else if (!BitstreamGetBit(bs))  // '10'
1105                    return (-2);
1106            else
1107                    return (2);                             // '11'
1108                  }                  }
1109    
1110    // add by MinChen <chenm001@163.com>
1111    // for decode B-frame mb_type
1112    // bit   ret_value
1113    // 1        0
1114    // 01       1
1115    // 001      2
1116    // 0001     3
1117    int32_t __inline
1118    get_mbtype(Bitstream * bs)
1119    {
1120            int32_t mb_type;
1121    
1122            for (mb_type = 0; mb_type <= 3; mb_type++) {
1123                    if (BitstreamGetBit(bs))
1124                            break;
1125          }          }
1126    
1127            if (mb_type <= 3)
1128                    return (mb_type);
1129            else
1130                    return (-1);
1131  }  }
1132    
1133  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  void
1134    decoder_bframe(DECODER * dec,
1135                               Bitstream * bs,
1136                               int quant,
1137                               int fcode_forward,
1138                               int fcode_backward)
1139    {
1140            uint32_t x, y;
1141            VECTOR mv;
1142            const VECTOR zeromv = {0,0};
1143    #ifdef BFRAMES_DEC_DEBUG
1144            FILE *fp;
1145            static char first=0;
1146    #define BFRAME_DEBUG    if (!first && fp){ \
1147                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1148            }
1149    #endif
1150    
1151            start_timer();
1152            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1153                                       dec->width, dec->height);
1154            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1155                                       dec->width, dec->height);
1156            stop_edges_timer();
1157    
1158    #ifdef BFRAMES_DEC_DEBUG
1159            if (!first){
1160                    fp=fopen("C:\\XVIDDBG.TXT","w");
1161            }
1162    #endif
1163    
1164            for (y = 0; y < dec->mb_height; y++) {
1165                    // Initialize Pred Motion Vector
1166                    dec->p_fmv = dec->p_bmv = zeromv;
1167                    for (x = 0; x < dec->mb_width; x++) {
1168                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1169                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1170    
1171                            mv =
1172                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1173                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1174    
1175                            // the last P_VOP is skip macroblock ?
1176                            if (last_mb->mode == MODE_NOT_CODED) {
1177                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1178                                    mb->cbp = 0;
1179    #ifdef BFRAMES_DEC_DEBUG
1180                                    mb->mb_type = MODE_NOT_CODED;
1181            BFRAME_DEBUG
1182    #endif
1183                                    mb->mb_type = MODE_FORWARD;
1184                                    mb->quant = last_mb->quant;
1185                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1186                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1187    
1188                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1189                                    continue;
1190                            }
1191    
1192                            if (!BitstreamGetBit(bs)) {     // modb=='0'
1193                                    const uint8_t modb2 = BitstreamGetBit(bs);
1194    
1195                                    mb->mb_type = get_mbtype(bs);
1196    
1197                                    if (!modb2) {   // modb=='00'
1198                                            mb->cbp = BitstreamGetBits(bs, 6);
1199                                    } else {
1200                                            mb->cbp = 0;
1201                                    }
1202                                    if (mb->mb_type && mb->cbp) {
1203                                            quant += get_dbquant(bs);
1204    
1205                                            if (quant > 31) {
1206                                                    quant = 31;
1207                                            } else if (quant < 1) {
1208                                                    quant = 1;
1209                                            }
1210                                    }
1211                            } else {
1212                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1213                                    mb->cbp = 0;
1214                            }
1215    
1216                            mb->quant = quant;
1217                            mb->mode = MODE_INTER4V;
1218                            //DEBUG1("Switch bm_type=",mb->mb_type);
1219    
1220    #ifdef BFRAMES_DEC_DEBUG
1221            BFRAME_DEBUG
1222    #endif
1223    
1224                            switch (mb->mb_type) {
1225                            case MODE_DIRECT:
1226                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1227    
1228                            case MODE_DIRECT_NONE_MV:
1229                                    {
1230                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1231                                            int i;
1232    
1233                                            for (i = 0; i < 4; i++) {
1234                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1235                                                                          / TRD + mv.x);
1236                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1237                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1238                                                                                      / TRD
1239                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1240                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1241                                                                          / TRD + mv.y);
1242                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1243                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1244                                                                                      / TRD
1245                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1246                                            }
1247                                            //DEBUG("B-frame Direct!\n");
1248                                    }
1249                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1250                                                                                               mb, x, y, bs);
1251                                    break;
1252    
1253                            case MODE_INTERPOLATE:
1254                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1255                                                                            dec->p_fmv);
1256                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1257    
1258                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1259                                                                            fcode_backward, dec->p_bmv);
1260                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1261                                            mb->b_mvs[3] = mb->b_mvs[0];
1262    
1263                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1264                                                                                               mb, x, y, bs);
1265                                    //DEBUG("B-frame Bidir!\n");
1266                                    break;
1267    
1268                            case MODE_BACKWARD:
1269                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1270                                                                            dec->p_bmv);
1271                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1272    
1273                                    mb->mode = MODE_INTER;
1274                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1275                                    //DEBUG("B-frame Backward!\n");
1276                                    break;
1277    
1278                            case MODE_FORWARD:
1279                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1280                                                                            dec->p_fmv);
1281                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1282    
1283                                    mb->mode = MODE_INTER;
1284                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1285                                    //DEBUG("B-frame Forward!\n");
1286                                    break;
1287    
1288                            default:
1289                                    DEBUG1("Not support B-frame mb_type =", mb->mb_type);
1290                            }
1291    
1292                    }                                               // end of FOR
1293            }
1294    #ifdef BFRAMES_DEC_DEBUG
1295            if (!first){
1296                    first=1;
1297                    if (fp)
1298                            fclose(fp);
1299            }
1300    #endif
1301    }
1302    
1303    // swap two MACROBLOCK array
1304    void
1305    mb_swap(MACROBLOCK ** mb1,
1306                    MACROBLOCK ** mb2)
1307    {
1308            MACROBLOCK *temp = *mb1;
1309    
1310            *mb1 = *mb2;
1311            *mb2 = temp;
1312    }
1313    
1314    int
1315    decoder_decode(DECODER * dec,
1316                               XVID_DEC_FRAME * frame)
1317  {  {
1318    
1319          Bitstream bs;          Bitstream bs;
1320          uint32_t rounding;          uint32_t rounding;
1321          uint32_t quant;          uint32_t quant;
1322          uint32_t fcode;          uint32_t fcode_forward;
1323            uint32_t fcode_backward;
1324          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1325            uint32_t vop_type;
1326    
1327          start_global_timer();          start_global_timer();
1328    
1329            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1330    
1331          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1332    
1333          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          // add by chenm001 <chenm001@163.com>
1334          {          // for support B-frame to reference last 2 frame
1335            dec->frames++;
1336            vop_type =
1337                    BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
1338                                                             &fcode_backward, &intra_dc_threshold);
1339    
1340            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1341    
1342            switch (vop_type) {
1343          case P_VOP :          case P_VOP :
1344                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1345                                               intra_dc_threshold);
1346    #ifdef BFRAMES_DEC
1347                    DEBUG1("P_VOP  Time=", dec->time);
1348    #endif
1349                  break;                  break;
1350    
1351          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1352                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1353    #ifdef BFRAMES_DEC
1354                    DEBUG1("I_VOP  Time=", dec->time);
1355    #endif
1356                  break;                  break;
1357    
1358          case B_VOP :    // ignore          case B_VOP:
1359    #ifdef BFRAMES_DEC
1360                    if (dec->time_pp > dec->time_bp) {
1361                            DEBUG1("B_VOP  Time=", dec->time);
1362                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1363                    } else {
1364                            DEBUG("broken B-frame!");
1365                    }
1366    #else
1367                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1368    #endif
1369                  break;                  break;
1370    
1371          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
1372                    // when low_delay==0, N_VOP's should interpolate between the past and future frames
1373                    image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1374                  break;                  break;
1375    
1376          default :          default :
1377                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1378          }          }
1379    
1380    #ifdef BFRAMES_DEC_DEBUG
1381            if (frame->length != BitstreamPos(&bs) / 8){
1382                    DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);
1383            }
1384    #endif
1385          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1386    
1387          start_timer();  
1388    #ifdef BFRAMES_DEC
1389            // test if no B_VOP
1390            if (dec->low_delay || dec->frames == 0) {
1391    #endif
1392          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1393                       frame->image, frame->stride, frame->colorspace);                       frame->image, frame->stride, frame->colorspace);
1394    
1395    #ifdef BFRAMES_DEC
1396            } else {
1397                    if (dec->frames >= 1) {
1398                            start_timer();
1399                            if ((vop_type == I_VOP || vop_type == P_VOP)) {
1400                                    image_output(&dec->refn[0], dec->width, dec->height,
1401                                                             dec->edged_width, frame->image, frame->stride,
1402                                                             frame->colorspace);
1403                            } else if (vop_type == B_VOP) {
1404                                    image_output(&dec->cur, dec->width, dec->height,
1405                                                             dec->edged_width, frame->image, frame->stride,
1406                                                             frame->colorspace);
1407                            }
1408          stop_conv_timer();          stop_conv_timer();
1409                    }
1410            }
1411    #endif
1412    
1413            if (vop_type == I_VOP || vop_type == P_VOP) {
1414                    image_swap(&dec->refn[0], &dec->refn[1]);
1415                    image_swap(&dec->cur, &dec->refn[0]);
1416    
1417                    // swap MACROBLOCK
1418                    // the Divx will not set the low_delay flage some times
1419                    // so follow code will wrong to not swap at that time
1420                    // this will broken bitstream! so I'm change it,
1421                    // But that is not the best way! can anyone tell me how
1422                    // to do another way?
1423                    // 18-07-2002   MinChen<chenm001@163.com>
1424                    //if (!dec->low_delay && vop_type == P_VOP)
1425                    if (vop_type == P_VOP)
1426                            mb_swap(&dec->mbs, &dec->last_mbs);
1427            }
1428    
1429          emms();          emms();
1430    
1431          stop_global_timer();          stop_global_timer();
1432    
1433          return XVID_ERR_OK;          return XVID_ERR_OK;
   
1434  }  }

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.37.2.1

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