[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.22, Fri Jun 28 15:14:40 2002 UTC revision 1.49.2.24, Wed Dec 10 15:07:42 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  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
# Line 26  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
  *      22.06.2002      added primative N_VOP support  
  *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder  
  *  08.05.2002  add low_delay support for B_VOP decode  
  *              MinChen <chenm001@163.com>  
  *  05.05.2002  fix some B-frame decode problem  
  *  02.05.2002  add B-frame decode support(have some problem);  
  *              MinChen <chenm001@163.com>  
  *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>  
  *  29.03.2002  interlacing fix - compensated block wasn't being used when  
  *              reconstructing blocks, thus artifacts  
  *              interlacing speedup - used transfers to re-interlace  
  *              interlaced decoding should be as fast as progressive now  
  *  26.03.2002  interlacing support - moved transfers outside decode loop  
  *  26.12.2001  decoder_mbinter: dequant/idct moved within if(coded) block  
  *  22.12.2001  lock based interpolation  
  *  01.12.2001  inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
31    #ifdef BFRAMES_DEC_DEBUG
32            #define BFRAMES_DEC
33    #endif
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
49    #include "image/reduced.h"
50    #include "image/font.h"
51    
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "utils/emms.h"  #include "utils/emms.h"
56    #include "motion/motion.h"
57    #include "motion/gmc.h"
58    
59  #include "image/image.h"  #include "image/image.h"
60  #include "image/colorspace.h"  #include "image/colorspace.h"
61    #include "image/postprocessing.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  int  static int
65  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
66  {  {
67          DECODER *dec;          /* free existing */
68            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
69            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
70            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
71            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
72            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
73    
74          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
75    
76          dec->width = param->width;          if (dec->last_mbs)
77          dec->height = param->height;                  xvid_free(dec->last_mbs);
78            if (dec->mbs)
79                    xvid_free(dec->mbs);
80    
81            /* realloc */
82          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
83          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
84    
85          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
86          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
87    
88          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
89                  xvid_free(dec);                  xvid_free(dec);
# Line 111  Line 95 
95                  xvid_free(dec);                  xvid_free(dec);
96                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
97          }          }
98          // add by chenm001 <chenm001@163.com>  
99          // for support B-frame to reference last 2 frame          /* Support B-frame to reference last 2 frame */
100          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
101                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
102                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
103                  xvid_free(dec);                  xvid_free(dec);
104                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
105          }          }
106          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
107                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
108                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
109                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
110                    xvid_free(dec);
111                    return XVID_ERR_MEMORY;
112            }
113    
114            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
115                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
116                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
117                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
118                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
119                    xvid_free(dec);
120                    return XVID_ERR_MEMORY;
121            }
122    
123            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
124                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
125                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
126                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
127                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
128                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
129                  xvid_free(dec);                  xvid_free(dec);
130                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
131          }          }
# Line 134  Line 137 
137                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
139                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
140                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
141                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
142                  xvid_free(dec);                  xvid_free(dec);
143                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
144          }          }
   
145          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
146    
147          // add by chenm001 <chenm001@163.com>          /* For skip MB flag */
         // for skip MB flag  
148          dec->last_mbs =          dec->last_mbs =
149                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
150                                          CACHE_LINE);                                          CACHE_LINE);
# Line 151  Line 153 
153                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
155                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
156                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
157                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
158                  xvid_free(dec);                  xvid_free(dec);
159                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
160          }          }
161    
162          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
163    
164            return 0;
165    }
166    
167    
168    int
169    decoder_create(xvid_dec_create_t * create)
170    {
171            DECODER *dec;
172    
173            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
174                    return XVID_ERR_VERSION;
175    
176            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
177            if (dec == NULL) {
178                    return XVID_ERR_MEMORY;
179            }
180    
181            memset(dec, 0, sizeof(DECODER));
182    
183            dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
184            if (dec->mpeg_quant_matrices == NULL) {
185                    xvid_free(dec);
186                    return XVID_ERR_MEMORY;
187            }
188    
189            create->handle = dec;
190    
191            dec->width = create->width;
192            dec->height = create->height;
193    
194            image_null(&dec->cur);
195            image_null(&dec->refn[0]);
196            image_null(&dec->refn[1]);
197            image_null(&dec->tmp);
198            image_null(&dec->qtmp);
199    
200            /* image based GMC */
201            image_null(&dec->gmc);
202    
203    
204            dec->mbs = NULL;
205            dec->last_mbs = NULL;
206    
207          init_timer();          init_timer();
208            init_mpeg_matrix(dec->mpeg_quant_matrices);
209    
210          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
211          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
212          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
213            dec->low_delay = 0;
214            dec->packed_mode = 0;
215    
216            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
217    
218          return XVID_ERR_OK;          if (dec->fixed_dimensions)
219                    return decoder_resize(dec);
220            else
221                    return 0;
222  }  }
223    
224    
# Line 174  Line 227 
227  {  {
228          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
229          xvid_free(dec->mbs);          xvid_free(dec->mbs);
230    
231            /* image based GMC */
232            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
233    
234          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
235          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
236          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
237            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
238          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
239            xvid_free(dec->mpeg_quant_matrices);
240          xvid_free(dec);          xvid_free(dec);
241    
242          write_timer();          write_timer();
243          return XVID_ERR_OK;          return 0;
244  }  }
245    
   
   
246  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
247          -1, -2, 1, 2          -1, -2, 1, 2
248  };  };
249    
250    /* decode an intra macroblock */
251    static void
   
 // decode an intra macroblock  
   
 void  
252  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
253                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
254                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 205  Line 258 
258                                  Bitstream * bs,                                  Bitstream * bs,
259                                  const uint32_t quant,                                  const uint32_t quant,
260                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
261                                  const unsigned int bound_x,                                  const unsigned int bound,
262                                  const unsigned int bound_y)                                  const int reduced_resolution)
263  {  {
264    
265          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 219  Line 272 
272          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
273          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
274    
275            if (reduced_resolution) {
276                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
277                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
278                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
279            }else{
280          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
281          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
282          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
283            }
284    
285          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
286    
287          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
288                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 232  Line 291 
291    
292                  start_timer();                  start_timer();
293                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
294                                           iQuant, iDcScaler, predictors, bound_x, bound_y);                                           iQuant, iDcScaler, predictors, bound);
295                  if (!acpred_flag) {                  if (!acpred_flag) {
296                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
297                  }                  }
# Line 246  Line 305 
305                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
306    
307                          if (dc_size > 8) {                          if (dc_size > 8) {
308                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
309                          }                          }
310    
311                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
312                          start_coeff = 1;                          start_coeff = 1;
313    
314                            DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
315                  } else {                  } else {
316                          start_coeff = 0;                          start_coeff = 0;
317                  }                  }
318    
319                  start_timer();                  start_timer();
320                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
321                  {                  {
322                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
323                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
324    
325                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
326                  }                  }
327                  stop_coding_timer();                  stop_coding_timer();
328    
# Line 269  Line 332 
332    
333                  start_timer();                  start_timer();
334                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
335                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
336                  } else {                  } else {
337                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
338                  }                  }
339                  stop_iquant_timer();                  stop_iquant_timer();
340    
341                  start_timer();                  start_timer();
342                  idct(&data[i * 64]);                  idct(&data[i * 64]);
343                  stop_idct_timer();                  stop_idct_timer();
344    
345          }          }
346    
347          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 286  Line 350 
350          }          }
351    
352          start_timer();          start_timer();
353    
354            if (reduced_resolution)
355            {
356                    next_block*=2;
357                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
358                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
359                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
360                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
361                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
362                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
363            }else{
364          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
365          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
366          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
367          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
368          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
369          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
370            }
371          stop_transfer_timer();          stop_transfer_timer();
372  }  }
373    
374    static void
375    decoder_mb_decode(DECODER * dec,
376                                    const uint32_t cbp,
377                                    Bitstream * bs,
378                                    uint8_t * pY_Cur,
379                                    uint8_t * pU_Cur,
380                                    uint8_t * pV_Cur,
381                                    const int reduced_resolution,
382                                    const MACROBLOCK * pMB)
383    {
384            DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);
385            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
386    
387            int stride = dec->edged_width;
388            int next_block = stride * (reduced_resolution ? 16 : 8);
389            const int stride2 = stride/2;
390            int i;
391            const uint32_t iQuant = pMB->quant;
392            const int direction = dec->alternate_vertical_scan ? 2 : 0;
393            const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;
394    
395            for (i = 0; i < 6; i++) {
396    
397                    if (cbp & (1 << (5 - i))) {     /* coded */
398    
399                            memset(block, 0, 64 * sizeof(int16_t)); /* clear */
400    
401                            start_timer();
402                            get_inter_block(bs, block, direction);
403                            stop_coding_timer();
404    
405                            start_timer();
406                            dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices);
407                            stop_iquant_timer();
408    
409  #define SIGN(X) (((X)>0)?1:-1)                          start_timer();
410  #define ABS(X) (((X)>0)?(X):-(X))                          idct(&data[i * 64]);
411  static const uint32_t roundtab[16] =                          stop_idct_timer();
412          { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };                  }
413            }
414    
415            if (dec->interlacing && pMB->field_dct) {
416                    next_block = stride;
417                    stride *= 2;
418            }
419    
420  // decode an inter macroblock          start_timer();
421            if (reduced_resolution) {
422                    if (cbp & 32)
423                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
424                    if (cbp & 16)
425                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
426                    if (cbp & 8)
427                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
428                    if (cbp & 4)
429                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
430                    if (cbp & 2)
431                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
432                    if (cbp & 1)
433                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
434            } else {
435                    if (cbp & 32)
436                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
437                    if (cbp & 16)
438                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
439                    if (cbp & 8)
440                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
441                    if (cbp & 4)
442                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
443                    if (cbp & 2)
444                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
445                    if (cbp & 1)
446                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
447            }
448            stop_transfer_timer();
449    }
450    
451  void  /* decode an inter macroblock */
452    static void
453  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
454                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
455                                  const uint32_t x_pos,                                  const uint32_t x_pos,
456                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t acpred_flag,  
457                                  const uint32_t cbp,                                  const uint32_t cbp,
458                                  Bitstream * bs,                                  Bitstream * bs,
459                                  const uint32_t quant,                                  const uint32_t rounding,
460                                  const uint32_t rounding)                                  const int reduced_resolution,
461                                    const int ref)
462  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
463          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
464          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
465          uint32_t i;          uint32_t i;
466          uint32_t iQuant = pMB->quant;  
467          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
468    
469          int uv_dx, uv_dy;          int uv_dx, uv_dy;
470            VECTOR mv[4];   /* local copy of mvs */
471    
472            if (reduced_resolution) {
473                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
474                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
475                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
476                    for (i = 0; i < 4; i++) {
477                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
478                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
479                    }
480            } else {
481          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
482          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
483          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
484                    for (i = 0; i < 4; i++)
485                            mv[i] = pMB->mvs[i];
486            }
487    
488          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          start_timer();
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
489    
490                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
         } else {  
                 int sum;  
491    
492                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  uv_dx = mv[0].x;
493                  uv_dx =                  uv_dy = mv[0].y;
494                          (sum ==                  if (dec->quarterpel) {
495                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          uv_dx /= 2;
496                                                                    (ABS(sum) / 16) * 2));                          uv_dy /= 2;
   
                 sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                 uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
497          }          }
498                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
499                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
500    
501          start_timer();                  if (reduced_resolution)
502                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
503                                                                            mv[0].x, mv[0].y, stride, rounding);
504                    else if (dec->quarterpel)
505                            interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
506                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
507                                                                                            mv[0].x, mv[0].y, stride, rounding);
508                    else
509                            interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
510                                                                            mv[0].x, mv[0].y, stride, rounding);
511    
512            } else {        /* MODE_INTER4V */
513    
514                    if(dec->quarterpel) {
515                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
516                            uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
517                    } else {
518                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
519                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
520                    }
521    
522                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
523                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
524    
525                    if (reduced_resolution) {
526                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
527                                                                    mv[0].x, mv[0].y, stride, rounding);
528                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
529                                                                    mv[1].x, mv[1].y, stride, rounding);
530                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
531                                                                    mv[2].x, mv[2].y, stride, rounding);
532                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
533                                                                    mv[3].x, mv[3].y, stride, rounding);
534                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
535                                                                    uv_dx, uv_dy, stride2, rounding);
536                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
537                                                                    uv_dx, uv_dy, stride2, rounding);
538    
539                    } else if (dec->quarterpel) {
540                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
541                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
542                                                                            mv[0].x, mv[0].y, stride, rounding);
543                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
544                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
545                                                                            mv[1].x, mv[1].y, stride, rounding);
546                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
547                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
548                                                                            mv[2].x, mv[2].y, stride, rounding);
549                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
550                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
551                                                                            mv[3].x, mv[3].y, stride, rounding);
552                    } else {
553          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos,
554                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);                                                                  mv[0].x, mv[0].y, stride, rounding);
555          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
556                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                  mv[1].x, mv[1].y, stride, rounding);
557                                                    rounding);                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
558          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                  mv[2].x, mv[2].y, stride, rounding);
559                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
560                                                    rounding);                                                                  mv[3].x, mv[3].y, stride, rounding);
561          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                  }
562                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
563                                                    rounding);  
564          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          /* chroma */
565            if (reduced_resolution) {
566                    interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
567                                                                    uv_dx, uv_dy, stride2, rounding);
568                    interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
569                                                                    uv_dx, uv_dy, stride2, rounding);
570            } else {
571                    interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
572                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
573          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
574                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
575            }
576    
577          stop_comp_timer();          stop_comp_timer();
578    
579          for (i = 0; i < 6; i++) {          if (cbp)
580                  if (cbp & (1 << (5 - i)))       // coded                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
581                                                            reduced_resolution, pMB);
582    }
583    
584    static void
585    decoder_mbgmc(DECODER * dec,
586                                    MACROBLOCK * const pMB,
587                                    const uint32_t x_pos,
588                                    const uint32_t y_pos,
589                                    const uint32_t fcode,
590                                    const uint32_t cbp,
591                                    Bitstream * bs,
592                                    const uint32_t rounding)
593                  {                  {
594                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear          const uint32_t stride = dec->edged_width;
595            const uint32_t stride2 = stride / 2;
596    
597                          start_timer();          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
598                          get_inter_block(bs, &block[i * 64]);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
599                          stop_coding_timer();          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
600    
601                          start_timer();          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
602                          if (dec->quant_type == 0) {  
603                                  dequant_inter(&data[i * 64], &block[i * 64], iQuant);          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
604    
605                          start_timer();                          start_timer();
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
606    
607          if (dec->interlacing && pMB->field_dct) {  /* this is where the calculations are done */
608                  next_block = stride;  
609                  stride *= 2;          gmc_data->predict_16x16(gmc_data,
610          }                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
611                            stride, stride, x_pos, y_pos, rounding);
612    
613            gmc_data->predict_8x8(gmc_data,
614                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
615                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
616                            stride2, stride2, x_pos, y_pos, rounding);
617    
618            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
619    
620            pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
621            pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
622    
623            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
624    
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
625          stop_transfer_timer();          stop_transfer_timer();
626    
627            if (cbp)
628                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
629    
630  }  }
631    
632    
633  void  static void
634  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
635                             Bitstream * bs,                             Bitstream * bs,
636                                    int reduced_resolution,
637                             int quant,                             int quant,
638                             int intra_dc_threshold)                             int intra_dc_threshold)
639  {  {
640          uint32_t bound_x, bound_y;          uint32_t bound;
641          uint32_t x, y;          uint32_t x, y;
642            uint32_t mb_width = dec->mb_width;
643            uint32_t mb_height = dec->mb_height;
644    
645          bound_x = bound_y = 0;          if (reduced_resolution) {
646                    mb_width = (dec->width + 31) / 32;
647                    mb_height = (dec->height + 31) / 32;
648            }
649    
650          for (y = 0; y < dec->mb_height; y++) {          bound = 0;
                 for (x = 0; x < dec->mb_width; x++) {  
                         MACROBLOCK *mb;  
651    
652            for (y = 0; y < mb_height; y++) {
653                    for (x = 0; x < mb_width; x++) {
654                            MACROBLOCK *mb;
655                          uint32_t mcbpc;                          uint32_t mcbpc;
656                          uint32_t cbpc;                          uint32_t cbpc;
657                          uint32_t acpred_flag;                          uint32_t acpred_flag;
658                          uint32_t cbpy;                          uint32_t cbpy;
659                          uint32_t cbp;                          uint32_t cbp;
660    
661                          skip_stuffing(bs);                          while (BitstreamShowBits(bs, 9) == 1)
662                                    BitstreamSkip(bs, 9);
663    
664                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
665                          {                          {
666                                  int mbnum = read_video_packet_header(bs, 0);                                  bound = read_video_packet_header(bs, dec, 0,
667                                  x = bound_x = mbnum % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
668                                  y = bound_y = mbnum / dec->mb_width;                                  x = bound % mb_width;
669                                    y = bound / mb_width;
670                          }                          }
   
671                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
672    
673                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
674    
675                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
676                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
677                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
# Line 468  Line 690 
690                                  }                                  }
691                          }                          }
692                          mb->quant = quant;                          mb->quant = quant;
693                            mb->mvs[0].x = mb->mvs[0].y =
694                            mb->mvs[1].x = mb->mvs[1].y =
695                            mb->mvs[2].x = mb->mvs[2].y =
696                            mb->mvs[3].x = mb->mvs[3].y =0;
697    
698                          if (dec->interlacing) {                          if (dec->interlacing) {
699                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
700                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
701                          }                          }
702    
703                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
704                                                          intra_dc_threshold, bound_x, bound_y);                                                          intra_dc_threshold, bound, reduced_resolution);
705    
706                  }                  }
707                    if(dec->out_frm)
708                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
709          }          }
710    
711  }  }
712    
713    
714  void  static void
715  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
716                                    Bitstream * bs,                                    Bitstream * bs,
717                                    int x,                                    int x,
718                                    int y,                                    int y,
719                                    int k,                                    int k,
720                                    VECTOR * mv,                                  VECTOR * ret_mv,
721                                    int fcode,                                    int fcode,
722                                    const int bound_x,                                  const int bound)
                                   const int bound_y)  
723  {  {
724    
725          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
726          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
727          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
728          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
   
         VECTOR pmv[4];  
         int32_t psad[4];  
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
729    
730            const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
731            VECTOR mv;
732    
733          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad, bound_x, bound_y);          mv.x = get_mv(bs, fcode);
734            mv.y = get_mv(bs, fcode);
735    
736          pmv_x = pmv[0].x;          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
         pmv_y = pmv[0].y;  
737    
738          mv_x = get_mv(bs, fcode);          mv.x += pmv.x;
739          mv_y = get_mv(bs, fcode);          mv.y += pmv.y;
740    
741          mv_x += pmv_x;          if (mv.x < low) {
742          mv_y += pmv_y;                  mv.x += range;
743            } else if (mv.x > high) {
744          if (mv_x < low) {                  mv.x -= range;
                 mv_x += range;  
         } else if (mv_x > high) {  
                 mv_x -= range;  
745          }          }
746    
747          if (mv_y < low) {          if (mv.y < low) {
748                  mv_y += range;                  mv.y += range;
749          } else if (mv_y > high) {          } else if (mv.y > high) {
750                  mv_y -= range;                  mv.y -= range;
751          }          }
752    
753          mv->x = mv_x;          ret_mv->x = mv.x;
754          mv->y = mv_y;          ret_mv->y = mv.y;
   
755  }  }
756    
757    /* for P_VOP set gmc_warp to NULL */
758  void  static void
759  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
760                             Bitstream * bs,                             Bitstream * bs,
761                             int rounding,                             int rounding,
762                                    int reduced_resolution,
763                             int quant,                             int quant,
764                             int fcode,                             int fcode,
765                             int intra_dc_threshold)                                  int intra_dc_threshold,
766                                    const WARPPOINTS *const gmc_warp)
767  {  {
   
768          uint32_t x, y;          uint32_t x, y;
769          uint32_t bound_x, bound_y;          uint32_t bound;
770            int cp_mb, st_mb;
771            uint32_t mb_width = dec->mb_width;
772            uint32_t mb_height = dec->mb_height;
773    
774            if (reduced_resolution) {
775                    mb_width = (dec->width + 31) / 32;
776                    mb_height = (dec->height + 31) / 32;
777            }
778    
779          start_timer();          start_timer();
780          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
781                                     dec->width, dec->height, dec->interlacing);                                          dec->width, dec->height);
782          stop_edges_timer();          stop_edges_timer();
783    
784          bound_x = bound_y = 0;          if (gmc_warp) {
785                    /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
786                    generate_GMCparameters( dec->sprite_warping_points,
787                                    dec->sprite_warping_accuracy, gmc_warp,
788                                    dec->width, dec->height, &dec->new_gmc_data);
789    
790          for (y = 0; y < dec->mb_height; y++) {                  /* image warping is done block-based in decoder_mbgmc(), now */
791                  for (x = 0; x < dec->mb_width; x++) {          }
792    
793            bound = 0;
794    
795            for (y = 0; y < mb_height; y++) {
796                    cp_mb = st_mb = 0;
797                    for (x = 0; x < mb_width; x++) {
798                          MACROBLOCK *mb;                          MACROBLOCK *mb;
799    
800                          skip_stuffing(bs);                          /* skip stuffing */
801                          if (check_resync_marker(bs, 0))                          while (BitstreamShowBits(bs, 10) == 1)
802                          {                                  BitstreamSkip(bs, 10);
803                                  int mbnum = read_video_packet_header(bs, 0);  
804                                  x = bound_x = mbnum % dec->mb_width;                          if (check_resync_marker(bs, fcode - 1)) {
805                                  y = bound_y = mbnum / dec->mb_width;                                  bound = read_video_packet_header(bs, dec, fcode - 1,
806                                            &quant, &fcode, NULL, &intra_dc_threshold);
807                                    x = bound % mb_width;
808                                    y = bound / mb_width;
809                          }                          }
810                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
811    
812                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
813                          if (!(BitstreamGetBit(bs)))     // not_coded  
814                          {                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
815                                  uint32_t mcbpc;                                  uint32_t mcbpc, cbpc, cbpy, cbp;
816                                  uint32_t cbpc;                                  uint32_t intra, acpred_flag = 0;
817                                  uint32_t acpred_flag;                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
818    
819                                    cp_mb++;
820                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
821                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
822                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
823                                  acpred_flag = 0;  
824                                    DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
825                                    DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
826    
827                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
828    
829                                  if (intra) {                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
830                                            mcsel = BitstreamGetBit(bs);
831                                    else if (intra)
832                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
833    
834                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
835                                    DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
836    
837                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
838    
839                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
840                                          quant += dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
841                                            DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
842                                            quant += dquant;
843                                          if (quant > 31) {                                          if (quant > 31) {
844                                                  quant = 31;                                                  quant = 31;
845                                          } else if (mb->quant < 1) {                                          } else if (quant < 1) {
846                                                  quant = 1;                                                  quant = 1;
847                                          }                                          }
848                                            DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
849                                  }                                  }
850                                  mb->quant = quant;                                  mb->quant = quant;
851    
852                                  if (dec->interlacing) {                                  if (dec->interlacing) {
853                                            if ((cbp || intra) && !mcsel) {
854                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
855                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
856                                            }
857    
858                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
859                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
860                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
861    
862                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
863                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
864                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
865                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
866                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
867                                                  }                                                  }
868                                          }                                          }
869                                  }                                  }
870    
871                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
872                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
873                                            continue;
874    
875                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
876    
877                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
878                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
879                                                                                    fcode, bound_x, bound_y);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],  
                                                                                   fcode, bound_x, bound_y);  
880                                          } else {                                          } else {
881                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
882                                                                                    fcode, bound_x, bound_y);                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
883                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          }
884                                                          mb->mvs[0].x;                                  } else if (mb->mode == MODE_INTER4V ) {
885                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
886                                                          mb->mvs[0].y;                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
887                                          }                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
888                                  } else if (mb->mode ==                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
889                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */
890                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound_x, bound_y);                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
891                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound_x, bound_y);                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
                                         get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound_x, bound_y);  
                                         get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound_x, bound_y);  
                                 } 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;  
892                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
893                                                                          intra_dc_threshold, bound_x, bound_y);                                                                          intra_dc_threshold, bound, reduced_resolution);
894                                          continue;                                          continue;
895                                  }                                  }
896    
897                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
898                                                                  rounding);                                                                  rounding, reduced_resolution, 0);
899                          } else                          // not coded  
900                          {                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
901                                  //DEBUG2("P-frame MB at (X,Y)=",x,y);                                  mb->mode = MODE_NOT_CODED_GMC;
902                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
903    
904                                    if(dec->out_frm && cp_mb > 0) {
905                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
906                                            cp_mb = 0;
907                                    }
908                                    st_mb = x+1;
909                            } else {        /* not coded P_VOP macroblock */
910                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
911    
912                                  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;
913                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
914    
915                                  // copy macroblock directly from ref to cur                                  decoder_mbinter(dec, mb, x, y, 0, bs,
916                                                                    rounding, reduced_resolution, 0);
                                 start_timer();  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
                                                                  (16 * x),  
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (16 * x), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
                                                                  (16 * x + 8),  
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (16 * x + 8), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +  
                                                                  (16 * x),  
                                                                  dec->refn[0].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[0].y + (16 * y +  
                                                                                                    8) * dec->edged_width +  
                                                                  (16 * x + 8), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +  
                                                                  (8 * x),  
                                                                  dec->refn[0].u +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
                                                                  dec->edged_width / 2);  
   
                                 transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +  
                                                                  (8 * x),  
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
                                                                  dec->edged_width / 2);  
917    
918                                  stop_transfer_timer();                                  if(dec->out_frm && cp_mb > 0) {
919                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
920                                            cp_mb = 0;
921                          }                          }
922                                    st_mb = x+1;
923                  }                  }
924          }          }
925    
926                    if(dec->out_frm && cp_mb > 0)
927                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
928            }
929  }  }
930    
931    
932  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
933  // decode B-frame motion vector  static void
934  void  get_b_motion_vector(Bitstream * bs,
 get_b_motion_vector(DECODER * dec,  
                                         Bitstream * bs,  
                                         int x,  
                                         int y,  
935                                          VECTOR * mv,                                          VECTOR * mv,
936                                          int fcode,                                          int fcode,
937                                          const VECTOR pmv)                                          const VECTOR pmv)
938  {  {
939          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
940          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
941          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
942          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
   
         pmv_x = pmv.x;  
         pmv_y = pmv.y;  
943    
944          mv_x = get_mv(bs, fcode);          int mv_x = get_mv(bs, fcode);
945          mv_y = get_mv(bs, fcode);          int mv_y = get_mv(bs, fcode);
946    
947          mv_x += pmv_x;          mv_x += pmv.x;
948          mv_y += pmv_y;          mv_y += pmv.y;
949    
950          if (mv_x < low) {          if (mv_x < low)
951                  mv_x += range;                  mv_x += range;
952          } else if (mv_x > high) {          else if (mv_x > high)
953                  mv_x -= range;                  mv_x -= range;
         }  
954    
955          if (mv_y < low) {          if (mv_y < low)
956                  mv_y += range;                  mv_y += range;
957          } else if (mv_y > high) {          else if (mv_y > high)
958                  mv_y -= range;                  mv_y -= range;
         }  
959    
960          mv->x = mv_x;          mv->x = mv_x;
961          mv->y = mv_y;          mv->y = mv_y;
962  }  }
963    
964    /* decode an B-frame direct & interpolate macroblock */
965  // add by MinChen <chenm001@163.com>  static void
966  // decode an B-frame forward & backward inter macroblock  decoder_bf_interpolate_mbinter(DECODER * dec,
967  void                                                                  IMAGE forward,
968  decoder_bf_mbinter(DECODER * dec,                                                                  IMAGE backward,
969                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
970                                     const uint32_t x_pos,                                     const uint32_t x_pos,
971                                     const uint32_t y_pos,                                     const uint32_t y_pos,
                                    const uint32_t cbp,  
972                                     Bitstream * bs,                                     Bitstream * bs,
973                                     const uint32_t quant,                                                                  const int direct)
                                    const uint8_t ref)  
974  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
975          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
976          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
977          int uv_dx, uv_dy;          int uv_dx, uv_dy;
978            int b_uv_dx, b_uv_dy;
979            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
980            const uint32_t cbp = pMB->cbp;
981    
982          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
983          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
984          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
985    
986          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!direct) {
987                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
988                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
989    
990                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  b_uv_dx = pMB->b_mvs[0].x;
991                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  b_uv_dy = pMB->b_mvs[0].y;
         } else {  
                 int sum;  
992    
993                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  if (dec->quarterpel) {
994                  uv_dx =                          uv_dx /= 2;
995                          (sum ==                          uv_dy /= 2;
996                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          b_uv_dx /= 2;
997                                                                    (ABS(sum) / 16) * 2));                          b_uv_dy /= 2;
   
                 sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
                 uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
998          }          }
999    
1000          start_timer();                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1001          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
                                                   pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,  
                                                   16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,  
                                                   16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  
                                                   0);  
         interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,  
                                                   16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  
                                                   0);  
         interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         stop_comp_timer();  
   
         for (i = 0; i < 6; i++) {  
                 if (cbp & (1 << (5 - i)))       // coded  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
1002    
1003                          start_timer();                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1004                          get_inter_block(bs, &block[i * 64]);                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
                         stop_coding_timer();  
1005    
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
1006                          } else {                          } else {
1007                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                  if(dec->quarterpel) {
1008                          }                          uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1009                          stop_iquant_timer();                          uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1010                            b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1011                          start_timer();                          b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1012                          idct(&data[i * 64]);                  } else {
1013                          stop_idct_timer();                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1014                  }                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1015                            b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1016                            b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1017          }          }
1018    
1019          if (dec->interlacing && pMB->field_dct) {                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1020                  next_block = stride;                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1021                  stride *= 2;                  b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1022                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1023          }          }
1024    
1025          start_timer();          start_timer();
1026          if (cbp & 32)          if(dec->quarterpel) {
1027                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  if(!direct) {
1028          if (cbp & 16)                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1029                  transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1030          if (cbp & 8)                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
   
 // add by MinChen <chenm001@163.com>  
 // decode an B-frame direct &  inter macroblock  
 void  
 decoder_bf_interpolate_mbinter(DECODER * dec,  
                                                            IMAGE forward,  
                                                            IMAGE backward,  
                                                            const MACROBLOCK * pMB,  
                                                            const uint32_t x_pos,  
                                                            const uint32_t y_pos,  
                                                            const uint32_t cbp,  
                                                            Bitstream * bs)  
 {  
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         uint32_t stride = dec->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint32_t iQuant = pMB->quant;  
         int uv_dx, uv_dy;  
         int b_uv_dx, b_uv_dy;  
         uint32_t i;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
   
         pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);  
         pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
         if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
   
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
                 b_uv_dx = pMB->b_mvs[0].x;  
                 b_uv_dy = pMB->b_mvs[0].y;  
   
                 b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
1031          } else {          } else {
1032                  int sum;                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1033                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1034                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1035                  uv_dx =                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1036                          (sum ==                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1037                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                                                                                  pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1038                                                                    (ABS(sum) / 16) * 2));                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1039                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1040                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                                                                                  pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1041                  uv_dy =                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1042                          (sum ==                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1043                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                                   (ABS(sum) / 16) * 2));  
   
                 sum =  
                         pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +  
                         pMB->b_mvs[3].x;  
                 b_uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
   
                 sum =  
                         pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +  
                         pMB->b_mvs[3].y;  
                 b_uv_dy =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1044          }          }
1045            } else {
   
         start_timer();  
1046          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1047                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1048          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
# Line 942  Line 1050 
1050          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1051                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1052          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1053                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1054                                                    0);          }
1055    
1056          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1057                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1058          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1059                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1060    
1061    
1062          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1063                    if(!direct) {
1064                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1065                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1066                                                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1067                    } else {
1068                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1069                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1070                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1071          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1072                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1073                                                    0);                                                                                  pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1074          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1075                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1076                                                    stride, 0);                                                                                  pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1077          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1078                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1079                                                    stride, 0);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1080          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,                  }
1081            } else {
1082                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1083                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1084                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1085                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1086                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1087                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1088                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1089                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1090            }
1091    
1092            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1093                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1094          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1095                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1096    
1097          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1098                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1099          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1100                                           stride);                                                  stride, 1, 8);
1101          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1102                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1103          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1104                                           16 * y_pos + 8, stride);                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1105          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1106                                           stride2);  
1107          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1108                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1109                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1110                                                    stride, 1, 8);
1111    
1112            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1113                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1114                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1115                                                    stride, 1, 8);
1116    
1117            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1118                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1119                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1120                                                    stride2, 1, 8);
1121    
1122            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1123                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1124                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1125                                                    stride2, 1, 8);
1126    
1127          stop_comp_timer();          stop_comp_timer();
1128    
1129          for (i = 0; i < 6; i++) {          if (cbp)
1130                  if (cbp & (1 << (5 - i)))       // coded                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64]);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
1131                          }                          }
                         stop_iquant_timer();  
1132    
1133                          start_timer();  /* for decode B-frame dbquant */
1134                          idct(&data[i * 64]);  static __inline int32_t
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
   
 // add by MinChen <chenm001@163.com>  
 // for decode B-frame dbquant  
 int32_t __inline  
1135  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1136  {  {
1137          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))               /*  '0' */
1138                  return (0);                  return (0);
1139          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs))  /* '10' */
1140                  return (-2);                  return (-2);
1141          else          else                                                    /* '11' */
1142                  return (2);                             // '11'                  return (2);
1143  }  }
1144    
1145  // add by MinChen <chenm001@163.com>  /*
1146  // for decode B-frame mb_type   * decode B-frame mb_type
1147  // bit   ret_value   * bit          ret_value
1148  // 1        0   * 1            0
1149  // 01       1   * 01           1
1150  // 001      2   * 001          2
1151  // 0001     3   * 0001         3
1152  int32_t __inline   */
1153    static int32_t __inline
1154  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1155  {  {
1156          int32_t mb_type;          int32_t mb_type;
1157    
1158          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1159                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1160                  return (mb_type);                  return (mb_type);
1161          else  
1162                  return (-1);          return -1;
1163  }  }
1164    
1165  void  static void
1166  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1167                             Bitstream * bs,                             Bitstream * bs,
1168                             int quant,                             int quant,
1169                             int fcode_forward,                             int fcode_forward,
1170                             int fcode_backward)                             int fcode_backward)
1171  {  {
   
1172          uint32_t x, y;          uint32_t x, y;
1173          VECTOR mv, zeromv;          VECTOR mv;
1174            const VECTOR zeromv = {0,0};
1175            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1176            int i;
1177    
1178          start_timer();          start_timer();
1179          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1180                                     dec->width, dec->height, dec->interlacing);                                          dec->width, dec->height);
1181          //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1182                                            dec->width, dec->height);
1183          stop_edges_timer();          stop_edges_timer();
1184    
   
1185          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1186                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1187                  dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;                  dec->p_fmv = dec->p_bmv = zeromv;
1188                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1189                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1190                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1191                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1192                            uint32_t intra_dc_threshold; /* fake variable */
1193    
1194                            if (check_resync_marker(bs, fcode_max  - 1)) {
1195                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1196                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1197                                    x = bound % dec->mb_width;
1198                                    y = bound / dec->mb_width;
1199                                    /* reset predicted macroblocks */
1200                                    dec->p_fmv = dec->p_bmv = zeromv;
1201                            }
1202    
1203                            mv =
1204                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1205                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1206                            mb->quant = quant;
1207    
1208                          mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y =                          /*
1209                                  0;                           * skip if the co-located P_VOP macroblock is not coded
1210                             * if not codec in co-located S_VOP macroblock is _not_
1211                             * automatically skipped
1212                             */
1213    
                         // the last P_VOP is skip macroblock ?  
1214                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
                                 //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);  
                                 mb->mb_type = MODE_FORWARD;  
1215                                  mb->cbp = 0;                                  mb->cbp = 0;
1216                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  mb->mode = MODE_FORWARD;
1217                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 mb->quant = 8;  
   
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);  
1218                                  continue;                                  continue;
1219                          }                          }
                         //t=BitstreamShowBits(bs,32);  
1220    
1221                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1222                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1223    
1224                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1225    
1226                                  if (!modb2) {   // modb=='00'                                  if (!modb2)             /* modb=='00' */
1227                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1228                                  } else {                                  else
1229                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1230    
1231                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1232                                            quant += get_dbquant(bs);
1233                                            if (quant > 31)
1234                                                  quant = 31;                                                  quant = 31;
1235                                          } else if (mb->quant < 1) {                                          else if (quant < 1)
1236                                                  quant = 1;                                                  quant = 1;
1237                                          }                                          }
                                 } else {  
                                         quant = 8;  
                                 }  
1238                                  mb->quant = quant;                                  mb->quant = quant;
1239    
1240                                    if (dec->interlacing) {
1241                                            if (mb->cbp) {
1242                                                    mb->field_dct = BitstreamGetBit(bs);
1243                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1244                                            }
1245    
1246                                            if (mb->mode) {
1247                                                    mb->field_pred = BitstreamGetBit(bs);
1248                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1249    
1250                                                    if (mb->field_pred) {
1251                                                            mb->field_for_top = BitstreamGetBit(bs);
1252                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1253                                                            mb->field_for_bot = BitstreamGetBit(bs);
1254                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1255                                                    }
1256                                            }
1257                                    }
1258    
1259                          } else {                          } else {
1260                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1261                                  mb->cbp = 0;                                  mb->cbp = 0;
1262                          }                          }
1263    
1264                          mb->mode = MODE_INTER;                          switch (mb->mode) {
                         //DEBUG1("Switch bm_type=",mb->mb_type);  
   
                         switch (mb->mb_type) {  
1265                          case MODE_DIRECT:                          case MODE_DIRECT:
1266                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv);
1267    
1268                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {                               // Because this file is a C file not C++ so I use '{' to define var  
                                         const int64_t TRB = dec->time_pp - dec->time_bp, TRD =  
                                                 dec->time_pp;  
                                         int i;  
   
1269                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1270                                                  mb->mvs[i].x =                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);
1271                                                          (int32_t) ((TRB * last_mb->mvs[i].x) / TRD +                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1272                                                                             mb->mvs[0].x);                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD
1273                                                  mb->b_mvs[i].x =                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);
1274                                                          (int32_t) ((mb->mvs[0].x ==                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);
1275                                                                                  0) ? ((TRB -                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1276                                                                                             TRD) * last_mb->mvs[i].x) /                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD
1277                                                                             TRD : mb->mvs[i].x - last_mb->mvs[i].x);                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);
                                                 mb->mvs[i].y =  
                                                         (int32_t) ((TRB * last_mb->mvs[i].y) / TRD +  
                                                                            mb->mvs[0].y);  
                                                 mb->b_mvs[i].y =  
                                                         (int32_t) ((mb->mvs[0].y ==  
                                                                                 0) ? ((TRB -  
                                                                                            TRD) * last_mb->mvs[i].y) /  
                                                                            TRD : mb->mvs[i].y - last_mb->mvs[i].y);  
                                         }  
                                         //DEBUG("B-frame Direct!\n");  
1278                                  }                                  }
1279                                  mb->mode = MODE_INTER4V;  
1280                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1281                                                                                             mb, x, y, mb->cbp, bs);                                                                                                  mb, x, y, bs, 1);
1282                                  break;                                  break;
1283    
1284                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1285                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
1286                                                                          dec->p_fmv);                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1287                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
1288                                          mb->mvs[0].x;                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1289                                  dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
                                         mb->mvs[0].y;  
   
                                 get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],  
                                                                         fcode_backward, dec->p_bmv);  
                                 dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x =  
                                         mb->b_mvs[3].x = mb->b_mvs[0].x;  
                                 dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y =  
                                         mb->b_mvs[3].y = mb->b_mvs[0].y;  
1290    
1291                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1292                                                                                             mb, x, y, mb->cbp, bs);                                                                                          mb, x, y, bs, 0);
                                 //DEBUG("B-frame Bidir!\n");  
1293                                  break;                                  break;
1294    
1295                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1296                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);
1297                                                                          dec->p_bmv);                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                 dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
                                         mb->mvs[0].x;  
                                 dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1298    
1299                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
                                 //DEBUG("B-frame Backward!\n");  
1300                                  break;                                  break;
1301    
1302                          case MODE_FORWARD:                          case MODE_FORWARD:
1303                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
1304                                                                          dec->p_fmv);                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                 dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =  
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1305    
1306                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 //DEBUG("B-frame Forward!\n");  
1307                                  break;                                  break;
1308    
1309                          default:                          default:
1310                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1311                          }                          }
1312                    } /* End of for */
                 }                                               // end of FOR  
1313          }          }
1314  }  }
1315    
1316  // swap two MACROBLOCK array  /* perform post processing if necessary, and output the image */
1317  void  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1318  mb_swap(MACROBLOCK ** mb1,                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1319                  MACROBLOCK ** mb2)  {
1320            if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV))    /* post process */
1321  {  {
1322          MACROBLOCK *temp = *mb1;                  /* note: image is stored to tmp */
1323                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1324                    image_deblock(&dec->tmp, dec->edged_width,
1325                                              mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1326                                              frame->general);
1327                    img = &dec->tmp;
1328            }
1329    
1330          *mb1 = *mb2;          image_output(img, dec->width, dec->height,
1331          *mb2 = temp;                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1332                                     frame->output.csp, dec->interlacing);
1333    
1334            if (stats) {
1335                    stats->type = coding2type(coding_type);
1336                    stats->data.vop.time_base = (int)dec->time_base;
1337                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1338            }
1339  }  }
1340    
1341    
1342  int  int
1343  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1344                             XVID_DEC_FRAME * frame)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1345  {  {
1346    
1347          Bitstream bs;          Bitstream bs;
1348          uint32_t rounding;          uint32_t rounding;
1349            uint32_t reduced_resolution;
1350          uint32_t quant;          uint32_t quant;
1351          uint32_t fcode_forward;          uint32_t fcode_forward;
1352          uint32_t fcode_backward;          uint32_t fcode_backward;
1353          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1354          uint32_t vop_type;          WARPPOINTS gmc_warp;
1355            int coding_type;
1356            int success, output, seen_something;
1357    
1358            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1359                    return XVID_ERR_VERSION;
1360    
1361          start_global_timer();          start_global_timer();
1362    
1363            dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1364            if ((frame->general & XVID_DISCONTINUITY))
1365                    dec->frames = 0;
1366            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1367    
1368            if (frame->length < 0) {        /* decoder flush */
1369                    int ret;
1370                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1371                            we have a reference frame, then outout the reference frame */
1372                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1373                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1374                            dec->frames = 0;
1375                            ret = 0;
1376                    } else {
1377                            if (stats) stats->type = XVID_TYPE_NOTHING;
1378                            ret = XVID_ERR_END;
1379                    }
1380    
1381                    emms();
1382                    stop_global_timer();
1383                    return ret;
1384            }
1385    
1386          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1387    
1388          // add by chenm001 <chenm001@163.com>          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1389          // for support B-frame to reference last 2 frame          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1390          dec->frames++;          {
1391          vop_type =                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1392                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1393                                                           &fcode_backward, &intra_dc_threshold);                  if (stats) stats->type = XVID_TYPE_NOTHING;
1394                    emms();
1395                    return 1;       /* one byte consumed */
1396            }
1397    
1398          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          success = 0;
1399            output = 0;
1400            seen_something = 0;
1401    
1402          switch (vop_type) {  repeat:
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, quant, fcode_forward,  
                                            intra_dc_threshold);  
                 DEBUG1("P_VOP  Time=", dec->time);  
                 break;  
1403    
1404          case I_VOP:          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1405                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
                 DEBUG1("I_VOP  Time=", dec->time);  
                 break;  
1406    
1407          case B_VOP:          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1408  #ifdef BFRAMES_DEC                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1409                  if (dec->time_pp > dec->time_bp) {  
1410                          DEBUG1("B_VOP  Time=", dec->time);          if (coding_type == -1) { /* nothing */
1411                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                  if (success) goto done;
1412                  } else {                  if (stats) stats->type = XVID_TYPE_NOTHING;
1413                          DEBUG("broken B-frame!");                  emms();
1414                    return BitstreamPos(&bs)/8;
1415            }
1416    
1417            if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1418    
1419                    if (coding_type == -3)
1420                            decoder_resize(dec);
1421    
1422                    if (stats) {
1423                            stats->type = XVID_TYPE_VOL;
1424                            stats->data.vol.general = 0;
1425                            /*XXX: if (dec->interlacing)
1426                                    stats->data.vol.general |= ++INTERLACING; */
1427                            stats->data.vol.width = dec->width;
1428                            stats->data.vol.height = dec->height;
1429                            stats->data.vol.par = dec->aspect_ratio;
1430                            stats->data.vol.par_width = dec->par_width;
1431                            stats->data.vol.par_height = dec->par_height;
1432                            emms();
1433                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1434                    }
1435                    goto repeat;
1436                  }                  }
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
                 break;  
1437    
1438          case N_VOP:                             // vop not coded          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1439                  // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1440            /* packed_mode: special-N_VOP treament */
1441            if (dec->packed_mode && coding_type == N_VOP) {
1442                    if (dec->low_delay_default && dec->frames > 0) {
1443                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1444                            output = 1;
1445                    }
1446                    /* ignore otherwise */
1447            } else if (coding_type != B_VOP) {
1448                    switch(coding_type) {
1449                    case I_VOP :
1450                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1451                            break;
1452                    case P_VOP :
1453                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1454                                                    fcode_forward, intra_dc_threshold, NULL);
1455                            break;
1456                    case S_VOP :
1457                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1458                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1459                            break;
1460                    case N_VOP :
1461                            /* XXX: not_coded vops are not used for forward prediction */
1462                            /* we should not swap(last_mbs,mbs) */
1463                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1464                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1465                  break;                  break;
   
         default:  
                 return XVID_ERR_FAIL;  
1466          }          }
1467    
1468          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution) {
1469                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1470                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1471                                    16, 0);
1472                    }
1473    
1474  #ifdef BFRAMES_DEC                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1475          // test if no B_VOP                  if (!(dec->low_delay_default && dec->packed_mode)) {
1476          if (dec->low_delay) {          if (dec->low_delay) {
1477  #endif                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1478                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                  output = 1;
1479                                           frame->image, frame->stride, frame->colorspace);                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1480  #ifdef BFRAMES_DEC                                  /* output the reference frame */
1481          } else {                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1482                  if (dec->frames >= 1) {                                  output = 1;
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
1483                          }                          }
                         stop_conv_timer();  
1484                  }                  }
         }  
 #endif  
1485    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1486                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1487                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1488                  // swap MACROBLOCK                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1489                  if (dec->low_delay && vop_type == P_VOP)                  dec->last_reduced_resolution = reduced_resolution;
1490                          mb_swap(&dec->mbs, &dec->last_mbs);                  dec->last_coding_type = coding_type;
1491    
1492                    dec->frames++;
1493                    seen_something = 1;
1494    
1495            } else {        /* B_VOP */
1496    
1497                    if (dec->low_delay) {
1498                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1499                            dec->low_delay = 1;
1500          }          }
1501    
1502          emms();                  if (dec->frames < 2) {
1503                            /* attemping to decode a bvop without atleast 2 reference frames */
1504                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1505                                                    "broken b-frame, mising ref frames");
1506                            stats->type = XVID_TYPE_NOTHING;
1507                    } else if (dec->time_pp <= dec->time_bp) {
1508                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1509                            decoded in vfw. */
1510                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1511                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1512                            stats->type = XVID_TYPE_NOTHING;
1513                    } else {
1514                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1515                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1516                    }
1517    
1518                    output = 1;
1519                    dec->frames++;
1520            }
1521    
1522            BitstreamByteAlign(&bs);
1523    
1524            /* low_delay_default mode: repeat in packed_mode */
1525            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
1526                    success = 1;
1527                    goto repeat;
1528            }
1529    
1530    done :
1531    
1532            /* low_delay_default mode: if we've gotten here without outputting anything,
1533               then output the recently decoded frame, or print an error message  */
1534            if (dec->low_delay_default && output == 0) {
1535                    if (dec->packed_mode && seen_something) {
1536                            /* output the recently decoded frame */
1537                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1538                    } else {
1539                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1540                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1541                                    "warning: nothing to output");
1542                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1543                                    "bframe decoder lag");
1544    
1545                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1546                            if (stats) stats->type = XVID_TYPE_NOTHING;
1547                    }
1548            }
1549    
1550            emms();
1551          stop_global_timer();          stop_global_timer();
1552    
1553          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1554  }  }

Legend:
Removed from v.1.22  
changed lines
  Added in v.1.49.2.24

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