[cvs] / xvidcore / src / decoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.37.2.18, Tue Dec 10 11:13:50 2002 UTC revision 1.49.4.1, Sat May 3 23:23:38 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
5   *   *
6   *  This program is an implementation of a part of one or more MPEG-4   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
  *  Video tools as specified in ISO/IEC 14496-2 standard.  Those intending  
  *  to use this software module in hardware or software products are  
  *  advised that its use may infringe existing patents or copyrights, and  
  *  any such use would be at such party's own risk.  The original  
  *  developer of this software module and his/her company, and subsequent  
  *  editors and their companies, will have no liability for use of this  
  *  software or modifications or derivatives thereof.  
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
9   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 26  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
  *************************************************************************/  
   
 /**************************************************************************  
  *  
  *  History:  
  *  
  *  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 90  Line 57 
57  #include "image/image.h"  #include "image/image.h"
58  #include "image/colorspace.h"  #include "image/colorspace.h"
59  #include "utils/mem_align.h"  #include "utils/mem_align.h"
60    #include "image/postprocessing.h"
61    
62  int  int
63  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
64  {  {
65          /* free existing */          /* free existing */
66            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
67          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
68          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
69          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
70          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
71          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
72            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
73    
74          if (dec->last_mbs)          if (dec->last_mbs)
75                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
# Line 108  Line 77 
77                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
78    
79          /* realloc */          /* realloc */
   
80          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
81          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
82    
# Line 126  Line 94 
94                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
95          }          }
96    
97          // add by chenm001 <chenm001@163.com>          /* Support B-frame to reference last 2 frame */
         // for support B-frame to reference last 2 frame  
98          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
99                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
100                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
101                  xvid_free(dec);                  xvid_free(dec);
102                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
103          }          }
104          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
105                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
106                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
107                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
108                    xvid_free(dec);
109                    return XVID_ERR_MEMORY;
110            }
111    
112            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
113                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
114                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
115                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
116                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
117                  xvid_free(dec);                  xvid_free(dec);
118                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
119          }          }
120    
121          if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
122                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
124                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
125                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
127                  xvid_free(dec);                  xvid_free(dec);
128                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
129          }          }
# Line 158  Line 135 
135                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
138                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
139                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
140                  xvid_free(dec);                  xvid_free(dec);
141                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
142          }          }
143          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
144    
145          // add by chenm001 <chenm001@163.com>          /* For skip MB flag */
         // for skip MB flag  
146          dec->last_mbs =          dec->last_mbs =
147                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
148                                          CACHE_LINE);                                          CACHE_LINE);
# Line 175  Line 151 
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
156                  xvid_free(dec);                  xvid_free(dec);
157                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
158          }          }
# Line 206  Line 182 
182          image_null(&dec->cur);          image_null(&dec->cur);
183          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
184          image_null(&dec->refn[1]);          image_null(&dec->refn[1]);
185          image_null(&dec->refn[2]);          image_null(&dec->tmp);
186          image_null(&dec->refh);          image_null(&dec->qtmp);
187    
188            /* image based GMC */
189            image_null(&dec->gmc);
190    
191    
192          dec->mbs = NULL;          dec->mbs = NULL;
193          dec->last_mbs = NULL;          dec->last_mbs = NULL;
194    
195          init_timer();          init_timer();
196    
197          // add by chenm001 <chenm001@163.com>          /* For B-frame support (used to save reference frame's time */
198          // for support B-frame to save reference frame's time          dec->frames = 0;
         dec->frames = -1;  
199          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
200          dec->low_delay = 0;          dec->low_delay = 0;
201          dec->packed_mode = 0;          dec->packed_mode = 0;
# Line 235  Line 214 
214  {  {
215          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
216          xvid_free(dec->mbs);          xvid_free(dec->mbs);
217    
218            /* image based GMC */
219            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
220    
221          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
222          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
223          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
225          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
226          xvid_free(dec);          xvid_free(dec);
227    
# Line 255  Line 238 
238    
239    
240    
241  // decode an intra macroblock  /* decode an intra macroblock */
   
242  void  void
243  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
244                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 291  Line 273 
273                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
274          }          }
275    
276          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
277    
278          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
279                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
# Line 314  Line 296 
296                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0;
297    
298                          if (dc_size > 8) {                          if (dc_size > 8) {
299                                  BitstreamSkip(bs, 1);   // marker                                  BitstreamSkip(bs, 1);   /* marker */
300                          }                          }
301    
302                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
# Line 326  Line 308 
308                  }                  }
309    
310                  start_timer();                  start_timer();
311                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
312                  {                  {
313                          int direction = dec->alternate_vertical_scan ?                          int direction = dec->alternate_vertical_scan ?
314                                  2 : pMB->acpred_directions[i];                                  2 : pMB->acpred_directions[i];
# Line 383  Line 365 
365    
366    
367    
368    /* decode an inter macroblock */
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
 // decode an inter macroblock  
   
369  void  void
370  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
371                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
372                                  const uint32_t x_pos,                                  const uint32_t x_pos,
373                                  const uint32_t y_pos,                                  const uint32_t y_pos,
374                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
375                                  const uint32_t cbp,                                  const uint32_t cbp,
376                                  Bitstream * bs,                                  Bitstream * bs,
377                                  const uint32_t quant,                                  const uint32_t quant,
# Line 432  Line 409 
409          }          }
410    
411          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
412    
413                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
414                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
415    
416                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
417                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 458  Line 430 
430                  else                  else
431                  {                  {
432                          if(dec->quarterpel) {                          if(dec->quarterpel) {
433                                  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,
434                                                                                          dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
435                                                                                          mv[0].x, mv[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride,  rounding);
436                          }                          }
437                          else {                          else {
# Line 507  Line 479 
479                          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,
480                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
481    
482                          // set_block(pY_Cur, stride, 32, 32, 127);                          /* set_block(pY_Cur, stride, 32, 32, 127); */
483                  }                  }
484                  else                  else
485                  {                  {
486                          if(dec->quarterpel) {                          if(dec->quarterpel) {
487                                  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,
488                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
489                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
490                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
492                                                                                    mv[1].x, mv[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
493                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
494                                                                                    dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
495                                                                                    mv[2].x, mv[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
496                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
497                                                                                    dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
498                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
499                          }                          }
500                          else {                          else {
# Line 547  Line 519 
519          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
520                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
521    
522                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
523                  {                  {
524                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
525    
526                          start_timer();                          start_timer();
527                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 608  Line 580 
580          stop_transfer_timer();          stop_transfer_timer();
581  }  }
582    
583    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
584    {
585            int length = 1 << (fcode+4);
586    
587    /*      if (quarterpel) value *= 2; */
588    
589            if (value < -length)
590                    return -length;
591            else if (value >= length)
592                    return length-1;
593            else return value;
594    }
595    
596    
597    static void
598    decoder_mbgmc(DECODER * dec,
599                                    MACROBLOCK * const pMB,
600                                    const uint32_t x_pos,
601                                    const uint32_t y_pos,
602                                    const uint32_t fcode,
603                                    const uint32_t cbp,
604                                    Bitstream * bs,
605                                    const uint32_t quant,
606                                    const uint32_t rounding,
607                                    const int reduced_resolution)   /* no reduced res support */
608    {
609    
610            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
611            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
612    
613            const uint32_t stride = dec->edged_width;
614            const uint32_t stride2 = stride / 2;
615            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
616            uint32_t i;
617            const uint32_t iQuant = pMB->quant;
618            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
619            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
620            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
621    
622            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
623    
624            start_timer();
625    
626    /* this is where the calculations are done */
627    
628            {
629                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
630                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
631    
632                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
633                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
634            }
635            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
636    
637    /*
638            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
639            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
640            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
641    */
642    
643    
644            stop_transfer_timer();
645    
646            if (!cbp) return;
647    
648            for (i = 0; i < 6; i++) {
649                    int direction = dec->alternate_vertical_scan ? 2 : 0;
650    
651                    if (cbp & (1 << (5 - i)))       /* coded */
652                    {
653                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
654    
655                            start_timer();
656                            get_inter_block(bs, &block[i * 64], direction);
657                            stop_coding_timer();
658    
659                            start_timer();
660                            if (dec->quant_type == 0) {
661                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
662                            } else {
663                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
664                            }
665                            stop_iquant_timer();
666    
667                            start_timer();
668                            idct(&data[i * 64]);
669                            stop_idct_timer();
670                    }
671            }
672    
673    /* interlace + GMC is this possible ??? */
674    /*
675      if (dec->interlacing && pMB->field_dct) {
676              next_block = stride;
677              stride *= 2;
678      }
679    */
680            start_timer();
681            if (cbp & 32)
682                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
683            if (cbp & 16)
684                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
685            if (cbp & 8)
686                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
687            if (cbp & 4)
688                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
689            if (cbp & 2)
690                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
691            if (cbp & 1)
692                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
693            stop_transfer_timer();
694    }
695    
696    
697  void  void
698  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
# Line 618  Line 703 
703  {  {
704          uint32_t bound;          uint32_t bound;
705          uint32_t x, y;          uint32_t x, y;
706          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
707          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
708    
709          if (reduced_resolution)          if (reduced_resolution)
710          {          {
# Line 677  Line 762 
762    
763                          if (dec->interlacing) {                          if (dec->interlacing) {
764                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
765                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
766                          }                          }
767    
768                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 715  Line 800 
800          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
801          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
802    
803          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
804    
805          mv.x += pmv.x;          mv.x += pmv.x;
806          mv.y += pmv.y;          mv.y += pmv.y;
# Line 738  Line 823 
823    
824    
825    
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
826    
         if (quarterpel) value *= 2;  
827    
828          if (value < -length)  /* for P_VOP set gmc_warp to NULL */
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
   
   
 /* for P_VOP set gmc_mv to NULL */  
829  void  void
830  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
831                             Bitstream * bs,                             Bitstream * bs,
# Line 761  Line 834 
834                             int quant,                             int quant,
835                             int fcode,                             int fcode,
836                             int intra_dc_threshold,                             int intra_dc_threshold,
837                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
838  {  {
839    
840          uint32_t x, y;          uint32_t x, y;
841          uint32_t bound;          uint32_t bound;
842          int cp_mb, st_mb;          int cp_mb, st_mb;
843          int mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
844          int mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
845    
846          if (reduced_resolution)          if (reduced_resolution)
847          {          {
# Line 781  Line 854 
854                                     dec->width, dec->height);                                     dec->width, dec->height);
855          stop_edges_timer();          stop_edges_timer();
856    
857            if (gmc_warp)
858            {
859    
860                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
861                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
862                    {
863                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
864                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
865                                    dec->sprite_warping_points);
866                    }
867    
868                    generate_GMCparameters( dec->sprite_warping_points,
869                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
870                                    dec->width, dec->height, &dec->gmc_data);
871    
872    /* image warping is done block-based  in decoder_mbgmc(), now */
873    /*
874            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
875                                            mb_width, mb_height,
876                                            dec->edged_width, dec->edged_width/2,
877                                            fcode, dec->quarterpel, 0,
878                                            rounding, dec->mbs, &dec->gmc);
879    */
880            }
881    
882          bound = 0;          bound = 0;
883    
884          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 788  Line 886 
886                  for (x = 0; x < mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
887                          MACROBLOCK *mb;                          MACROBLOCK *mb;
888    
889                          // skip stuffing                          /* skip stuffing */
890                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
891                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
892    
# Line 803  Line 901 
901    
902                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
903    
904                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
905                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
906                          {                          {
907                                  uint32_t mcbpc;                                  uint32_t mcbpc;
908                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 812  Line 910 
910                                  uint32_t cbpy;                                  uint32_t cbpy;
911                                  uint32_t cbp;                                  uint32_t cbp;
912                                  uint32_t intra;                                  uint32_t intra;
913                                  int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
914    
915                                  cp_mb++;                                  cp_mb++;
916                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 829  Line 927 
927                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
928                                  }                                  }
929    
930                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
931                                  {                                  {
932                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
933                                  }                                  }
934    
935                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
936                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
937    
938                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
939    
# Line 855  Line 953 
953                                  if (dec->interlacing) {                                  if (dec->interlacing) {
954                                          if (cbp || intra) {                                          if (cbp || intra) {
955                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
956                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
957                                          }                                          }
958    
959                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
960                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
961                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
962    
963                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
964                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
965                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
966                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
967                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
968                                                  }                                                  }
969                                          }                                          }
970                                  }                                  }
971    
972                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
973                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
974                                                                    rounding, reduced_resolution);
975                                            continue;
976    
977                                          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);  
978    
979                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
980                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
981                                                                                    fcode, bound);                                                                                    fcode, bound);
982                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 886  Line 984 
984                                          } else {                                          } else {
985                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
986                                                                                    fcode, bound);                                                                                    fcode, bound);
987                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
988                                          }                                          }
989                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
990    
# Line 897  Line 992 
992                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
993                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
994                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
995                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
996                                  {                                  {
997                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
998                                                  0;                                                  0;
# Line 908  Line 1003 
1003                                          continue;                                          continue;
1004                                  }                                  }
1005    
1006                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1007                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
1008    
1009                          }                          }
1010                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1011                          {                          {
1012                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1013                                  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);  
1014                                  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();
1015                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1016                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1017                                                                    rounding, reduced_resolution);
1018    
1019                                    stop_transfer_timer();
1020    
1021                                    if(dec->out_frm && cp_mb > 0) {
1022                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1023                                      cp_mb = 0;
1024                                    }
1025                                    st_mb = x+1;
1026                          }                          }
1027                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1028                          {                          {
# Line 925  Line 1030 
1030    
1031                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1032                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1033                                  // copy macroblock directly from ref to cur                                  /* copy macroblock directly from ref to cur */
1034    
1035                                  start_timer();                                  start_timer();
1036    
# Line 973  Line 1078 
1078  }  }
1079    
1080    
1081  // add by MinChen <chenm001@163.com>  /* decode B-frame motion vector */
 // decode B-frame motion vector  
1082  void  void
1083  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(DECODER * dec,
1084                                          Bitstream * bs,                                          Bitstream * bs,
# Line 1018  Line 1122 
1122  }  }
1123    
1124    
1125  // add by MinChen <chenm001@163.com>  /* decode an B-frame forward & backward inter macroblock */
 // decode an B-frame forward & backward inter macroblock  
1126  void  void
1127  decoder_bf_mbinter(DECODER * dec,  decoder_bf_mbinter(DECODER * dec,
1128                                     const MACROBLOCK * pMB,                                     const MACROBLOCK * pMB,
# Line 1079  Line 1182 
1182    
1183          start_timer();          start_timer();
1184          if(dec->quarterpel) {          if(dec->quarterpel) {
1185                  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,
1186                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1187                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                      pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1188          }          }
1189          else {          else {
# Line 1103  Line 1206 
1206          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1207                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1208    
1209                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1210                  {                  {
1211                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1212    
1213                          start_timer();                          start_timer();
1214                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1146  Line 1249 
1249          stop_transfer_timer();          stop_transfer_timer();
1250  }  }
1251    
1252  // add by MinChen <chenm001@163.com>  /* decode an B-frame direct &  inter macroblock */
 // decode an B-frame direct &  inter macroblock  
1253  void  void
1254  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1255                                                             IMAGE forward,                                                             IMAGE forward,
# Line 1234  Line 1336 
1336          start_timer();          start_timer();
1337          if(dec->quarterpel) {          if(dec->quarterpel) {
1338                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1339                          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,
1340                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1341                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1342                  else {                  else {
1343                          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,
1344                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1345                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1346                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1347                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1348                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1349                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1350                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1351                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1352                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1353                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1354                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1355                  }                  }
1356          }          }
# Line 1272  Line 1374 
1374    
1375          if(dec->quarterpel) {          if(dec->quarterpel) {
1376                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1377                          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,
1378                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1379                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1380                  else {                  else {
1381                          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,
1382                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1383                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1384                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1385                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1386                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                                              pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1387                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1388                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1389                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                                              pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1390                          interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1391                                                                              dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1392                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1393                  }                  }
1394          }          }
1395          else {          else {
1396                  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,
1397                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1398                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1399                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1400                                                            0);                                                            0);
1401                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1402                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1403                                                            stride, 0);                                                            stride, 0);
1404                  interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1405                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1406                                                            stride, 0);                                                            stride, 0);
1407          }          }
1408    
1409          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,
1410                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1411          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1412                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1413    
1414          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1415                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1416                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1417                                                  stride, 1, 8);                                                  stride, 1, 8);
1418    
1419          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,
1420                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1421                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1422                                                  stride, 1, 8);                                                  stride, 1, 8);
1423    
1424          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,
1425                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1426                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1427                                                  stride, 1, 8);                                                  stride, 1, 8);
1428    
1429          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,
1430                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1431                                                  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,
1432                                                  stride, 1, 8);                                                  stride, 1, 8);
1433    
1434          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1435                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1436                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1437                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1438    
1439          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1440                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1441                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1442                                                  stride2, 1, 8);                                                  stride2, 1, 8);
1443    
1444          stop_comp_timer();          stop_comp_timer();
# Line 1344  Line 1446 
1446          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1447                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1448    
1449                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       /* coded */
1450                  {                  {
1451                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1452    
1453                          start_timer();                          start_timer();
1454                          get_inter_block(bs, &block[i * 64], direction);                          get_inter_block(bs, &block[i * 64], direction);
# Line 1388  Line 1490 
1490  }  }
1491    
1492    
1493  // add by MinChen <chenm001@163.com>  /* for decode B-frame dbquant */
 // for decode B-frame dbquant  
1494  int32_t __inline  int32_t __inline
1495  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1496  {  {
1497          if (!BitstreamGetBit(bs))       // '0'          if (!BitstreamGetBit(bs))      /*  '0' */
1498                  return (0);                  return (0);
1499          else if (!BitstreamGetBit(bs))  // '10'          else if (!BitstreamGetBit(bs)) /* '10' */
1500                  return (-2);                  return (-2);
1501          else          else                           /* '11' */
1502                  return (2);                             // '11'                  return (2);
1503  }  }
1504    
1505  // add by MinChen <chenm001@163.com>  /*
1506  // for decode B-frame mb_type   * For decode B-frame mb_type
1507  // bit   ret_value   * bit   ret_value
1508  // 1        0   * 1        0
1509  // 01       1   * 01       1
1510  // 001      2   * 001      2
1511  // 0001     3   * 0001     3
1512     */
1513  int32_t __inline  int32_t __inline
1514  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1515  {  {
# Line 1456  Line 1558 
1558  #endif  #endif
1559    
1560          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1561                  // Initialize Pred Motion Vector                  /* Initialize Pred Motion Vector */
1562                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1563                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1564                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
# Line 1466  Line 1568 
1568                          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] =
1569                          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;
1570    
1571                          // the last P_VOP is skip macroblock ?                          /*
1572                             * skip if the co-located P_VOP macroblock is not coded
1573                             * if not codec in co-located S_VOP macroblock is _not_
1574                             * automatically skipped
1575                             */
1576    
1577                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1578                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1579                                  mb->cbp = 0;                                  mb->cbp = 0;
1580  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1581                                  mb->mb_type = MODE_NOT_CODED;                                  mb->mb_type = MODE_NOT_CODED;
# Line 1476  Line 1583 
1583  #endif  #endif
1584                                  mb->mb_type = MODE_FORWARD;                                  mb->mb_type = MODE_FORWARD;
1585                                  mb->quant = last_mb->quant;                                  mb->quant = last_mb->quant;
1586                                  //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                  /*
1587                                  //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1588                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1589                                    */
1590    
1591                                  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);
1592                                  continue;                                  continue;
1593                          }                          }
1594    
1595                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1596                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1597    
1598                                  mb->mb_type = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1599    
1600                                  if (!modb2) {   // modb=='00'                                  if (!modb2) {   /* modb=='00' */
1601                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1602                                  } else {                                  } else {
1603                                          mb->cbp = 0;                                          mb->cbp = 0;
# Line 1509  Line 1618 
1618    
1619                          mb->quant = quant;                          mb->quant = quant;
1620                          mb->mode = MODE_INTER4V;                          mb->mode = MODE_INTER4V;
1621                          //DEBUG1("Switch bm_type=",mb->mb_type);                          /* DEBUG1("Switch bm_type=",mb->mb_type); */
1622    
1623  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1624          BFRAME_DEBUG          BFRAME_DEBUG
# Line 1538  Line 1647 
1647                                                                                    / TRD                                                                                    / TRD
1648                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1649                                          }                                          }
1650                                          //DEBUG("B-frame Direct!\n");                                          /* DEBUG("B-frame Direct!\n"); */
1651                                  }                                  }
1652                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1653                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
# Line 1556  Line 1665 
1665    
1666                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1667                                                                                             mb, x, y, bs);                                                                                             mb, x, y, bs);
1668                                  //DEBUG("B-frame Bidir!\n");                                  /* DEBUG("B-frame Bidir!\n"); */
1669                                  break;                                  break;
1670    
1671                          case MODE_BACKWARD:                          case MODE_BACKWARD:
# Line 1566  Line 1675 
1675    
1676                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1677                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1678                                  //DEBUG("B-frame Backward!\n");                                  /* DEBUG("B-frame Backward!\n"); */
1679                                  break;                                  break;
1680    
1681                          case MODE_FORWARD:                          case MODE_FORWARD:
# Line 1576  Line 1685 
1685    
1686                                  mb->mode = MODE_INTER;                                  mb->mode = MODE_INTER;
1687                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1688                                  //DEBUG("B-frame Forward!\n");                                  /* DEBUG("B-frame Forward!\n"); */
1689                                  break;                                  break;
1690    
1691                          default:                          default:
1692                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1693                          }                          }
1694                    } /* End of for */
                 }                                               // end of FOR  
1695          }          }
1696    
1697  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1698          if (!first){          if (!first){
1699                  first=1;                  first=1;
# Line 1594  Line 1703 
1703  #endif  #endif
1704  }  }
1705    
1706  // swap two MACROBLOCK array  /* swap two MACROBLOCK array */
1707  void  void
1708  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
1709                  MACROBLOCK ** mb2)                  MACROBLOCK ** mb2)
# Line 1605  Line 1714 
1714          *mb2 = temp;          *mb2 = temp;
1715  }  }
1716    
1717    
1718    /* perform post processing if necessary, and output the image */
1719    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1720                                            const XVID_DEC_FRAME * frame, int pp_disable)
1721    {
1722    
1723            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1724            {
1725                    /* note: image is stored to tmp */
1726                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1727                    image_deblock(&dec->tmp, dec->edged_width,
1728                                              mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1729                                              frame->general);
1730                    img = &dec->tmp;
1731            }
1732    
1733            image_output(img, dec->width, dec->height,
1734                                     dec->edged_width, frame->image, frame->stride,
1735                                     frame->colorspace, dec->interlacing);
1736    }
1737    
1738    
1739  int  int
1740  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1741                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1617  Line 1748 
1748          uint32_t fcode_forward;          uint32_t fcode_forward;
1749          uint32_t fcode_backward;          uint32_t fcode_backward;
1750          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1751          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1752          uint32_t vop_type;          int vop_type;
1753          int success = 0;          int success = 0;
1754            int output = 0;
1755            int seen_something = 0;
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 1639  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 1671  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    
         switch (vop_type) {  
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  
                                                 fcode_forward, intra_dc_threshold, NULL);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("P_VOP  Time=", dec->time);  
 #endif  
                 break;  
1843    
1844            /* packed_mode: special-N_VOP treament */
1845            if (dec->packed_mode && vop_type == N_VOP)
1846            {
1847                    if (dec->low_delay_default && dec->frames > 0)
1848                    {
1849                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1850                            output = 1;
1851                    }
1852                    /* ignore otherwise */
1853            }
1854            else if (vop_type != B_VOP)
1855            {
1856                    switch(vop_type)
1857                    {
1858          case I_VOP:          case I_VOP:
1859                  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  
1860                  break;                  break;
1861                    case P_VOP :
1862          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1863  #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  
1864                  break;                  break;
   
1865          case S_VOP :          case S_VOP :
1866                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1867                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1868                  break;                  break;
1869                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1870                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
 #ifdef BFRAMES_DEC  
                 DEBUG1("N_VOP  Time=", dec->time);  
 #endif  
1871                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1872          }          }
1873    
   
1874          if (reduced_resolution)          if (reduced_resolution)
1875          {          {
1876                  image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                  image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1877                          (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width);                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1878                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1879          }          }
1880    
1881          BitstreamByteAlign(&bs);                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1882                    if (!(dec->low_delay_default && dec->packed_mode))
1883  #ifdef BFRAMES_DEC                  {
1884          // test if no B_VOP                          if (dec->low_delay)
1885          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {                          {
1886  #endif                                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1887                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                  output = 1;
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
   
 #ifdef BFRAMES_DEC  
         } else {  
                 if (dec->frames >= 1 && !(dec->packed_mode)) {  
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
1888                          }                          }
1889                          stop_conv_timer();                          else if (dec->frames > 0)       /* is the reference frame valid? */
1890                            {
1891                                    /* output the reference frame */
1892                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1893                                    output = 1;
1894                  }                  }
1895          }          }
 #endif  
1896    
         if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {  
1897                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1898                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
   
                 // swap MACROBLOCK  
                 // the Divx will not set the low_delay flage some times  
                 // so follow code will wrong to not swap at that time  
                 // this will broken bitstream! so I'm change it,  
                 // But that is not the best way! can anyone tell me how  
                 // to do another way?  
                 // 18-07-2002   MinChen<chenm001@163.com>  
                 //if (!dec->low_delay && vop_type == P_VOP)  
                 if (vop_type == P_VOP)  
1899                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1900                    dec->last_reduced_resolution = reduced_resolution;
1901    
1902                    dec->frames++;
1903                    seen_something = 1;
1904    
1905            }else{  /* B_VOP */
1906    
1907                    if (dec->low_delay)
1908                    {
1909                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1910                            dec->low_delay = 1;
1911          }          }
1912    
1913                    if (dec->frames < 2)
1914                    {
1915                            /* attemping to decode a bvop without atleast 2 reference frames */
1916                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1917                                                    "broken b-frame, mising ref frames");
1918                    }else if (dec->time_pp <= dec->time_bp) {
1919                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1920                            decoded in vfw. */
1921                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1922                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1923                    }else{
1924                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1925                    }
1926    
1927                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1928                    output = 1;
1929                    dec->frames++;
1930            }
1931    
1932            BitstreamByteAlign(&bs);
1933    
1934          if (success == 0 && dec->packed_mode)          /* low_delay_default mode: repeat in packed_mode */
1935            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1936          {          {
1937                  success = 1;                  success = 1;
1938          //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together                  goto repeat;
                 goto start;  
1939          }          }
1940    
1941  done :  done :
1942    
1943            /* low_delay_default mode: if we've gotten here without outputting anything,
1944               then output the recently decoded frame, or print an error message  */
1945            if (dec->low_delay_default && output == 0)
1946            {
1947                    if (dec->packed_mode && seen_something)
1948                    {
1949                            /* output the recently decoded frame */
1950                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1951                            output = 1;
1952                    }
1953                    else
1954                    {
1955                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1956                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1957                                    "warning: nothing to output");
1958                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1959                                    "bframe decoder lag");
1960    
1961                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1962                    }
1963            }
1964    
1965          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1966    
1967          if (stats)          if (stats)
1968          {          {
1969                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1970                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1971                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     /* XXX: todo */
1972          }          }
1973    
1974          emms();          emms();

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

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