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

Legend:
Removed from v.1.37.2.17  
changed lines
  Added in v.1.49.2.3

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