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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.40

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