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

Legend:
Removed from v.1.27  
changed lines
  Added in v.1.49.4.1

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