[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.28, Fri Jul 12 00:49:59 2002 UTC revision 1.49, Wed Feb 19 21:59:30 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   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  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.  
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
9   *  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 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  10.07.2002  added BFRAMES_DEC_DEBUG support  
  *              Fix a little bug for low_delay flage  
  *              MinChen <chenm001@163.com>  
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
  *  22.06.2002  added primative N_VOP support  
  *              #define BFRAMES_DEC now enables Minchen'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>  
  *  
22   *  $Id$   *  $Id$
23   *   *
24   *************************************************************************/   ****************************************************************************/
25    
26    #include <stdio.h>
27  #include <stdlib.h>  #include <stdlib.h>
28  #include <string.h>  #include <string.h>
29    
# Line 66  Line 33 
33    
34  #include "xvid.h"  #include "xvid.h"
35  #include "portab.h"  #include "portab.h"
36    #include "global.h"
37    
38  #include "decoder.h"  #include "decoder.h"
39  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 77  Line 45 
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
47  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
48    #include "image/reduced.h"
49    #include "image/font.h"
50    
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55    #include "motion/motion.h"
56    
57  #include "image/image.h"  #include "image/image.h"
58  #include "image/colorspace.h"  #include "image/colorspace.h"
59  #include "utils/mem_align.h"  #include "utils/mem_align.h"
60    
61  int  int
62  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
63  {  {
64          DECODER *dec;          /* free existing */
65            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
66            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
67            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
68            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
69            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
70    
71          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;  
72    
73          dec->width = param->width;          if (dec->last_mbs)
74          dec->height = param->height;                  xvid_free(dec->last_mbs);
75            if (dec->mbs)
76                    xvid_free(dec->mbs);
77    
78            /* realloc */
79          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
80          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
81    
82          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
83          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
84    
85          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
86                  xvid_free(dec);                  xvid_free(dec);
# Line 118  Line 92 
92                  xvid_free(dec);                  xvid_free(dec);
93                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
94          }          }
95          // add by chenm001 <chenm001@163.com>  
96          // for support B-frame to reference last 2 frame          /* Support B-frame to reference last 2 frame */
97          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
98                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
99                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
100                  xvid_free(dec);                  xvid_free(dec);
101                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
102          }          }
103          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
104                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
105                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
106                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 134  Line 108 
108                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
109          }          }
110    
111            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
112                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
113                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
114                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
115                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
116                    xvid_free(dec);
117                    return XVID_ERR_MEMORY;
118            }
119    
120            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
121                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
122                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
123                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
124                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
125                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
126                    xvid_free(dec);
127                    return XVID_ERR_MEMORY;
128            }
129    
130          dec->mbs =          dec->mbs =
131                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
132                                          CACHE_LINE);                                          CACHE_LINE);
# Line 141  Line 134 
134                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
135                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
138                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
139                  xvid_free(dec);                  xvid_free(dec);
140                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
141          }          }
   
142          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
143    
144          // add by chenm001 <chenm001@163.com>          /* For skip MB flag */
         // for skip MB flag  
145          dec->last_mbs =          dec->last_mbs =
146                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
147                                          CACHE_LINE);                                          CACHE_LINE);
# Line 158  Line 150 
150                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
151                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
154                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
155                  xvid_free(dec);                  xvid_free(dec);
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          }          }
158    
159          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);
160    
161            return XVID_ERR_OK;
162    }
163    
164    
165    int
166    decoder_create(XVID_DEC_PARAM * param)
167    {
168            DECODER *dec;
169    
170            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
171            if (dec == NULL) {
172                    return XVID_ERR_MEMORY;
173            }
174            memset(dec, 0, sizeof(DECODER));
175    
176            param->handle = dec;
177    
178            dec->width = param->width;
179            dec->height = param->height;
180    
181            image_null(&dec->cur);
182            image_null(&dec->refn[0]);
183            image_null(&dec->refn[1]);
184            image_null(&dec->tmp);
185            image_null(&dec->qtmp);
186    
187            /* image based GMC */
188            image_null(&dec->gmc);
189    
190    
191            dec->mbs = NULL;
192            dec->last_mbs = NULL;
193    
194          init_timer();          init_timer();
195    
196          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
197          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
198          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
199            dec->low_delay = 0;
200            dec->packed_mode = 0;
201    
202            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
203    
204            if (dec->fixed_dimensions)
205                    return decoder_resize(dec);
206            else
207          return XVID_ERR_OK;          return XVID_ERR_OK;
208  }  }
209    
# Line 181  Line 213 
213  {  {
214          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
215          xvid_free(dec->mbs);          xvid_free(dec->mbs);
216    
217            /* image based GMC */
218            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
219    
220          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
221          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
222          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
223            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
225          xvid_free(dec);          xvid_free(dec);
226    
# Line 200  Line 237 
237    
238    
239    
240  // decode an intra macroblock  /* decode an intra macroblock */
   
241  void  void
242  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
243                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 212  Line 248 
248                                  Bitstream * bs,                                  Bitstream * bs,
249                                  const uint32_t quant,                                  const uint32_t quant,
250                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
251                                  const unsigned int bound)                                  const unsigned int bound,
252                                    const int reduced_resolution)
253  {  {
254    
255          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 225  Line 262 
262          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
263          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
264    
265            if (reduced_resolution) {
266                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
267                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
268                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
269            }else{
270          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
271          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
272          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
273            }
274    
275          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
276    
277          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
278                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 252  Line 295 
295                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
296    
297                          if (dc_size > 8) {                          if (dc_size > 8) {
298                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
299                          }                          }
300    
301                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
# Line 264  Line 307 
307                  }                  }
308    
309                  start_timer();                  start_timer();
310                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
311                  {                  {
312                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
313                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
314    
315                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
316                  }                  }
317                  stop_coding_timer();                  stop_coding_timer();
318    
# Line 286  Line 331 
331                  start_timer();                  start_timer();
332                  idct(&data[i * 64]);                  idct(&data[i * 64]);
333                  stop_idct_timer();                  stop_idct_timer();
334    
335          }          }
336    
337          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 294  Line 340 
340          }          }
341    
342          start_timer();          start_timer();
343    
344            if (reduced_resolution)
345            {
346                    next_block*=2;
347                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
348                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
349                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
350                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
351                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
352                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
353            }else{
354          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
355          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
356          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
357          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
358          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
359          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
360            }
361          stop_transfer_timer();          stop_transfer_timer();
362  }  }
363    
364    
365    
366    
367    /* decode an inter macroblock */
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
 static const uint32_t roundtab[16] =  
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
   
 // decode an inter macroblock  
   
368  void  void
369  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
370                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
371                                  const uint32_t x_pos,                                  const uint32_t x_pos,
372                                  const uint32_t y_pos,                                  const uint32_t y_pos,
373                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
374                                  const uint32_t cbp,                                  const uint32_t cbp,
375                                  Bitstream * bs,                                  Bitstream * bs,
376                                  const uint32_t quant,                                  const uint32_t quant,
377                                  const uint32_t rounding)                                  const uint32_t rounding,
378                                    const int reduced_resolution)
379  {  {
380    
381          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 332  Line 383 
383    
384          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
385          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
386          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
387          uint32_t i;          uint32_t i;
388          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
389          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
390    
391          int uv_dx, uv_dy;          int uv_dx, uv_dy;
392            VECTOR mv[4];   /* local copy of mvs */
393    
394            if (reduced_resolution) {
395                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
396                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
397                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
398                    for (i = 0; i < 4; i++) {
399                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
400                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
401                    }
402            } else {
403          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
404          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
405          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
406                    for (i = 0; i < 4; i++)
407                            mv[i] = pMB->mvs[i];
408            }
409    
410          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
411    
412                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
413                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         } else {  
                 int sum;  
414    
415                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
416                  uv_dx =                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
417                          (sum ==  
418                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                  start_timer();
419                                                                    (ABS(sum) / 16) * 2));                  if (reduced_resolution)
420                    {
421                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
422                                                                      mv[0].x, mv[0].y, stride,  rounding);
423                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
424                                                                      uv_dx, uv_dy, stride2, rounding);
425                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
426                                                                      uv_dx, uv_dy, stride2, rounding);
427    
428                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  }
429                  uv_dy =                  else
430                          (sum ==                  {
431                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          if(dec->quarterpel) {
432                                                                    (ABS(sum) / 16) * 2));                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
433                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
434                                                                                            mv[0].x, mv[0].y, stride, rounding);
435                            }
436                            else {
437                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
438                                                                              mv[0].x, mv[0].y, stride, rounding);
439          }          }
440    
441                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
442                                                                      uv_dx, uv_dy, stride2, rounding);
443                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
444                                                                      uv_dx, uv_dy, stride2, rounding);
445                    }
446                    stop_comp_timer();
447    
448            } else {        /* MODE_INTER4V */
449                    int sum;
450    
451                    if(dec->quarterpel)
452                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
453                    else
454                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
455    
456                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
457    
458                    if(dec->quarterpel)
459                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
460                    else
461                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
462    
463                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
464    
465          start_timer();          start_timer();
466                    if (reduced_resolution)
467                    {
468                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
469                                                                      mv[0].x, mv[0].y, stride,  rounding);
470                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
471                                                                      mv[1].x, mv[1].y, stride,  rounding);
472                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
473                                                                      mv[2].x, mv[2].y, stride,  rounding);
474                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
475                                                                      mv[3].x, mv[3].y, stride,  rounding);
476                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
477                                                                      uv_dx, uv_dy, stride2, rounding);
478                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
479                                                                      uv_dx, uv_dy, stride2, rounding);
480    
481                            /* set_block(pY_Cur, stride, 32, 32, 127); */
482                    }
483                    else
484                    {
485                            if(dec->quarterpel) {
486                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
487                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
488                                                                                      mv[0].x, mv[0].y, stride,  rounding);
489                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
490                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
491                                                                                      mv[1].x, mv[1].y, stride,  rounding);
492                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
493                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
494                                                                                      mv[2].x, mv[2].y, stride,  rounding);
495                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
496                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
497                                                                                      mv[3].x, mv[3].y, stride,  rounding);
498                            }
499                            else {
500          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,
501                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
502          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,
503                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                            mv[1].x, mv[1].y, stride,  rounding);
504                                                    rounding);                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
505          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                            mv[2].x, mv[2].y, stride,  rounding);
506                                                    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,
507                                                    rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
508          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          }
509                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  
                                                   rounding);  
510          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
511                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
512          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
513                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
514                    }
515          stop_comp_timer();          stop_comp_timer();
516            }
517    
518          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
519                  if (cbp & (1 << (5 - i)))       // coded                  int direction = dec->alternate_vertical_scan ? 2 : 0;
520    
521                    if (cbp & (1 << (5 - i)))       /* coded */
522                  {                  {
523                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
524    
525                          start_timer();                          start_timer();
526                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
527                          stop_coding_timer();                          stop_coding_timer();
528    
529                          start_timer();                          start_timer();
# Line 411  Line 546 
546          }          }
547    
548          start_timer();          start_timer();
549            if (reduced_resolution)
550            {
551                    if (cbp & 32)
552                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
553                    if (cbp & 16)
554                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
555                    if (cbp & 8)
556                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
557                    if (cbp & 4)
558                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
559                    if (cbp & 2)
560                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
561                    if (cbp & 1)
562                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
563            }
564            else
565            {
566                    if (cbp & 32)
567                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
568                    if (cbp & 16)
569                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
570                    if (cbp & 8)
571                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
572                    if (cbp & 4)
573                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
574                    if (cbp & 2)
575                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
576                    if (cbp & 1)
577                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
578            }
579            stop_transfer_timer();
580    }
581    
582    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
583    {
584            int length = 1 << (fcode+4);
585    
586    /*      if (quarterpel) value *= 2; */
587    
588            if (value < -length)
589                    return -length;
590            else if (value >= length)
591                    return length-1;
592            else return value;
593    }
594    
595    
596    static void
597    decoder_mbgmc(DECODER * dec,
598                                    MACROBLOCK * const pMB,
599                                    const uint32_t x_pos,
600                                    const uint32_t y_pos,
601                                    const uint32_t fcode,
602                                    const uint32_t cbp,
603                                    Bitstream * bs,
604                                    const uint32_t quant,
605                                    const uint32_t rounding,
606                                    const int reduced_resolution)   /* no reduced res support */
607    {
608    
609            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
610            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
611    
612            const uint32_t stride = dec->edged_width;
613            const uint32_t stride2 = stride / 2;
614            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
615            uint32_t i;
616            const uint32_t iQuant = pMB->quant;
617            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
618            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
619            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
620    
621            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
622    
623            start_timer();
624    
625    /* this is where the calculations are done */
626    
627            {
628                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
629                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680          if (cbp & 32)          if (cbp & 32)
681                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682          if (cbp & 16)          if (cbp & 16)
# Line 430  Line 696 
696  void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
698                             Bitstream * bs,                             Bitstream * bs,
699                               int reduced_resolution,
700                             int quant,                             int quant,
701                             int intra_dc_threshold)                             int intra_dc_threshold)
702  {  {
703          uint32_t bound;          uint32_t bound;
704          uint32_t x, y;          uint32_t x, y;
705            uint32_t mb_width = dec->mb_width;
706            uint32_t mb_height = dec->mb_height;
707    
708            if (reduced_resolution)
709            {
710                    mb_width = (dec->width + 31) / 32;
711                    mb_height = (dec->height + 31) / 32;
712            }
713    
714          bound = 0;          bound = 0;
715    
716          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
717                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
718                          MACROBLOCK *mb;                          MACROBLOCK *mb;
719                          uint32_t mcbpc;                          uint32_t mcbpc;
720                          uint32_t cbpc;                          uint32_t cbpc;
# Line 452  Line 727 
727    
728                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
729                          {                          {
730                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
731                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
732                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
733                                    y = bound / mb_width;
734                          }                          }
735                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
736    
# Line 478  Line 754 
754                                  }                                  }
755                          }                          }
756                          mb->quant = quant;                          mb->quant = quant;
757                            mb->mvs[0].x = mb->mvs[0].y =
758                            mb->mvs[1].x = mb->mvs[1].y =
759                            mb->mvs[2].x = mb->mvs[2].y =
760                            mb->mvs[3].x = mb->mvs[3].y =0;
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
768                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
769    
770                  }                  }
771                    if(dec->out_frm)
772                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
773          }          }
774    
775  }  }
# Line 498  Line 781 
781                                    int x,                                    int x,
782                                    int y,                                    int y,
783                                    int k,                                    int k,
784                                    VECTOR * mv,                                    VECTOR * ret_mv,
785                                    int fcode,                                    int fcode,
786                                    const int bound)                                    const int bound)
787  {  {
# Line 509  Line 792 
792          int range = (64 * scale_fac);          int range = (64 * scale_fac);
793    
794          VECTOR pmv;          VECTOR pmv;
795          int mv_x, mv_y;          VECTOR mv;
796    
797          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
798    
799          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
803    
804          mv_x += pmv.x;          mv.x += pmv.x;
805          mv_y += pmv.y;          mv.y += pmv.y;
806    
807          if (mv_x < low) {          if (mv.x < low) {
808                  mv_x += range;                  mv.x += range;
809          } else if (mv_x > high) {          } else if (mv.x > high) {
810                  mv_x -= range;                  mv.x -= range;
811          }          }
812    
813          if (mv_y < low) {          if (mv.y < low) {
814                  mv_y += range;                  mv.y += range;
815          } else if (mv_y > high) {          } else if (mv.y > high) {
816                  mv_y -= range;                  mv.y -= range;
817          }          }
818    
819          mv->x = mv_x;          ret_mv->x = mv.x;
820          mv->y = mv_y;          ret_mv->y = mv.y;
   
821  }  }
822    
823    
824    
825    
826    
827    /* for P_VOP set gmc_warp to NULL */
828  void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                             Bitstream * bs,                             Bitstream * bs,
831                             int rounding,                             int rounding,
832                               int reduced_resolution,
833                             int quant,                             int quant,
834                             int fcode,                             int fcode,
835                             int intra_dc_threshold)                             int intra_dc_threshold,
836                               const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840          uint32_t bound;          uint32_t bound;
841            int cp_mb, st_mb;
842            uint32_t mb_width = dec->mb_width;
843            uint32_t mb_height = dec->mb_height;
844    
845            if (reduced_resolution)
846            {
847                    mb_width = (dec->width + 31) / 32;
848                    mb_height = (dec->height + 31) / 32;
849            }
850    
851          start_timer();          start_timer();
852          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
853                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856            if (gmc_warp)
857            {
858    
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
861                    {
862                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
863                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
864                                    dec->sprite_warping_points);
865                    }
866    
867                    generate_GMCparameters( dec->sprite_warping_points,
868                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
869                                    dec->width, dec->height, &dec->gmc_data);
870    
871    /* image warping is done block-based  in decoder_mbgmc(), now */
872    /*
873            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
874                                            mb_width, mb_height,
875                                            dec->edged_width, dec->edged_width/2,
876                                            fcode, dec->quarterpel, 0,
877                                            rounding, dec->mbs, &dec->gmc);
878    */
879            }
880    
881          bound = 0;          bound = 0;
882    
883          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
884                  for (x = 0; x < dec->mb_width; x++) {                  cp_mb = st_mb = 0;
885                    for (x = 0; x < mb_width; x++) {
886                          MACROBLOCK *mb;                          MACROBLOCK *mb;
887    
888                          // skip stuffing                          /* skip stuffing */
889                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
890                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
891    
892                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
893                          {                          {
894                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
895                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
896                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
897                                    y = bound / mb_width;
898                          }                          }
899                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
900    
901                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
902    
903                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
904                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
905                          {                          {
906                                  uint32_t mcbpc;                                  uint32_t mcbpc;
907                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 585  Line 909 
909                                  uint32_t cbpy;                                  uint32_t cbpy;
910                                  uint32_t cbp;                                  uint32_t cbp;
911                                  uint32_t intra;                                  uint32_t intra;
912                                    int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
913    
914                                    cp_mb++;
915                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
916                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
917                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
# Line 600  Line 926 
926                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
927                                  }                                  }
928    
929                                    if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
930                                    {
931                                            mcsel = BitstreamGetBit(bs);
932                                    }
933    
934                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
935                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
936    
937                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
938    
# Line 619  Line 950 
950                                  mb->quant = quant;                                  mb->quant = quant;
951    
952                                  if (dec->interlacing) {                                  if (dec->interlacing) {
953                                            if (cbp || intra) {
954                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
955                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
956                                            }
957    
958                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
959                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
960                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
961    
962                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
963                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
964                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
965                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
966                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
967                                                  }                                                  }
968                                          }                                          }
969                                  }                                  }
970    
971                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
972                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
973                                                                    rounding, reduced_resolution);
974                                            continue;
975    
976                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
977    
978                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
979                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
980                                                                                    fcode, bound);                                                                                    fcode, bound);
# Line 644  Line 983 
983                                          } else {                                          } else {
984                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
985                                                                                    fcode, bound);                                                                                    fcode, bound);
986                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
987                                          }                                          }
988                                  } else if (mb->mode ==                                  } else if (mb->mode == MODE_INTER4V ) {
989                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {  
990                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
991                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
992                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
993                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
994                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
995                                  {                                  {
996                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
997                                                  0;                                                  0;
998                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
999                                                  0;                                                  0;
1000                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1001                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
1005                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1006                                                                  rounding);                                                                  rounding, reduced_resolution);
1007                          } else                          // not coded  
1008                            }
1009                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1010                            {
1011                                    mb->mode = MODE_NOT_CODED_GMC;
1012    
1013                                    start_timer();
1014    
1015                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1016                                                                    rounding, reduced_resolution);
1017    
1018                                    stop_transfer_timer();
1019    
1020                                    if(dec->out_frm && cp_mb > 0) {
1021                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1022                                      cp_mb = 0;
1023                                    }
1024                                    st_mb = x+1;
1025                            }
1026                            else    /* not coded P_VOP macroblock */
1027                          {                          {
                                 //DEBUG2("P-frame MB at (X,Y)=",x,y);  
1028                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
1029    
1030                                  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;
1031                                  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;
1032                                    /* copy macroblock directly from ref to cur */
                                 // copy macroblock directly from ref to cur  
1033    
1034                                  start_timer();                                  start_timer();
1035    
1036                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  if (reduced_resolution)
1037                                                                   (16 * x),                                  {
1038                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1039                                                                   (16 * x), dec->edged_width);                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1040                                                                             dec->edged_width);
1041                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
1042                                                                   (16 * x + 8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1043                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
                                                                  (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),  
1044                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
1045    
1046                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1047                                                                   (8 * x),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
1048                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
1049                                    }
1050                                    else
1051                                    {
1052                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1053                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1054                                                                             dec->edged_width);
1055    
1056                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1057                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1058                                                                            dec->edged_width/2);
1059    
1060                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1061                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1062                                                                             dec->edged_width/2);
1063                                    }
1064    
1065                                  stop_transfer_timer();                                  stop_transfer_timer();
1066    
1067                                    if(dec->out_frm && cp_mb > 0) {
1068                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1069                                      cp_mb = 0;
1070                                    }
1071                                    st_mb = x+1;
1072                          }                          }
1073                  }                  }
1074                    if(dec->out_frm && cp_mb > 0)
1075                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1076          }          }
1077  }  }
1078    
1079    
1080  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
 // decode B-frame motion vector  
1081  void  void
1082  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
1083                                          Bitstream * bs,                                          Bitstream * bs,
# Line 765  Line 1121 
1121  }  }
1122    
1123    
1124  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
 // decode an B-frame forward & backward inter macroblock  
1125  void  void
1126  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1127                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 793  Line 1148 
1148          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1149          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1150    
1151    
1152          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1153                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1154                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1155    
1156                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1157                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1158                            uv_dx /= 2;
1159                            uv_dy /= 2;
1160                    }
1161    
1162                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1163                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1164          } else {          } else {
1165                  int sum;                  int sum;
1166    
1167                    if(dec->quarterpel)
1168                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1169                    else
1170                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1171    
1172                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1173    
1174                    if(dec->quarterpel)
1175                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1176                    else
1177                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1178                  uv_dy =  
1179                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1180          }          }
1181    
1182          start_timer();          start_timer();
1183            if(dec->quarterpel) {
1184                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1185                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1186                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1187            }
1188            else {
1189          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
1190                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1191          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1192                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1193          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1194                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1195                                                    0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1196          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1197                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
1198                                                    0);  
1199          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1200                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
1201          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
# Line 833  Line 1203 
1203          stop_comp_timer();          stop_comp_timer();
1204    
1205          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1206                  if (cbp & (1 << (5 - i)))       // coded                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1207    
1208                    if (cbp & (1 << (5 - i)))       /* coded */
1209                  {                  {
1210                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1211    
1212                          start_timer();                          start_timer();
1213                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1214                          stop_coding_timer();                          stop_coding_timer();
1215    
1216                          start_timer();                          start_timer();
# Line 876  Line 1248 
1248          stop_transfer_timer();          stop_transfer_timer();
1249  }  }
1250    
1251    /* decode an B-frame direct &  inter macroblock */
 // add by MinChen <chenm001@163.com>  
 // decode an B-frame direct &  inter macroblock  
1252  void  void
1253  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1254                                                             IMAGE forward,                                                             IMAGE forward,
# Line 886  Line 1256 
1256                                                             const MACROBLOCK * pMB,                                                             const MACROBLOCK * pMB,
1257                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
1258                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
                                                            const uint32_t cbp,  
1259                                                             Bitstream * bs)                                                             Bitstream * bs)
1260  {  {
1261    
# Line 901  Line 1270 
1270          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
1271          uint32_t i;          uint32_t i;
1272          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1273        const uint32_t cbp = pMB->cbp;
1274    
1275          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1276          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1277          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1278    
1279    
1280          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1281                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1282                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1283    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1284                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1285                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1286    
1287                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1288                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1289                            uv_dx /= 2;
1290                            uv_dy /= 2;
1291    
1292                            b_uv_dx /= 2;
1293                            b_uv_dy /= 2;
1294                    }
1295    
1296                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1297                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1298    
1299                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1300                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1301          } else {          } else {
1302                  int sum;                  int sum;
1303    
1304                    if(dec->quarterpel)
1305                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1306                    else
1307                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1308    
1309                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1310    
1311                    if(dec->quarterpel)
1312                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1313                    else
1314                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1315                  uv_dy =  
1316                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1317                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1318                                                                    (ABS(sum) / 16) * 2));  
1319                    if(dec->quarterpel)
1320                  sum =                          sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1321                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +                  else
1322                          pMB->b_mvs[3].x;                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1323                  b_uv_dx =  
1324                          (sum ==                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1325                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1326                                                                    (ABS(sum) / 16) * 2));                  if(dec->quarterpel)
1327                            sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1328                  sum =                  else
1329                          pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1330                          pMB->b_mvs[3].y;  
1331                  b_uv_dy =                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1332          }          }
1333    
1334    
1335          start_timer();          start_timer();
1336            if(dec->quarterpel) {
1337                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1338                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1339                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1340                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1341                    else {
1342                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1343                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1344                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1345                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1346                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1347                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1348                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1349                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1350                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1351                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1352                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1353                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1354                    }
1355            }
1356            else {
1357          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,
1358                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1359          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 961  Line 1363 
1363          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1364                                                    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,
1365                                                    0);                                                    0);
1366            }
1367    
1368          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,
1369                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1370          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,
1371                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1372    
1373    
1374          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1375                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1376                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1377                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1378                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1379                    else {
1380                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1381                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1382                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1383          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,
1384                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1385                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1386                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1387                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1388                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1389                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1390                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1391                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1392                    }
1393            }
1394            else {
1395                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1396                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1397                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1398                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1399                                                    0);                                                    0);
1400          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1401                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1402                                                    stride, 0);                                                    stride, 0);
1403          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1404                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1405                                                    stride, 0);                                                    stride, 0);
1406          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          }
1407    
1408            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1409                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1410          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,
1411                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1412    
1413          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,
1414                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1415          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,
1416                                           stride);                                                  stride, 1, 8);
1417          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1418                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1419          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1420                                           16 * y_pos + 8, stride);                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1421          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1422                                           stride2);  
1423          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,
1424                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1425                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1426                                                    stride, 1, 8);
1427    
1428            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1429                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1430                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1431                                                    stride, 1, 8);
1432    
1433            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1434                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1435                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1436                                                    stride2, 1, 8);
1437    
1438            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1439                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1440                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1441                                                    stride2, 1, 8);
1442    
1443          stop_comp_timer();          stop_comp_timer();
1444    
1445          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1446                  if (cbp & (1 << (5 - i)))       // coded                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1447    
1448                    if (cbp & (1 << (5 - i)))       /* coded */
1449                  {                  {
1450                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1451    
1452                          start_timer();                          start_timer();
1453                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1454                          stop_coding_timer();                          stop_coding_timer();
1455    
1456                          start_timer();                          start_timer();
# Line 1043  Line 1489 
1489  }  }
1490    
1491    
1492  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
 // for decode B-frame dbquant  
1493  int32_t __inline  int32_t __inline
1494  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1495  {  {
1496          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1497                  return (0);                  return (0);
1498          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1499                  return (-2);                  return (-2);
1500          else          else                           /* '11' */
1501                  return (2);                             // '11'                  return (2);
1502  }  }
1503    
1504  // add by MinChen <chenm001@163.com>  /*
1505  // for decode B-frame mb_type   * For decode B-frame mb_type
1506  // bit   ret_value   * bit   ret_value
1507  // 1        0   * 1        0
1508  // 01       1   * 01       1
1509  // 001      2   * 001      2
1510  // 0001     3   * 0001     3
1511     */
1512  int32_t __inline  int32_t __inline
1513  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1514  {  {
# Line 1086  Line 1532 
1532                             int fcode_forward,                             int fcode_forward,
1533                             int fcode_backward)                             int fcode_backward)
1534  {  {
   
1535          uint32_t x, y;          uint32_t x, y;
1536          VECTOR mv, zeromv;          VECTOR mv;
1537            const VECTOR zeromv = {0,0};
1538  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1539          FILE *fp;          FILE *fp;
1540          static char first=0;          static char first=0;
# Line 1099  Line 1545 
1545    
1546          start_timer();          start_timer();
1547          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1548                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1549          //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,
1550                                       dec->width, dec->height);
1551          stop_edges_timer();          stop_edges_timer();
1552    
1553  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
# Line 1110  Line 1557 
1557  #endif  #endif
1558    
1559          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1560                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1561                  dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;                  dec->p_fmv = dec->p_bmv = zeromv;
1562                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1563                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1564                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1565    
1566                          mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = 0;                          mv =
1567                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1568                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1569    
1570                            /*
1571                             * skip if the co-located P_VOP macroblock is not coded
1572                             * if not codec in co-located S_VOP macroblock is _not_
1573                             * automatically skipped
1574                             */
1575    
                         // the last P_VOP is skip macroblock ?  
1576                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1577                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
                                 mb->mb_type = MODE_NOT_CODED;  
1578                                  mb->cbp = 0;                                  mb->cbp = 0;
1579  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1580                                    mb->mb_type = MODE_NOT_CODED;
1581          BFRAME_DEBUG          BFRAME_DEBUG
1582  #endif  #endif
1583                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1584                                    mb->quant = last_mb->quant;
1585                                    /*
1586                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1587                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1588                                  mb->quant = 8;                                  */
1589    
1590                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1591                                  continue;                                  continue;
1592                          }                          }
                         //t=BitstreamShowBits(bs,32);  
1593    
1594                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1595                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1596    
1597                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1598    
1599                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1600                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1601                                  } else {                                  } else {
1602                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1154  Line 1609 
1609                                          } else if (quant < 1) {                                          } else if (quant < 1) {
1610                                                  quant = 1;                                                  quant = 1;
1611                                          }                                          }
                                 } else {  
                                         quant = 8;  
1612                                  }                                  }
                                 mb->quant = quant;  
1613                          } else {                          } else {
1614                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mb_type = MODE_DIRECT_NONE_MV;
1615                                  mb->cbp = 0;                                  mb->cbp = 0;
1616                          }                          }
1617    
1618                          mb->mode = MODE_INTER;                          mb->quant = quant;
1619                          //DEBUG1("Switch bm_type=",mb->mb_type);                          mb->mode = MODE_INTER4V;
1620                            /* DEBUG1("Switch bm_type=",mb->mb_type); */
1621    
1622  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1623          BFRAME_DEBUG          BFRAME_DEBUG
1624  #endif  #endif
1625    
1626                          switch (mb->mb_type) {                          switch (mb->mb_type) {
1627                          case MODE_DIRECT:                          case MODE_DIRECT:
1628                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1629    
1630                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1631                                  {                               // Because this file is a C file not C++ so I use '{' to define var                                  {
1632                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD =                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
                                                 dec->time_pp;  
1633                                          int i;                                          int i;
1634    
1635                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1636                                                  mb->mvs[i].x =                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1637                                                          (int32_t) ((TRB * last_mb->mvs[i].x) / TRD +                                                                        / TRD + mv.x);
1638                                                                             mb->mvs[0].x);                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1639                                                  mb->b_mvs[i].x =                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)
1640                                                          (int32_t) ((mb->mvs[0].x ==                                                                                    / TRD
1641                                                                                  0) ? ((TRB -                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);
1642                                                                                             TRD) * last_mb->mvs[i].x) /                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1643                                                                             TRD : mb->mvs[i].x - last_mb->mvs[i].x);                                                                        / TRD + mv.y);
1644                                                  mb->mvs[i].y =                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1645                                                          (int32_t) ((TRB * last_mb->mvs[i].y) / TRD +                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)
1646                                                                             mb->mvs[0].y);                                                                                    / TRD
1647                                                  mb->b_mvs[i].y =                                                                              : mb->mvs[i].y - last_mb->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);  
1648                                          }                                          }
1649                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1650                                  }                                  }
                                 mb->mode = MODE_INTER4V;  
1651                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1652                                                                                             mb, x, y, mb->cbp, bs);                                                                                             mb, x, y, bs);
1653                                  break;                                  break;
1654    
1655                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1656                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1657                                                                          dec->p_fmv);                                                                          dec->p_fmv);
1658                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1659    
1660                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1661                                                                          fcode_backward, dec->p_bmv);                                                                          fcode_backward, dec->p_bmv);
1662                                  dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x =                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1663                                          mb->b_mvs[3].x = mb->b_mvs[0].x;                                          mb->b_mvs[3] = mb->b_mvs[0];
                                 dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y =  
                                         mb->b_mvs[3].y = mb->b_mvs[0].y;  
1664    
1665                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1666                                                                                             mb, x, y, mb->cbp, bs);                                                                                             mb, x, y, bs);
1667                                  //DEBUG("B-frame Bidir!\n");                                  /* DEBUG("B-frame Bidir!\n"); */
1668                                  break;                                  break;
1669    
1670                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1671                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1672                                                                          dec->p_bmv);                                                                          dec->p_bmv);
1673                                  dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1674    
1675                                    mb->mode = MODE_INTER;
1676                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1677                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1678                                  break;                                  break;
1679    
1680                          case MODE_FORWARD:                          case MODE_FORWARD:
1681                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1682                                                                          dec->p_fmv);                                                                          dec->p_fmv);
1683                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1684    
1685                                    mb->mode = MODE_INTER;
1686                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1687                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1688                                  break;                                  break;
1689    
1690                          default:                          default:
1691                                  //DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
                                 ;  
1692                          }                          }
1693                    } /* End of for */
                 }                                               // end of FOR  
1694          }          }
1695    
1696  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1697          if (!first){          if (!first){
1698                  first=1;                  first=1;
# Line 1264  Line 1702 
1702  #endif  #endif
1703  }  }
1704    
1705  // swap two MACROBLOCK array  /* swap two MACROBLOCK array */
1706  void  void
1707  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
1708                  MACROBLOCK ** mb2)                  MACROBLOCK ** mb2)
# Line 1275  Line 1713 
1713          *mb2 = temp;          *mb2 = temp;
1714  }  }
1715    
1716    
1717    /* perform post processing if necessary, and output the image */
1718    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1719                                            const XVID_DEC_FRAME * frame, int pp_disable)
1720    {
1721    
1722            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1723            {
1724                    /* note: image is stored to tmp */
1725                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1726                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1727                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1728                                                    8, frame->general);
1729                    img = &dec->tmp;
1730            }
1731    
1732            image_output(img, dec->width, dec->height,
1733                                     dec->edged_width, frame->image, frame->stride,
1734                                     frame->colorspace, dec->interlacing);
1735    }
1736    
1737    
1738  int  int
1739  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1740                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1741  {  {
1742    
1743          Bitstream bs;          Bitstream bs;
1744          uint32_t rounding;          uint32_t rounding;
1745            uint32_t reduced_resolution;
1746          uint32_t quant;          uint32_t quant;
1747          uint32_t fcode_forward;          uint32_t fcode_forward;
1748          uint32_t fcode_backward;          uint32_t fcode_backward;
1749          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1750          uint32_t vop_type;          WARPPOINTS gmc_warp;
1751            int vop_type;
1752            int success = 0;
1753            int output = 0;
1754            int seen_something = 0;
1755    
1756          start_global_timer();          start_global_timer();
1757    
1758          BitstreamInit(&bs, frame->bitstream, frame->length);          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1759            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1760    
1761          // add by chenm001 <chenm001@163.com>          if ((frame->general & XVID_DEC_DISCONTINUITY))
1762          // for support B-frame to reference last 2 frame                  dec->frames = 0;
         dec->frames++;  
         vop_type =  
                 BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,  
                                                          &fcode_backward, &intra_dc_threshold);  
1763    
1764          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          if (frame->length < 0)  /* decoder flush */
1765            {
1766                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1767                        we have a reference frame, then outout the reference frame */
1768                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1769                    {
1770                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1771                            output = 1;
1772                    }
1773    
1774          switch (vop_type) {                  frame->length = 0;
1775          case P_VOP:                  if (stats)
1776                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,                  {
1777                                             intra_dc_threshold);                          stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1778  #ifdef BFRAMES_DEC                          stats->data.vop.time_base = (int)dec->time_base;
1779                  DEBUG1("P_VOP  Time=", dec->time);                          stats->data.vop.time_increment = 0;     /* XXX: todo */
1780  #endif                  }
                 break;  
1781    
1782          case I_VOP:                  emms();
                 decoder_iframe(dec, &bs, quant, intra_dc_threshold);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
                 break;  
1783    
1784          case B_VOP:                  stop_global_timer();
1785  #ifdef BFRAMES_DEC                  return XVID_ERR_OK;
                 if (dec->time_pp > dec->time_bp) {  
                         DEBUG1("B_VOP  Time=", dec->time);  
                         decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);  
                 } else {  
                         DEBUG("broken B-frame!");  
1786                  }                  }
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
                 break;  
1787    
1788          case N_VOP:                             // vop not coded          BitstreamInit(&bs, frame->bitstream, frame->length);
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
                 break;  
1789    
1790          default:          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1791                  return XVID_ERR_FAIL;          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1792            {
1793                    if (stats)
1794                            stats->notify = XVID_DEC_VOP;
1795                    frame->length = 1;
1796                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1797                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1798                    emms();
1799                    return XVID_ERR_OK;
1800          }          }
1801    
1802  #ifdef BFRAMES_DEC_DEBUG  repeat:
1803          if (frame->length != BitstreamPos(&bs) / 8){  
1804                  DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1805                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1806    
1807            DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1808                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1809    
1810            if (vop_type == -1)
1811            {
1812                    if (success) goto done;
1813                    emms();
1814                    return XVID_ERR_FAIL;
1815          }          }
1816  #endif  
1817            if (vop_type == -2 || vop_type == -3)
1818            {
1819                    if (vop_type == -3)
1820                            decoder_resize(dec);
1821    
1822                    if (stats)
1823                    {
1824                            stats->notify = XVID_DEC_VOL;
1825                            stats->data.vol.general = 0;
1826                            if (dec->interlacing)
1827                                    stats->data.vol.general |= XVID_INTERLACING;
1828                            stats->data.vol.width = dec->width;
1829                            stats->data.vol.height = dec->height;
1830                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1831                            stats->data.vol.par_width = dec->par_width;
1832                            stats->data.vol.par_height = dec->par_height;
1833          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1834                            emms();
1835                            return XVID_ERR_OK;
1836                    }
1837                    goto repeat;
1838            }
1839    
1840            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1841    
 #ifdef BFRAMES_DEC  
         // test if no B_VOP  
         if (dec->low_delay) {  
 #endif  
         image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
                                          frame->image, frame->stride, frame->colorspace);  
1842    
1843  #ifdef BFRAMES_DEC          /* packed_mode: special-N_VOP treament */
1844          } else {          if (dec->packed_mode && vop_type == N_VOP)
1845                  if (dec->frames >= 0) {          {
1846                          start_timer();                  if (dec->low_delay_default && dec->frames > 0)
1847                          if ((vop_type == I_VOP || vop_type == P_VOP)) {                  {
1848                                  image_output(&dec->refn[0], dec->width, dec->height,                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1849                                                           dec->edged_width, frame->image, frame->stride,                          output = 1;
                                                          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);  
1850                          }                          }
1851                          stop_conv_timer();                  /* ignore otherwise */
1852            }
1853            else if (vop_type != B_VOP)
1854            {
1855                    switch(vop_type)
1856                    {
1857                    case I_VOP :
1858                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1859                            break;
1860                    case P_VOP :
1861                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1862                                                    fcode_forward, intra_dc_threshold, NULL);
1863                            break;
1864                    case S_VOP :
1865                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1866                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1867                            break;
1868                    case N_VOP :
1869                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1870                            break;
1871                    }
1872    
1873                    if (reduced_resolution)
1874                    {
1875                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1876                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1877                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1878                    }
1879    
1880                    /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1881                    if (!(dec->low_delay_default && dec->packed_mode))
1882                    {
1883                            if (dec->low_delay)
1884                            {
1885                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1886                                    output = 1;
1887                            }
1888                            else if (dec->frames > 0)       /* is the reference frame valid? */
1889                            {
1890                                    /* output the reference frame */
1891                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1892                                    output = 1;
1893                  }                  }
1894          }          }
 #endif  
1895    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1896                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1897                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
                 // swap MACROBLOCK  
                 if (!dec->low_delay && vop_type == P_VOP)  
1898                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1899                    dec->last_reduced_resolution = reduced_resolution;
1900    
1901                    dec->frames++;
1902                    seen_something = 1;
1903    
1904            }else{  /* B_VOP */
1905    
1906                    if (dec->low_delay)
1907                    {
1908                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1909                            dec->low_delay = 1;
1910                    }
1911    
1912                    if (dec->frames < 2)
1913                    {
1914                            /* attemping to decode a bvop without atleast 2 reference frames */
1915                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1916                                                    "broken b-frame, mising ref frames");
1917                    }else if (dec->time_pp <= dec->time_bp) {
1918                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1919                            decoded in vfw. */
1920                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1921                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1922                    }else{
1923                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1924                    }
1925    
1926                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1927                    output = 1;
1928                    dec->frames++;
1929            }
1930    
1931            BitstreamByteAlign(&bs);
1932    
1933            /* low_delay_default mode: repeat in packed_mode */
1934            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1935            {
1936                    success = 1;
1937                    goto repeat;
1938            }
1939    
1940    done :
1941    
1942            /* low_delay_default mode: if we've gotten here without outputting anything,
1943               then output the recently decoded frame, or print an error message  */
1944            if (dec->low_delay_default && output == 0)
1945            {
1946                    if (dec->packed_mode && seen_something)
1947                    {
1948                            /* output the recently decoded frame */
1949                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1950                            output = 1;
1951                    }
1952                    else
1953                    {
1954                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1955                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1956                                    "warning: nothing to output");
1957                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1958                                    "bframe decoder lag");
1959    
1960                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1961                    }
1962            }
1963    
1964            frame->length = BitstreamPos(&bs) / 8;
1965    
1966            if (stats)
1967            {
1968                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1969                    stats->data.vop.time_base = (int)dec->time_base;
1970                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1971          }          }
1972    
1973          emms();          emms();

Legend:
Removed from v.1.28  
changed lines
  Added in v.1.49

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