[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.37.2.18, Tue Dec 10 11:13:50 2002 UTC revision 1.49.2.8, Mon Jun 9 19:41:53 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2003 Peter Ross <pross@xvid.org>
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  15.07.2002  fix a bug in B-frame decode at DIRECT mode  
  *              MinChen <chenm001@163.com>  
  *  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>  
  *  
23   *  $Id$   *  $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  #include <string.h>
30    
# Line 68  Line 34 
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 80  Line 47 
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
49  #include "image/reduced.h"  #include "image/reduced.h"
50    #include "image/font.h"
51    
52  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
53  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 95  Line 63 
63  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
64  {  {
65          /* free existing */          /* 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);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
72            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
73    
74          if (dec->last_mbs)          if (dec->last_mbs)
75                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
# Line 108  Line 77 
77                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
78    
79          /* realloc */          /* 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    
# Line 126  Line 94 
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96    
97          // add by chenm001 <chenm001@163.com>          /* Support B-frame to reference last 2 frame */
         // for 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);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
114                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
115                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  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);                  xvid_free(dec);
118                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
119          }          }
120    
121          if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {          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->refn[2], dec->edged_width, dec->edged_height);                  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 158  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->refh, dec->edged_width, dec->edged_height);                  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 175  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->refh, dec->edged_width, dec->edged_height);                  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;          return 0;
163  }  }
164    
165    
166  int  int
167  decoder_create(XVID_DEC_PARAM * param)  decoder_create(xvid_dec_create_t * create)
168  {  {
169          DECODER *dec;          DECODER *dec;
170    
171            if (XVID_MAJOR(create->version) != 1)   /* v1.x.x */
172                    return XVID_ERR_VERSION;
173    
174          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
175          if (dec == NULL) {          if (dec == NULL) {
176                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
177          }          }
178          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
179    
180          param->handle = dec;          create->handle = dec;
181    
182          dec->width = param->width;          dec->width = create->width;
183          dec->height = param->height;          dec->height = create->height;
184    
185          image_null(&dec->cur);          image_null(&dec->cur);
186          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
187          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
188          image_null(&dec->refn[2]);          image_null(&dec->tmp);
189          image_null(&dec->refh);          image_null(&dec->qtmp);
190    
191            /* image based GMC */
192            image_null(&dec->gmc);
193    
194    
195          dec->mbs = NULL;          dec->mbs = NULL;
196          dec->last_mbs = NULL;          dec->last_mbs = NULL;
197    
198          init_timer();          init_timer();
199    
200          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
201          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
202          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
203          dec->low_delay = 0;          dec->low_delay = 0;
204          dec->packed_mode = 0;          dec->packed_mode = 0;
# Line 226  Line 208 
208          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
209                  return decoder_resize(dec);                  return decoder_resize(dec);
210          else          else
211                  return XVID_ERR_OK;                  return 0;
212  }  }
213    
214    
# Line 235  Line 217 
217  {  {
218          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
219          xvid_free(dec->mbs);          xvid_free(dec->mbs);
220    
221            /* image based GMC */
222            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
223    
224          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
225          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
226          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
227          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
228          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
229          xvid_free(dec);          xvid_free(dec);
230    
231          write_timer();          write_timer();
232          return XVID_ERR_OK;          return 0;
233  }  }
234    
235    
# Line 255  Line 241 
241    
242    
243    
244  // decode an intra macroblock  /* decode an intra macroblock */
   
245  void  void
246  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
247                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 291  Line 276 
276                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
277          }          }
278    
279          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
280    
281          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
282                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 314  Line 299 
299                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
300    
301                          if (dc_size > 8) {                          if (dc_size > 8) {
302                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
303                          }                          }
304    
305                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
306                          start_coeff = 1;                          start_coeff = 1;
307    
308                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
309                  } else {                  } else {
310                          start_coeff = 0;                          start_coeff = 0;
311                  }                  }
312    
313                  start_timer();                  start_timer();
314                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
315                  {                  {
316                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
317                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 383  Line 368 
368    
369    
370    
371    /* decode an inter macroblock */
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
 // decode an inter macroblock  
   
372  void  void
373  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
374                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
375                                  const uint32_t x_pos,                                  const uint32_t x_pos,
376                                  const uint32_t y_pos,                                  const uint32_t y_pos,
377                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
378                                  const uint32_t cbp,                                  const uint32_t cbp,
379                                  Bitstream * bs,                                  Bitstream * bs,
380                                  const uint32_t quant,                                  const uint32_t quant,
# Line 432  Line 412 
412          }          }
413    
414          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
415    
416                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
417                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
418    
419                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
420                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 458  Line 433 
433                  else                  else
434                  {                  {
435                          if(dec->quarterpel) {                          if(dec->quarterpel) {
436                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
437                                                                                          dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
438                                                                                          mv[0].x, mv[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
439                          }                          }
440                          else {                          else {
# Line 507  Line 482 
482                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
483                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
484    
485                          // set_block(pY_Cur, stride, 32, 32, 127);                          /* set_block(pY_Cur, stride, 32, 32, 127); */
486                  }                  }
487                  else                  else
488                  {                  {
489                          if(dec->quarterpel) {                          if(dec->quarterpel) {
490                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
493                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
494                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
495                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
496                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
497                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
498                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
499                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
500                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
501                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
502                          }                          }
503                          else {                          else {
# Line 547  Line 522 
522          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
523                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
524    
525                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
526                  {                  {
527                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
528    
529                          start_timer();                          start_timer();
530                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 608  Line 583 
583          stop_transfer_timer();          stop_transfer_timer();
584  }  }
585    
586    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
587    {
588            int length = 1 << (fcode+4);
589    
590    /*      if (quarterpel) value *= 2; */
591    
592            if (value < -length)
593                    return -length;
594            else if (value >= length)
595                    return length-1;
596            else return value;
597    }
598    
599    
600    static void
601    decoder_mbgmc(DECODER * dec,
602                                    MACROBLOCK * const pMB,
603                                    const uint32_t x_pos,
604                                    const uint32_t y_pos,
605                                    const uint32_t fcode,
606                                    const uint32_t cbp,
607                                    Bitstream * bs,
608                                    const uint32_t quant,
609                                    const uint32_t rounding,
610                                    const int reduced_resolution)   /* no reduced res support */
611    {
612    
613            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
614            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
615    
616            const uint32_t stride = dec->edged_width;
617            const uint32_t stride2 = stride / 2;
618            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
619            uint32_t i;
620            const uint32_t iQuant = pMB->quant;
621            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
622            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
623            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
624    
625            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
626    
627            start_timer();
628    
629    /* this is where the calculations are done */
630    
631            {
632                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
633                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
634    
635                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
636                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
637            }
638            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
639    
640    /*
641            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
642            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
643            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
644    */
645    
646    
647            stop_transfer_timer();
648    
649            if (!cbp) return;
650    
651            for (i = 0; i < 6; i++) {
652                    int direction = dec->alternate_vertical_scan ? 2 : 0;
653    
654                    if (cbp & (1 << (5 - i)))       /* coded */
655                    {
656                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
657    
658                            start_timer();
659                            get_inter_block(bs, &block[i * 64], direction);
660                            stop_coding_timer();
661    
662                            start_timer();
663                            if (dec->quant_type == 0) {
664                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
665                            } else {
666                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
667                            }
668                            stop_iquant_timer();
669    
670                            start_timer();
671                            idct(&data[i * 64]);
672                            stop_idct_timer();
673                    }
674            }
675    
676    /* interlace + GMC is this possible ??? */
677    /*
678      if (dec->interlacing && pMB->field_dct) {
679              next_block = stride;
680              stride *= 2;
681      }
682    */
683            start_timer();
684            if (cbp & 32)
685                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
686            if (cbp & 16)
687                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
688            if (cbp & 8)
689                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
690            if (cbp & 4)
691                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
692            if (cbp & 2)
693                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
694            if (cbp & 1)
695                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
696            stop_transfer_timer();
697    }
698    
699    
700  void  void
701  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
# Line 618  Line 706 
706  {  {
707          uint32_t bound;          uint32_t bound;
708          uint32_t x, y;          uint32_t x, y;
709          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
710          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
711    
712          if (reduced_resolution)          if (reduced_resolution)
713          {          {
# Line 650  Line 738 
738                          }                          }
739                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
740    
741                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
742    
743                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
744                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 677  Line 765 
765    
766                          if (dec->interlacing) {                          if (dec->interlacing) {
767                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
768                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
769                          }                          }
770    
771                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 715  Line 803 
803          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
804          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
805    
806          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
807    
808          mv.x += pmv.x;          mv.x += pmv.x;
809          mv.y += pmv.y;          mv.y += pmv.y;
# Line 738  Line 826 
826    
827    
828    
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
829    
         if (quarterpel) value *= 2;  
830    
831          if (value < -length)  /* for P_VOP set gmc_warp to NULL */
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
   
   
 /* for P_VOP set gmc_mv to NULL */  
832  void  void
833  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
834                             Bitstream * bs,                             Bitstream * bs,
# Line 761  Line 837 
837                             int quant,                             int quant,
838                             int fcode,                             int fcode,
839                             int intra_dc_threshold,                             int intra_dc_threshold,
840                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
841  {  {
842    
843          uint32_t x, y;          uint32_t x, y;
844          uint32_t bound;          uint32_t bound;
845          int cp_mb, st_mb;          int cp_mb, st_mb;
846          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
847          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
848    
849          if (reduced_resolution)          if (reduced_resolution)
850          {          {
# Line 781  Line 857 
857                                     dec->width, dec->height);                                     dec->width, dec->height);
858          stop_edges_timer();          stop_edges_timer();
859    
860            if (gmc_warp)
861            {
862    
863                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
864                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
865                    {
866                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
867                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
868                                    dec->sprite_warping_points);
869                    }
870    
871                    generate_GMCparameters( dec->sprite_warping_points,
872                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
873                                    dec->width, dec->height, &dec->gmc_data);
874    
875    /* image warping is done block-based  in decoder_mbgmc(), now */
876    /*
877            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
878                                            mb_width, mb_height,
879                                            dec->edged_width, dec->edged_width/2,
880                                            fcode, dec->quarterpel, 0,
881                                            rounding, dec->mbs, &dec->gmc);
882    */
883            }
884    
885          bound = 0;          bound = 0;
886    
887          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 788  Line 889 
889                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
890                          MACROBLOCK *mb;                          MACROBLOCK *mb;
891    
892                          // skip stuffing                          /* skip stuffing */
893                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
894                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
895    
# Line 801  Line 902 
902                          }                          }
903                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
904    
905                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
906    
907                          //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 */
908                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
909                          {                          {
910                                  uint32_t mcbpc;                                  uint32_t mcbpc;
911                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 812  Line 913 
913                                  uint32_t cbpy;                                  uint32_t cbpy;
914                                  uint32_t cbp;                                  uint32_t cbp;
915                                  uint32_t intra;                                  uint32_t intra;
916                                  int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
917    
918                                  cp_mb++;                                  cp_mb++;
919                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
920                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
921                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
922    
923                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
924                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
925                                  acpred_flag = 0;                                  acpred_flag = 0;
926    
927                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
# Line 829  Line 930 
930                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
931                                  }                                  }
932    
933                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
934                                  {                                  {
935                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
936                                  }                                  }
937    
938                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
939                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i  mcsel %i \n", cbpy,mcsel);
940    
941                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
942    
943                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
944                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
945                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
946                                          quant += dquant;                                          quant += dquant;
947                                          if (quant > 31) {                                          if (quant > 31) {
948                                                  quant = 31;                                                  quant = 31;
949                                          } else if (quant < 1) {                                          } else if (quant < 1) {
950                                                  quant = 1;                                                  quant = 1;
951                                          }                                          }
952                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
953                                  }                                  }
954                                  mb->quant = quant;                                  mb->quant = quant;
955    
956                                  if (dec->interlacing) {                                  if (dec->interlacing) {
957                                          if (cbp || intra) {                                          if (cbp || intra) {
958                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
959                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
960                                          }                                          }
961    
962                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
963                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
964                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
965    
966                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
967                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
968                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
969                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
970                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
971                                                  }                                                  }
972                                          }                                          }
973                                  }                                  }
974    
975                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
976                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
977                                                                    rounding, reduced_resolution);
978                                            continue;
979    
980                                          if (mcsel)                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
                                         {  
                                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
                                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);  
981    
982                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
983                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
984                                                                                    fcode, bound);                                                                                    fcode, bound);
985                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 886  Line 987 
987                                          } else {                                          } else {
988                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
989                                                                                    fcode, bound);                                                                                    fcode, bound);
990                                                  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;  
991                                          }                                          }
992                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
993    
# Line 897  Line 995 
995                                          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);
996                                          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);
997                                          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);
998                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
999                                  {                                  {
1000                                          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 =
1001                                                  0;                                                  0;
# Line 908  Line 1006 
1006                                          continue;                                          continue;
1007                                  }                                  }
1008    
1009                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1010                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
1011    
1012                          }                          }
1013                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1014                          {                          {
1015                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1016                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);  
1017                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);                                  start_timer();
1018                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1019                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1020                                                                    rounding, reduced_resolution);
1021    
1022                                    stop_transfer_timer();
1023    
1024                                    if(dec->out_frm && cp_mb > 0) {
1025                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1026                                      cp_mb = 0;
1027                                    }
1028                                    st_mb = x+1;
1029                          }                          }
1030                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1031                          {                          {
# Line 925  Line 1033 
1033    
1034                                  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;
1035                                  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;
1036                                  // copy macroblock directly from ref to cur                                  /* copy macroblock directly from ref to cur */
1037    
1038                                  start_timer();                                  start_timer();
1039    
# Line 973  Line 1081 
1081  }  }
1082    
1083    
1084  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
 // decode B-frame motion vector  
1085  void  void
1086  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
1087                                          Bitstream * bs,                                          Bitstream * bs,
# Line 1018  Line 1125 
1125  }  }
1126    
1127    
1128  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
 // decode an B-frame forward & backward inter macroblock  
1129  void  void
1130  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1131                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 1079  Line 1185 
1185    
1186          start_timer();          start_timer();
1187          if(dec->quarterpel) {          if(dec->quarterpel) {
1188                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->refh.y, dec->refh.y + 64,                  interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1189                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1190                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1191          }          }
1192          else {          else {
# Line 1103  Line 1209 
1209          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1210                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1211    
1212                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1213                  {                  {
1214                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1215    
1216                          start_timer();                          start_timer();
1217                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1146  Line 1252 
1252          stop_transfer_timer();          stop_transfer_timer();
1253  }  }
1254    
1255  // add by MinChen <chenm001@163.com>  /* decode an B-frame direct &  inter macroblock */
 // decode an B-frame direct &  inter macroblock  
1256  void  void
1257  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1258                                                             IMAGE forward,                                                             IMAGE forward,
# Line 1234  Line 1339 
1339          start_timer();          start_timer();
1340          if(dec->quarterpel) {          if(dec->quarterpel) {
1341                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1342                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1343                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1344                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1345                  else {                  else {
1346                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1347                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1348                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1349                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1350                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1351                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1352                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1353                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1354                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1355                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1356                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1357                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1358                  }                  }
1359          }          }
# Line 1272  Line 1377 
1377    
1378          if(dec->quarterpel) {          if(dec->quarterpel) {
1379                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1380                          interpolate16x16_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1381                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1382                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1383                  else {                  else {
1384                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1385                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1386                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1387                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1388                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1389                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1390                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1391                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1392                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1393                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1394                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1395                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1396                  }                  }
1397          }          }
1398          else {          else {
1399                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1400                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1401                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1402                                                            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,
1403                                                            0);                                                            0);
1404                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1405                                                            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,
1406                                                            stride, 0);                                                            stride, 0);
1407                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1408                                                            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,
1409                                                            stride, 0);                                                            stride, 0);
1410          }          }
1411    
1412          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1413                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1414          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,
1415                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1416    
1417          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1418                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1419                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1420                                                  stride, 1, 8);                                                  stride, 1, 8);
1421    
1422          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1423                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1424                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1425                                                  stride, 1, 8);                                                  stride, 1, 8);
1426    
1427          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1428                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1429                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1430                                                  stride, 1, 8);                                                  stride, 1, 8);
1431    
1432          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1433                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1434                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1435                                                  stride, 1, 8);                                                  stride, 1, 8);
1436    
1437          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1438                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1439                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1440                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1441    
1442          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1443                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1444                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1445                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1446    
1447          stop_comp_timer();          stop_comp_timer();
# Line 1344  Line 1449 
1449          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1450                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1451    
1452                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1453                  {                  {
1454                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1455    
1456                          start_timer();                          start_timer();
1457                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1388  Line 1493 
1493  }  }
1494    
1495    
1496  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
 // for decode B-frame dbquant  
1497  int32_t __inline  int32_t __inline
1498  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1499  {  {
1500          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1501                  return (0);                  return (0);
1502          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1503                  return (-2);                  return (-2);
1504          else          else                           /* '11' */
1505                  return (2);                             // '11'                  return (2);
1506  }  }
1507    
1508  // add by MinChen <chenm001@163.com>  /*
1509  // for decode B-frame mb_type   * For decode B-frame mb_type
1510  // bit   ret_value   * bit   ret_value
1511  // 1        0   * 1        0
1512  // 01       1   * 01       1
1513  // 001      2   * 001      2
1514  // 0001     3   * 0001     3
1515     */
1516  int32_t __inline  int32_t __inline
1517  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1518  {  {
# Line 1456  Line 1561 
1561  #endif  #endif
1562    
1563          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1564                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1565                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1566                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1567                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1466  Line 1571 
1571                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1572                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1573    
1574                          // the last P_VOP is skip macroblock ?                          /*
1575                             * skip if the co-located P_VOP macroblock is not coded
1576                             * if not codec in co-located S_VOP macroblock is _not_
1577                             * automatically skipped
1578                             */
1579    
1580                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1581                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1582                                  mb->cbp = 0;                                  mb->cbp = 0;
1583  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1584                                  mb->mb_type = MODE_NOT_CODED;                                  mb->mb_type = MODE_NOT_CODED;
# Line 1476  Line 1586 
1586  #endif  #endif
1587                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1588                                  mb->quant = last_mb->quant;                                  mb->quant = last_mb->quant;
1589                                  //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  /*
1590                                  //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1591                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1592                                    */
1593    
1594                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1595                                  continue;                                  continue;
1596                          }                          }
1597    
1598                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1599                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1600    
1601                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1602    
1603                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1604                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1605                                  } else {                                  } else {
1606                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1509  Line 1621 
1621    
1622                          mb->quant = quant;                          mb->quant = quant;
1623                          mb->mode = MODE_INTER4V;                          mb->mode = MODE_INTER4V;
1624                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /* DEBUG1("Switch bm_type=",mb->mb_type); */
1625    
1626  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1627          BFRAME_DEBUG          BFRAME_DEBUG
# Line 1538  Line 1650 
1650                                                                                    / TRD                                                                                    / TRD
1651                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1652                                          }                                          }
1653                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1654                                  }                                  }
1655                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1656                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
# Line 1556  Line 1668 
1668    
1669                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1670                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
1671                                  //DEBUG("B-frame Bidir!\n");                                  /* DEBUG("B-frame Bidir!\n"); */
1672                                  break;                                  break;
1673    
1674                          case MODE_BACKWARD:                          case MODE_BACKWARD:
# Line 1566  Line 1678 
1678    
1679                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1680                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1681                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1682                                  break;                                  break;
1683    
1684                          case MODE_FORWARD:                          case MODE_FORWARD:
# Line 1576  Line 1688 
1688    
1689                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1690                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1691                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1692                                  break;                                  break;
1693    
1694                          default:                          default:
1695                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not support B-frame mb_type = %i\n", mb->mb_type);
1696                          }                          }
1697                    } /* End of for */
                 }                                               // end of FOR  
1698          }          }
1699    
1700  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1701          if (!first){          if (!first){
1702                  first=1;                  first=1;
# Line 1594  Line 1706 
1706  #endif  #endif
1707  }  }
1708    
1709  // swap two MACROBLOCK array  
1710  void  
1711  mb_swap(MACROBLOCK ** mb1,  /* perform post processing if necessary, and output the image */
1712                  MACROBLOCK ** mb2)  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1713                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1714  {  {
         MACROBLOCK *temp = *mb1;  
1715    
1716          *mb1 = *mb2;  
1717          *mb2 = temp;          image_output(img, dec->width, dec->height,
1718                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1719                                     frame->output.csp, dec->interlacing);
1720    
1721            if (stats)
1722            {
1723                    stats->type = coding2type(coding_type);
1724                    stats->data.vop.time_base = (int)dec->time_base;
1725                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1726  }  }
1727    }
1728    
1729    
1730  int  int
1731  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1732                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1733  {  {
1734    
1735          Bitstream bs;          Bitstream bs;
# Line 1617  Line 1739 
1739          uint32_t fcode_forward;          uint32_t fcode_forward;
1740          uint32_t fcode_backward;          uint32_t fcode_backward;
1741          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1742          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1743          uint32_t vop_type;          int coding_type;
1744          int success = 0;          int success, output, seen_something;
1745            idctFuncPtr idct_save;
1746    
1747            if (XVID_MAJOR(frame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1))      /* v1.x.x */
1748                    return XVID_ERR_VERSION;
1749    
1750          start_global_timer();          start_global_timer();
1751    
1752          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1753            if ((frame->general & XVID_DISCONTINUITY))
1754                    dec->frames = 0;
1755            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1756    
1757            if (frame->length < 0)  /* decoder flush */
1758            {
1759            int ret;
1760                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1761                        we have a reference frame, then outout the reference frame */
1762                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1763                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1764                dec->frames = 0;
1765                ret = 0;
1766            }else{
1767                if (stats) stats->type = XVID_TYPE_NOTHING;
1768                ret = XVID_ERR_END;
1769            }
1770    
1771                    emms();
1772                    stop_global_timer();
1773                    return ret;
1774            }
1775    
1776          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1777    
1778          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1779          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1780          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1781                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1782                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1783                    if (stats) stats->type = XVID_TYPE_NOTHING;
1784                  emms();                  emms();
1785                  return XVID_ERR_OK;                  return 1;   /* one byte consumed */
1786          }          }
1787    
1788  start:          success = 0;
1789          // add by chenm001 <chenm001@163.com>          output = 0;
1790          // for support B-frame to reference last 2 frame          seen_something = 0;
1791          dec->frames++;          idct_save = idct;
1792    
1793  xxx:  repeat:
         vop_type =  
                 BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
                         &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);  
1794    
1795          //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          coding_type =   BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1796                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1797    
1798          if (vop_type == -1 && success)          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1799                  goto done;                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1800    
1801          if (vop_type == -2 || vop_type == -3)          if (coding_type == -1) /* nothing */
1802          {          {
1803                  if (vop_type == -3)                  if (success) goto done;
1804            if (stats) stats->type = XVID_TYPE_NOTHING;
1805                    emms();
1806            return BitstreamPos(&bs)/8;
1807            }
1808    
1809            if (coding_type == -2 || coding_type == -3)   /* vol and/or resize */
1810            {
1811                    if (coding_type == -3)
1812                          decoder_resize(dec);                          decoder_resize(dec);
1813    
1814                  if (stats)                  if (stats)
1815                  {                  {
1816                          stats->notify = XVID_DEC_VOL;                          stats->type = XVID_TYPE_VOL;
1817                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1818                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1819                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1820                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1821                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1822                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1823                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1824                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1825                          frame->length = BitstreamPos(&bs) / 8;                          emms();
1826                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1827                  }                  }
1828                  goto xxx;                  goto repeat;
1829          }          }
1830    
1831          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1832    
1833          switch (vop_type) {  #if defined(ARCH_IS_IA32)
1834          case P_VOP:          /*
1835                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,           * /!\ Ugly hack /!\
1836                                                  fcode_forward, intra_dc_threshold, NULL);           * IA32: Prior to xvid bitstream 10, we were using Walten's mmx/xmm idct
1837  #ifdef BFRAMES_DEC           */
1838                  DEBUG1("P_VOP  Time=", dec->time);          if((idct == simple_idct_mmx) && (dec->bs_version < 10))
1839                    idct = idct_mmx;
1840  #endif  #endif
                 break;  
1841    
1842            /* packed_mode: special-N_VOP treament */
1843            if (dec->packed_mode && coding_type == N_VOP)
1844            {
1845                    if (dec->low_delay_default && dec->frames > 0)
1846                    {
1847                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1848                            output = 1;
1849                    }
1850                    /* ignore otherwise */
1851            }
1852            else if (coding_type != B_VOP)
1853            {
1854                    switch(coding_type)
1855                    {
1856          case I_VOP:          case I_VOP:
1857                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
1858                  break;                  break;
1859                    case P_VOP :
1860          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1861  #ifdef BFRAMES_DEC                                                  fcode_forward, intra_dc_threshold, NULL);
                 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!");  
                 }  
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
1862                  break;                  break;
   
1863          case S_VOP :          case S_VOP :
1864                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1865                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1866                  break;                  break;
1867                    case N_VOP :
1868          case N_VOP:                             // vop not coded                          /* XXX: not_coded vops are not used for forward prediction */
1869                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                          /* we should not swap(last_mbs,mbs) */
1870                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
 #ifdef BFRAMES_DEC  
                 DEBUG1("N_VOP  Time=", dec->time);  
 #endif  
1871                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1872          }          }
1873    
   
1874          if (reduced_resolution)          if (reduced_resolution)
1875          {          {
1876                  image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                  image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1877                          (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width);                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1878                                    16, 0);
1879          }          }
1880    
1881          BitstreamByteAlign(&bs);                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1882                    if (!(dec->low_delay_default && dec->packed_mode))
1883  #ifdef BFRAMES_DEC                  {
1884          // test if no B_VOP                          if (dec->low_delay)
1885          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {                          {
1886  #endif                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1887                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                  output = 1;
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
   
 #ifdef BFRAMES_DEC  
         } else {  
                 if (dec->frames >= 1 && !(dec->packed_mode)) {  
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1888                          }                          }
1889                          stop_conv_timer();                          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, stats, dec->last_coding_type);
1893                                    output = 1;
1894                  }                  }
1895          }          }
 #endif  
1896    
         if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_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]);
1899            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1900                    dec->last_reduced_resolution = reduced_resolution;
1901            dec->last_coding_type = coding_type;
1902    
1903                    dec->frames++;
1904                    seen_something = 1;
1905    
1906            }else{  /* B_VOP */
1907    
1908                    if (dec->low_delay)
1909                    {
1910                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1911                            dec->low_delay = 1;
1912                    }
1913    
1914                    if (dec->frames < 2)
1915                    {
1916                            /* attemping to decode a bvop without atleast 2 reference frames */
1917                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1918                                                    "broken b-frame, mising ref frames");
1919                    }else if (dec->time_pp <= dec->time_bp) {
1920                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1921                            decoded in vfw. */
1922                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1923                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1924                    }else{
1925                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1926                    }
1927    
1928                  // swap MACROBLOCK                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1929                  // the Divx will not set the low_delay flage some times                  output = 1;
1930                  // so follow code will wrong to not swap at that time                  dec->frames++;
                 // this will broken bitstream! so I'm change it,  
                 // But that is not the best way! can anyone tell me how  
                 // to do another way?  
                 // 18-07-2002   MinChen<chenm001@163.com>  
                 //if (!dec->low_delay && vop_type == P_VOP)  
                 if (vop_type == P_VOP)  
                         mb_swap(&dec->mbs, &dec->last_mbs);  
1931          }          }
1932    
1933            BitstreamByteAlign(&bs);
1934    
1935          if (success == 0 && dec->packed_mode)          /* low_delay_default mode: repeat in packed_mode */
1936            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1937          {          {
1938                  success = 1;                  success = 1;
1939          //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                  goto repeat;
                 goto start;  
1940          }          }
1941    
1942  done :  done :
1943    
1944          frame->length = BitstreamPos(&bs) / 8;          /* low_delay_default mode: if we've gotten here without outputting anything,
1945               then output the recently decoded frame, or print an error message  */
1946          if (stats)          if (dec->low_delay_default && output == 0)
1947          {          {
1948                  stats->notify = XVID_DEC_VOP;                  if (dec->packed_mode && seen_something)
1949                  stats->data.vop.time_base = (int)dec->time_base;                  {
1950                  stats->data.vop.time_increment = 0;     //XXX: todo                          /* output the recently decoded frame */
1951                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
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          emms();                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1962                            if (stats) stats->type = XVID_TYPE_NOTHING;
1963    
1964                    }
1965            }
1966    
1967            emms();
1968          stop_global_timer();          stop_global_timer();
1969    
1970          return XVID_ERR_OK;          idct = idct_save;
1971    
1972            return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1973  }  }

Legend:
Removed from v.1.37.2.18  
changed lines
  Added in v.1.49.2.8

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