[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.15, Sun Dec 8 05:38:56 2002 UTC revision 1.50, Mon Jun 9 17:02:38 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
9   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  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          }          }
# Line 206  Line 181 
181          image_null(&dec->cur);          image_null(&dec->cur);
182          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
183          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
184          image_null(&dec->refn[2]);          image_null(&dec->tmp);
185          image_null(&dec->refh);          image_null(&dec->qtmp);
186    
187            /* image based GMC */
188            image_null(&dec->gmc);
189    
190    
191          dec->mbs = NULL;          dec->mbs = NULL;
192          dec->last_mbs = NULL;          dec->last_mbs = NULL;
193    
194          init_timer();          init_timer();
195    
196          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
197          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
198          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
199          dec->low_delay = 0;          dec->low_delay = 0;
200          dec->packed_mode = 0;          dec->packed_mode = 0;
# Line 235  Line 213 
213  {  {
214          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
215          xvid_free(dec->mbs);          xvid_free(dec->mbs);
216    
217            /* image based GMC */
218            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
219    
220          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
221          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
222          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
223          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
225          xvid_free(dec);          xvid_free(dec);
226    
# Line 255  Line 237 
237    
238    
239    
240  // decode an intra macroblock  /* decode an intra macroblock */
   
241  void  void
242  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
243                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 291  Line 272 
272                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
273          }          }
274    
275          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
276    
277          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
278                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 314  Line 295 
295                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
296    
297                          if (dc_size > 8) {                          if (dc_size > 8) {
298                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
299                          }                          }
300    
301                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
# Line 326  Line 307 
307                  }                  }
308    
309                  start_timer();                  start_timer();
310                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
311                  {                  {
312                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
313                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 383  Line 364 
364    
365    
366    
367    /* decode an inter macroblock */
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
 // decode an inter macroblock  
   
 static void  
 set_block(uint8_t * dst, int stride, int width, int height, int color)  
 {  
         int i;  
         for (i = 0; i < height; i++)  
         {  
                 memset(dst, color, width);  
                 dst += stride;  
         }  
 }  
   
 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;  
         }  
 }  
   
   
   
368  void  void
369  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
370                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
371                                  const uint32_t x_pos,                                  const uint32_t x_pos,
372                                  const uint32_t y_pos,                                  const uint32_t y_pos,
373                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
374                                  const uint32_t cbp,                                  const uint32_t cbp,
375                                  Bitstream * bs,                                  Bitstream * bs,
376                                  const uint32_t quant,                                  const uint32_t quant,
# Line 442  Line 389 
389          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
390    
391          int uv_dx, uv_dy;          int uv_dx, uv_dy;
392          VECTOR mv[4];          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);  
         }  
393    
394          if (reduced_resolution) {          if (reduced_resolution) {
395                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
396                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
397                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
398                  DPRINTF(DPRINTF_MB,"[%i,%i] %i,%i  %i,%i  %i,%i  %i,%i",                  for (i = 0; i < 4; i++) {
399                          x_pos, y_pos,                          mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
400                          mv[0].x, mv[0].y,                          mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
401                          mv[1].x, mv[1].y,                  }
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
   
                 rrv_mv_scaleup(&mv[0]);  
                 rrv_mv_scaleup(&mv[1]);  
                 rrv_mv_scaleup(&mv[2]);  
                 rrv_mv_scaleup(&mv[3]);  
   
                 DPRINTF(DPRINTF_MB,"        %i,%i  %i,%i  %i,%i  %i,%i",  
                         mv[0].x, mv[0].y,  
                         mv[1].x, mv[1].y,  
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
402          }else{          }else{
403                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
404                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
405                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
406                    for (i = 0; i < 4; i++)
407                            mv[i] = pMB->mvs[i];
408          }          }
409    
410          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
411    
412                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
413                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
414    
415                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
416                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 504  Line 429 
429                  else                  else
430                  {                  {
431                          if(dec->quarterpel) {                          if(dec->quarterpel) {
432                                  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,
433                                                                                          dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
434                                                                                          mv[0].x, mv[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
435                          }                          }
436                          else {                          else {
# Line 553  Line 478 
478                          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,
479                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
480    
481                          // set_block(pY_Cur, stride, 32, 32, 127);                          /* set_block(pY_Cur, stride, 32, 32, 127); */
482                  }                  }
483                  else                  else
484                  {                  {
485                          if(dec->quarterpel) {                          if(dec->quarterpel) {
486                                  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,
487                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
488                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
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 + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
491                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].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, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
494                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].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 + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
497                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
498                          }                          }
499                          else {                          else {
# Line 593  Line 518 
518          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
519                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
520    
521                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
522                  {                  {
523                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
524    
525                          start_timer();                          start_timer();
526                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 654  Line 579 
579          stop_transfer_timer();          stop_transfer_timer();
580  }  }
581    
582    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
583    {
584            int length = 1 << (fcode+4);
585    
586    /*      if (quarterpel) value *= 2; */
587    
588            if (value < -length)
589                    return -length;
590            else if (value >= length)
591                    return length-1;
592            else return value;
593    }
594    
595    
596    static void
597    decoder_mbgmc(DECODER * dec,
598                                    MACROBLOCK * const pMB,
599                                    const uint32_t x_pos,
600                                    const uint32_t y_pos,
601                                    const uint32_t fcode,
602                                    const uint32_t cbp,
603                                    Bitstream * bs,
604                                    const uint32_t quant,
605                                    const uint32_t rounding,
606                                    const int reduced_resolution)   /* no reduced res support */
607    {
608    
609            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
610            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
611    
612            const uint32_t stride = dec->edged_width;
613            const uint32_t stride2 = stride / 2;
614            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
615            uint32_t i;
616            const uint32_t iQuant = pMB->quant;
617            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
618            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
619            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
620    
621            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
622    
623            start_timer();
624    
625    /* this is where the calculations are done */
626    
627            {
628                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
629                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680            if (cbp & 32)
681                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682            if (cbp & 16)
683                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
684            if (cbp & 8)
685                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
686            if (cbp & 4)
687                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
688            if (cbp & 2)
689                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
690            if (cbp & 1)
691                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
692            stop_transfer_timer();
693    }
694    
695    
696  void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
# Line 664  Line 702 
702  {  {
703          uint32_t bound;          uint32_t bound;
704          uint32_t x, y;          uint32_t x, y;
705          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
706          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
707    
708          if (reduced_resolution)          if (reduced_resolution)
709          {          {
710                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
711                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
712          }          }
713    
714          bound = 0;          bound = 0;
# Line 723  Line 761 
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 761  Line 799 
799          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
803    
804          mv.x += pmv.x;          mv.x += pmv.x;
805          mv.y += pmv.y;          mv.y += pmv.y;
# Line 784  Line 822 
822    
823    
824    
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
825    
826    
827  /* for P_VOP set gmc_mv to NULL */  /* for P_VOP set gmc_warp to NULL */
828  void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                             Bitstream * bs,                             Bitstream * bs,
# Line 807  Line 833 
833                             int quant,                             int quant,
834                             int fcode,                             int fcode,
835                             int intra_dc_threshold,                             int intra_dc_threshold,
836                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840          uint32_t bound;          uint32_t bound;
841          int cp_mb, st_mb;          int cp_mb, st_mb;
842          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
843          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
844    
845          if (reduced_resolution)          if (reduced_resolution)
846          {          {
847                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
848                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
849          }          }
850    
851          start_timer();          start_timer();
# Line 827  Line 853 
853                                     dec->width, dec->height);                                     dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856            if (gmc_warp)
857            {
858    
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
861                    {
862                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
863                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
864                                    dec->sprite_warping_points);
865                    }
866    
867                    generate_GMCparameters( dec->sprite_warping_points,
868                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
869                                    dec->width, dec->height, &dec->gmc_data);
870    
871    /* image warping is done block-based  in decoder_mbgmc(), now */
872    /*
873            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
874                                            mb_width, mb_height,
875                                            dec->edged_width, dec->edged_width/2,
876                                            fcode, dec->quarterpel, 0,
877                                            rounding, dec->mbs, &dec->gmc);
878    */
879            }
880    
881          bound = 0;          bound = 0;
882    
883          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 834  Line 885 
885                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
886                          MACROBLOCK *mb;                          MACROBLOCK *mb;
887    
888                          // skip stuffing                          /* skip stuffing */
889                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
890                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
891    
# Line 849  Line 900 
900    
901                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
902    
903                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
904                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
905                          {                          {
906                                  uint32_t mcbpc;                                  uint32_t mcbpc;
907                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 858  Line 909 
909                                  uint32_t cbpy;                                  uint32_t cbpy;
910                                  uint32_t cbp;                                  uint32_t cbp;
911                                  uint32_t intra;                                  uint32_t intra;
912                                  int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
913    
914                                  cp_mb++;                                  cp_mb++;
915                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 875  Line 926 
926                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
927                                  }                                  }
928    
929                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
930                                  {                                  {
931                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
932                                  }                                  }
933    
934                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
935                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
936    
937                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
938    
# Line 901  Line 952 
952                                  if (dec->interlacing) {                                  if (dec->interlacing) {
953                                          if (cbp || intra) {                                          if (cbp || intra) {
954                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
955                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
956                                          }                                          }
957    
958                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
959                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
960                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
961    
962                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
963                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
964                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
965                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
966                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
967                                                  }                                                  }
968                                          }                                          }
969                                  }                                  }
970    
971                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
972                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
973                                                                    rounding, reduced_resolution);
974                                            continue;
975    
976                                          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);  
977    
978                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
979                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
980                                                                                    fcode, bound);                                                                                    fcode, bound);
981                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 932  Line 983 
983                                          } else {                                          } else {
984                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
985                                                                                    fcode, bound);                                                                                    fcode, bound);
986                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
987                                          }                                          }
988                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
989    
# Line 943  Line 991 
991                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
992                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
993                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
994                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
995                                  {                                  {
996                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
997                                                  0;                                                  0;
# Line 954  Line 1002 
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
1005                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1006                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
1007    
1008                          }                          }
1009                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1010                          {                          {
1011                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1012                                  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);  
1013                                  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();
1014                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1015                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1016                                                                    rounding, reduced_resolution);
1017    
1018                                    stop_transfer_timer();
1019    
1020                                    if(dec->out_frm && cp_mb > 0) {
1021                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1022                                      cp_mb = 0;
1023                                    }
1024                                    st_mb = x+1;
1025                          }                          }
1026                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1027                          {                          {
# Line 971  Line 1029 
1029    
1030                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1031                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1032                                  // copy macroblock directly from ref to cur                                  /* copy macroblock directly from ref to cur */
1033    
1034                                  start_timer();                                  start_timer();
1035    
# Line 1019  Line 1077 
1077  }  }
1078    
1079    
1080  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
 // decode B-frame motion vector  
1081  void  void
1082  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
1083                                          Bitstream * bs,                                          Bitstream * bs,
# Line 1064  Line 1121 
1121  }  }
1122    
1123    
1124  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
 // decode an B-frame forward & backward inter macroblock  
1125  void  void
1126  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1127                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 1125  Line 1181 
1181    
1182          start_timer();          start_timer();
1183          if(dec->quarterpel) {          if(dec->quarterpel) {
1184                  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,
1185                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1186                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1187          }          }
1188          else {          else {
# Line 1149  Line 1205 
1205          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1206                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1207    
1208                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1209                  {                  {
1210                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1211    
1212                          start_timer();                          start_timer();
1213                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1192  Line 1248 
1248          stop_transfer_timer();          stop_transfer_timer();
1249  }  }
1250    
1251  // add by MinChen <chenm001@163.com>  /* decode an B-frame direct &  inter macroblock */
 // decode an B-frame direct &  inter macroblock  
1252  void  void
1253  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1254                                                             IMAGE forward,                                                             IMAGE forward,
# Line 1280  Line 1335 
1335          start_timer();          start_timer();
1336          if(dec->quarterpel) {          if(dec->quarterpel) {
1337                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1338                          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,
1339                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1340                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1341                  else {                  else {
1342                          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,
1343                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1344                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1345                          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 + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1347                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].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, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1350                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].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 + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1353                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1354                  }                  }
1355          }          }
# Line 1318  Line 1373 
1373    
1374          if(dec->quarterpel) {          if(dec->quarterpel) {
1375                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1376                          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,
1377                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1378                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1379                  else {                  else {
1380                          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,
1381                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1382                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1383                          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 + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1385                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].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, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1388                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].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 + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1391                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1392                  }                  }
1393          }          }
1394          else {          else {
1395                  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,
1396                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1397                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1398                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1399                                                            0);                                                            0);
1400                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1401                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1402                                                            stride, 0);                                                            stride, 0);
1403                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1404                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1405                                                            stride, 0);                                                            stride, 0);
1406          }          }
1407    
1408          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,
1409                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1410          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1411                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1412    
1413          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1414                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1415                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1416                                                  stride, 1, 8);                                                  stride, 1, 8);
1417    
1418          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,
1419                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1420                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1421                                                  stride, 1, 8);                                                  stride, 1, 8);
1422    
1423          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,
1424                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1425                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1426                                                  stride, 1, 8);                                                  stride, 1, 8);
1427    
1428          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,
1429                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1430                                                  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,
1431                                                  stride, 1, 8);                                                  stride, 1, 8);
1432    
1433          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1434                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1435                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1436                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1437    
1438          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1439                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1440                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1441                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1442    
1443          stop_comp_timer();          stop_comp_timer();
# Line 1390  Line 1445 
1445          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1446                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1447    
1448                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1449                  {                  {
1450                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1451    
1452                          start_timer();                          start_timer();
1453                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1434  Line 1489 
1489  }  }
1490    
1491    
1492  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
 // for decode B-frame dbquant  
1493  int32_t __inline  int32_t __inline
1494  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1495  {  {
1496          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1497                  return (0);                  return (0);
1498          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1499                  return (-2);                  return (-2);
1500          else          else                           /* '11' */
1501                  return (2);                             // '11'                  return (2);
1502  }  }
1503    
1504  // add by MinChen <chenm001@163.com>  /*
1505  // for decode B-frame mb_type   * For decode B-frame mb_type
1506  // bit   ret_value   * bit   ret_value
1507  // 1        0   * 1        0
1508  // 01       1   * 01       1
1509  // 001      2   * 001      2
1510  // 0001     3   * 0001     3
1511     */
1512  int32_t __inline  int32_t __inline
1513  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1514  {  {
# Line 1502  Line 1557 
1557  #endif  #endif
1558    
1559          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1560                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1561                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1562                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1563                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1512  Line 1567 
1567                          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] =
1568                          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;
1569    
1570                          // the last P_VOP is skip macroblock ?                          /*
1571                             * skip if the co-located P_VOP macroblock is not coded
1572                             * if not codec in co-located S_VOP macroblock is _not_
1573                             * automatically skipped
1574                             */
1575    
1576                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1577                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1578                                  mb->cbp = 0;                                  mb->cbp = 0;
1579  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1580                                  mb->mb_type = MODE_NOT_CODED;                                  mb->mb_type = MODE_NOT_CODED;
# Line 1522  Line 1582 
1582  #endif  #endif
1583                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1584                                  mb->quant = last_mb->quant;                                  mb->quant = last_mb->quant;
1585                                  //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  /*
1586                                  //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;
1587                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1588                                    */
1589    
1590                                  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);
1591                                  continue;                                  continue;
1592                          }                          }
1593    
1594                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1595                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1596    
1597                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1598    
1599                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1600                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1601                                  } else {                                  } else {
1602                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1555  Line 1617 
1617    
1618                          mb->quant = quant;                          mb->quant = quant;
1619                          mb->mode = MODE_INTER4V;                          mb->mode = MODE_INTER4V;
1620                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /* DEBUG1("Switch bm_type=",mb->mb_type); */
1621    
1622  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1623          BFRAME_DEBUG          BFRAME_DEBUG
# Line 1584  Line 1646 
1646                                                                                    / TRD                                                                                    / TRD
1647                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1648                                          }                                          }
1649                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1650                                  }                                  }
1651                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1652                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
# Line 1602  Line 1664 
1664    
1665                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1666                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
1667                                  //DEBUG("B-frame Bidir!\n");                                  /* DEBUG("B-frame Bidir!\n"); */
1668                                  break;                                  break;
1669    
1670                          case MODE_BACKWARD:                          case MODE_BACKWARD:
# Line 1612  Line 1674 
1674    
1675                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1676                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1677                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1678                                  break;                                  break;
1679    
1680                          case MODE_FORWARD:                          case MODE_FORWARD:
# Line 1622  Line 1684 
1684    
1685                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1686                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1687                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1688                                  break;                                  break;
1689    
1690                          default:                          default:
1691                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1692                          }                          }
1693                    } /* End of for */
                 }                                               // end of FOR  
1694          }          }
1695    
1696  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1697          if (!first){          if (!first){
1698                  first=1;                  first=1;
# Line 1640  Line 1702 
1702  #endif  #endif
1703  }  }
1704    
1705  // swap two MACROBLOCK array  /* swap two MACROBLOCK array */
1706  void  void
1707  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
1708                  MACROBLOCK ** mb2)                  MACROBLOCK ** mb2)
# Line 1651  Line 1713 
1713          *mb2 = temp;          *mb2 = temp;
1714  }  }
1715    
1716    
1717    /* perform post processing if necessary, and output the image */
1718    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1719                                            const XVID_DEC_FRAME * frame, int pp_disable)
1720    {
1721    
1722            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1723            {
1724                    /* note: image is stored to tmp */
1725                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1726                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1727                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1728                                                    8, frame->general);
1729                    img = &dec->tmp;
1730            }
1731    
1732            image_output(img, dec->width, dec->height,
1733                                     dec->edged_width, frame->image, frame->stride,
1734                                     frame->colorspace, dec->interlacing);
1735    }
1736    
1737    
1738  int  int
1739  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1740                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1663  Line 1747 
1747          uint32_t fcode_forward;          uint32_t fcode_forward;
1748          uint32_t fcode_backward;          uint32_t fcode_backward;
1749          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1750          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1751          uint32_t vop_type;          int vop_type;
1752          int success = 0;          int success = 0;
1753            int output = 0;
1754            int seen_something = 0;
1755            idctFuncPtr idct_save = idct;
1756    
1757          start_global_timer();          start_global_timer();
1758    
1759            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1760          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1761    
1762            if ((frame->general & XVID_DEC_DISCONTINUITY))
1763                    dec->frames = 0;
1764    
1765            if (frame->length < 0)  /* decoder flush */
1766            {
1767                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1768                        we have a reference frame, then outout the reference frame */
1769                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1770                    {
1771                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1772                            output = 1;
1773                    }
1774    
1775                    frame->length = 0;
1776                    if (stats)
1777                    {
1778                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1779                            stats->data.vop.time_base = (int)dec->time_base;
1780                            stats->data.vop.time_increment = 0;     /* XXX: todo */
1781                    }
1782    
1783                    emms();
1784    
1785                    stop_global_timer();
1786                    return XVID_ERR_OK;
1787            }
1788    
1789          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1790    
1791          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1792          if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1793          {          {
1794                  if (stats)                  if (stats)
1795                          stats->notify = XVID_DEC_VOP;                          stats->notify = XVID_DEC_VOP;
# Line 1685  Line 1800 
1800                  return XVID_ERR_OK;                  return XVID_ERR_OK;
1801          }          }
1802    
1803  start:  repeat:
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         dec->frames++;  
1804    
1805  xxx:          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1806          vop_type =                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
                 BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,  
                         &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);  
1807    
1808          //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1809                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1810    
1811          if (vop_type == -1 && success)          if (vop_type == -1)
1812                  goto done;          {
1813                    if (success) goto done;
1814                    emms();
1815                    return XVID_ERR_FAIL;
1816            }
1817    
1818          if (vop_type == -2 || vop_type == -3)          if (vop_type == -2 || vop_type == -3)
1819          {          {
# Line 1717  Line 1832 
1832                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1833                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1834                          frame->length = BitstreamPos(&bs) / 8;                          frame->length = BitstreamPos(&bs) / 8;
1835                            emms();
1836                          return XVID_ERR_OK;                          return XVID_ERR_OK;
1837                  }                  }
1838                  goto xxx;                  goto repeat;
1839          }          }
1840    
1841          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1842    
1843          switch (vop_type) {          if((idct == simple_idct_mmx) && (dec->bs_version < 10)) /* rather ugly but should work */
1844          case P_VOP:                  idct = idct_mmx;
                 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;  
1845    
1846            /* packed_mode: special-N_VOP treament */
1847            if (dec->packed_mode && vop_type == N_VOP)
1848            {
1849                    if (dec->low_delay_default && dec->frames > 0)
1850                    {
1851                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1852                            output = 1;
1853                    }
1854                    /* ignore otherwise */
1855            }
1856            else if (vop_type != B_VOP)
1857            {
1858                    switch(vop_type)
1859                    {
1860          case I_VOP:          case I_VOP:
1861                  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  
1862                  break;                  break;
1863                    case P_VOP :
1864          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1865  #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  
1866                  break;                  break;
   
1867          case S_VOP :          case S_VOP :
1868                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1869                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1870                  break;                  break;
1871                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1872                  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  
1873                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1874          }          }
1875    
   
         /* reduced resolution deblocking filter */  
   
1876          if (reduced_resolution)          if (reduced_resolution)
1877          {          {
1878                  const int rmb_height = dec->mb_height / 2;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1879                  const int rmb_width = dec->mb_width / 2;                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1880                  const int edged_width2 = dec->edged_width /2;                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1881                  int i,j;                  }
   
                 /* horizontal deblocking */  
1882    
1883                  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 */
1884                  for (i = 0; i < rmb_width*2; i++)                  if (!(dec->low_delay_default && dec->packed_mode))
1885                  {                  {
1886                          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)  
1887                          {                          {
1888                                  xvid_HFilter_31_C(dec->cur.y + (j*16 - 1)*dec->edged_width + i*16,                                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1889                                                                dec->cur.y + (j*16 + 0)*dec->edged_width + i*16, 2);                                  output = 1;
                         }  
1890                  }                  }
1891                            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++)  
                 {  
                         if (dec->mbs[(j-1)*dec->mb_width + i].mode != MODE_NOT_CODED ||  
                                 dec->mbs[(j+0)*dec->mb_width + i].mode != MODE_NOT_CODED)  
1892                          {                          {
1893                                  hfilter_31(dec->cur.u + (j*16 - 1)*edged_width2 + i*16,                                  /* output the reference frame */
1894                                                                    dec->cur.u + (j*16 + 0)*edged_width2 + i*16, 2);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1895                                  hfilter_31(dec->cur.v + (j*16 - 1)*edged_width2 + i*16,                                  output = 1;
                                                                   dec->cur.v + (j*16 + 0)*edged_width2 + i*16, 2);  
1896                          }                          }
1897                  }                  }
1898    
1899                  /* vertical deblocking */                  image_swap(&dec->refn[0], &dec->refn[1]);
1900                    image_swap(&dec->cur, &dec->refn[0]);
1901                    mb_swap(&dec->mbs, &dec->last_mbs);
1902                    dec->last_reduced_resolution = reduced_resolution;
1903    
1904                  for (j = 0; j < rmb_height*2; j++)              // luma: i,j in block units                  dec->frames++;
1905                  for (i = 1; i < rmb_width*2; i++)                  seen_something = 1;
1906                  {  
1907                          if (dec->mbs[(j/2)*dec->mb_width + (i-1)/2].mode != MODE_NOT_CODED ||          }else{  /* B_VOP */
1908                                  dec->mbs[(j/2)*dec->mb_width + (i+0)/2].mode != MODE_NOT_CODED)  
1909                    if (dec->low_delay)
1910                          {                          {
1911                                  vfilter_31(dec->cur.y + (j*16)*dec->edged_width + i*16 - 1,                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1912                                                                dec->cur.y + (j*16)*dec->edged_width + i*16 + 0,                          dec->low_delay = 1;
                                                                   dec->edged_width, 2);  
                         }  
1913                  }                  }
1914    
1915                  for (j = 0; j < rmb_height; j++)        // chroma                  if (dec->frames < 2)
                 for (i = 1; i < rmb_width; i++)  
                 {  
                         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)  
1916                          {                          {
1917                                  vfilter_31(dec->cur.u + (j*16)*edged_width2 + i*16 - 1,                          /* attemping to decode a bvop without atleast 2 reference frames */
1918                                                                    dec->cur.u + (j*16)*edged_width2 + i*16 + 0,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1919                                                                    edged_width2, 2);                                                  "broken b-frame, mising ref frames");
1920                                  vfilter_31(dec->cur.v + (j*16)*edged_width2 + i*16 - 1,                  }else if (dec->time_pp <= dec->time_bp) {
1921                                                                    dec->cur.v + (j*16)*edged_width2 + i*16 + 0,                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1922                                                                    edged_width2, 2);                          decoded in vfw. */
1923                          }                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1924                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1925                    }else{
1926                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1927                  }                  }
1928    
1929                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1930                    output = 1;
1931                    dec->frames++;
1932          }          }
1933    
1934          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1935    
1936  #ifdef BFRAMES_DEC          /* low_delay_default mode: repeat in packed_mode */
1937          // test if no B_VOP          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1938          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {          {
1939  #endif                  success = 1;
1940                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                  goto repeat;
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
   
 #ifdef BFRAMES_DEC  
         } else {  
                 if (dec->frames >= 1 && !(dec->packed_mode)) {  
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         }  
                         stop_conv_timer();  
                 }  
1941          }          }
 #endif  
1942    
1943          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {  done :
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
1944    
1945                  // swap MACROBLOCK          /* low_delay_default mode: if we've gotten here without outputting anything,
1946                  // the Divx will not set the low_delay flage some times             then output the recently decoded frame, or print an error message  */
1947                  // so follow code will wrong to not swap at that time          if (dec->low_delay_default && output == 0)
1948                  // this will broken bitstream! so I'm change it,          {
1949                  // But that is not the best way! can anyone tell me how                  if (dec->packed_mode && seen_something)
1950                  // to do another way?                  {
1951                  // 18-07-2002   MinChen<chenm001@163.com>                          /* output the recently decoded frame */
1952                  //if (!dec->low_delay && vop_type == P_VOP)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1953                  if (vop_type == P_VOP)                          output = 1;
                         mb_swap(&dec->mbs, &dec->last_mbs);  
1954          }          }
1955                    else
   
         if (success == 0 && dec->packed_mode)  
1956          {          {
1957                  success = 1;                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1958          //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1959                  goto start;                                  "warning: nothing to output");
1960                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1961                                    "bframe decoder lag");
1962    
1963                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1964                    }
1965          }          }
1966    
1967  done :          idct = idct_save;
1968    
1969          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1970    
1971          if (stats)          if (stats)
1972          {          {
1973                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1974                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1975                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     /* XXX: todo */
1976          }          }
1977    
1978          emms();          emms();

Legend:
Removed from v.1.37.2.15  
changed lines
  Added in v.1.50

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