[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.9, Thu Apr 4 13:58:06 2002 UTC revision 1.18, Mon May 20 17:12:53 2002 UTC
# Line 1  Line 1 
1  /**************************************************************************  /**************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  -  Decoder main module  -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *      This program is an implementation of a part of one or more MPEG-4
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending
# Line 12  Line 12 
12   *      editors and their companies, will have no liability for use of this   *      editors and their companies, will have no liability for use of this
13   *      software or modifications or derivatives thereof.   *      software or modifications or derivatives thereof.
14   *   *
15   *      This program is xvid_free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
16   *      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
17   *      the xvid_free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
18   *      (at your option) any later version.   *      (at your option) any later version.
19   *   *
20   *      This program is distributed in the hope that it will be useful,   *      This program is distributed in the hope that it will be useful,
# Line 23  Line 23 
23   *      GNU General Public License for more details.   *      GNU General Public License for more details.
24   *   *
25   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
26   *      along with this program; if not, write to the xvid_free Software   *  along with this program; if not, write to the Free Software
27   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28   *   *
29   *************************************************************************/   *************************************************************************/
30    
# Line 32  Line 32 
32   *   *
33   *      History:   *      History:
34   *   *
35     *  08.05.2002  add low_delay support for B_VOP decode
36     *              MinChen <chenm001@163.com>
37     *  05.05.2002  fix some B-frame decode problem
38     *  02.05.2002  add B-frame decode support(have some problem);
39     *              MinChen <chenm001@163.com>
40     *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
41   *  29.03.2002  interlacing fix - compensated block wasn't being used when   *  29.03.2002  interlacing fix - compensated block wasn't being used when
42   *              reconstructing blocks, thus artifacts   *              reconstructing blocks, thus artifacts
43   *              interlacing speedup - used transfers to re-interlace   *              interlacing speedup - used transfers to re-interlace
44   *              interlaced decoding should be as fast as progressive now   *              interlaced decoding should be as fast as progressive now
45   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
46   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block
47   *      22.12.2001      block based interpolation   *  22.12.2001  lock based interpolation
48   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>   *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
49   *   *
50     *  $Id$
51     *
52   *************************************************************************/   *************************************************************************/
53    
54  #include <stdlib.h>  #include <stdlib.h>
55  #include <string.h>  // memset  #include <string.h>
56    
57  #include "xvid.h"  #include "xvid.h"
58  #include "portab.h"  #include "portab.h"
# Line 95  Line 103 
103                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
104          }          }
105    
106          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height))
107            {
108                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
109                    xvid_free(dec);
110                    return XVID_ERR_MEMORY;
111            }
112            // add by chenm001 <chenm001@163.com>
113            // for support B-frame to reference last 2 frame
114            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height))
115            {
116                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
117                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
118                    xvid_free(dec);
119                    return XVID_ERR_MEMORY;
120            }
121            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height))
122          {          {
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);
125                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
126                  xvid_free(dec);                  xvid_free(dec);
127                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
128          }          }
# Line 106  Line 131 
131          if (dec->mbs == NULL)          if (dec->mbs == NULL)
132          {          {
133                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
134                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
135                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
136                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
137                    xvid_free(dec);
138                    return XVID_ERR_MEMORY;
139            }
140            // add by chenm001 <chenm001@163.com>
141            // for skip MB flag
142            dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);
143            if (dec->last_mbs == NULL)
144            {
145                    xvid_free(dec->mbs);
146                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
147                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
148                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
149                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
150                  xvid_free(dec);                  xvid_free(dec);
151                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
152          }          }
153    
154          init_timer();          init_timer();
155    
156            // add by chenm001 <chenm001@163.com>
157            // for support B-frame to save reference frame's time
158            dec->frames = -1;
159            dec->time = dec->time_base = dec->last_time_base = 0;
160    
161          return XVID_ERR_OK;          return XVID_ERR_OK;
162  }  }
163    
164    
165  int decoder_destroy(DECODER * dec)  int decoder_destroy(DECODER * dec)
166  {  {
167            xvid_free(dec->last_mbs);
168          xvid_free(dec->mbs);          xvid_free(dec->mbs);
169          image_destroy(&dec->refn, dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
170            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
171            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
172          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
173          xvid_free(dec);          xvid_free(dec);
174    
# Line 135  Line 184 
184  };  };
185    
186    
187    
188    
189  // decode an intra macroblock  // decode an intra macroblock
190    
191  void decoder_mbintra(DECODER * dec,  void decoder_mbintra(DECODER * dec,
# Line 226  Line 277 
277                  stop_idct_timer();                  stop_idct_timer();
278          }          }
279    
280          if (pMB->field_dct)          if (dec->interlacing && pMB->field_dct)
281          {          {
282                  next_block = stride;                  next_block = stride;
283                  stride *= 2;                  stride *= 2;
# Line 299  Line 350 
350          }          }
351    
352          start_timer();          start_timer();
353          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);
354          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);
355          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);
356          interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);
357          interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);
358          interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, rounding);
359          stop_comp_timer();          stop_comp_timer();
360    
361          for (i = 0; i < 6; i++)          for (i = 0; i < 6; i++)
# Line 334  Line 385 
385                  }                  }
386          }          }
387    
388          if (pMB->field_dct)          if (dec->interlacing && pMB->field_dct)
389          {          {
390                  next_block = stride;                  next_block = stride;
391                  stride *= 2;                  stride *= 2;
# Line 425  Line 476 
476          int range = (64 * scale_fac);          int range = (64 * scale_fac);
477    
478          VECTOR pmv[4];          VECTOR pmv[4];
479          uint32_t psad[4];          int32_t psad[4];
480    
481          int mv_x, mv_y;          int mv_x, mv_y;
482          int pmv_x, pmv_y;          int pmv_x, pmv_y;
# Line 471  Line 522 
522    
523          uint32_t x, y;          uint32_t x, y;
524    
         image_swap(&dec->cur, &dec->refn);  
   
525          start_timer();          start_timer();
526          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
527          stop_edges_timer();          stop_edges_timer();
528    
529          for (y = 0; y < dec->mb_height; y++)          for (y = 0; y < dec->mb_height; y++)
# Line 483  Line 532 
532                  {                  {
533                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
534    
535                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))                 // not_coded
536                            if (!(BitstreamGetBit(bs)))                     // not_coded
537                          {                          {
538                                  uint32_t mcbpc;                                  uint32_t mcbpc;
539                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 580  Line 630 
630                          }                          }
631                          else    // not coded                          else    // not coded
632                          {                          {
633                                    //DEBUG2("P-frame MB at (X,Y)=",x,y);
634                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
635                                  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;
636                                  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;
# Line 590  Line 640 
640                                  start_timer();                                  start_timer();
641    
642                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
643                                                   dec->refn.y + (16*y)*dec->edged_width + (16*x),                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
644                                                   dec->edged_width);                                                   dec->edged_width);
645    
646                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),
647                                                   dec->refn.y + (16*y)*dec->edged_width + (16*x+8),                                                   dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8),
648                                                   dec->edged_width);                                                   dec->edged_width);
649    
650                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),
651                                                   dec->refn.y + (16*y+8)*dec->edged_width + (16*x),                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x),
652                                                   dec->edged_width);                                                   dec->edged_width);
653    
654                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),
655                                                   dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),                                                   dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8),
656                                                   dec->edged_width);                                                   dec->edged_width);
657    
658                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
659                                                   dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                                                   dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
660                                                   dec->edged_width/2);                                                   dec->edged_width/2);
661    
662                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
663                                                   dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                                                   dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
664                                                   dec->edged_width/2);                                                   dec->edged_width/2);
665    
666                                  stop_transfer_timer();                                  stop_transfer_timer();
# Line 619  Line 669 
669          }          }
670  }  }
671    
672    
673    // add by MinChen <chenm001@163.com>
674    // decode B-frame motion vector
675    void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv)
676    {
677            int scale_fac = 1 << (fcode - 1);
678            int high = (32 * scale_fac) - 1;
679            int low = ((-32) * scale_fac);
680            int range = (64 * scale_fac);
681    
682            int mv_x, mv_y;
683            int pmv_x, pmv_y;
684    
685            pmv_x = pmv.x;
686            pmv_y = pmv.y;
687    
688            mv_x = get_mv(bs, fcode);
689            mv_y = get_mv(bs, fcode);
690    
691            mv_x += pmv_x;
692            mv_y += pmv_y;
693    
694            if (mv_x < low)
695            {
696                    mv_x += range;
697            }
698            else if (mv_x > high)
699            {
700                    mv_x -= range;
701            }
702    
703            if (mv_y < low)
704            {
705                    mv_y += range;
706            }
707            else if (mv_y > high)
708            {
709                    mv_y -= range;
710            }
711    
712            mv->x = mv_x;
713            mv->y = mv_y;
714    }
715    
716    
717    // add by MinChen <chenm001@163.com>
718    // decode an B-frame forward & backward inter macroblock
719    void decoder_bf_mbinter(DECODER * dec,
720                         const MACROBLOCK * pMB,
721                         const uint32_t x_pos,
722                         const uint32_t y_pos,
723                         const uint32_t cbp,
724                         Bitstream * bs,
725                         const uint32_t quant,
726                             const uint8_t ref)
727    {
728    
729            DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
730            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
731    
732            uint32_t stride = dec->edged_width;
733            uint32_t stride2 = stride / 2;
734            uint32_t next_block = stride * 8;
735            uint32_t i;
736            uint32_t iQuant = pMB->quant;
737            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
738            int uv_dx, uv_dy;
739    
740            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
741            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
742            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
743    
744            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
745            {
746                    uv_dx = pMB->mvs[0].x;
747                    uv_dy = pMB->mvs[0].y;
748    
749                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
750                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
751            }
752            else
753            {
754                    int sum;
755                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
756                    uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
757    
758                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
759                    uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
760            }
761    
762            start_timer();
763            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);
764            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);
765            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);
766            interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  0);
767            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
768            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
769            stop_comp_timer();
770    
771            for (i = 0; i < 6; i++)
772            {
773                    if (cbp & (1 << (5-i)))                 // coded
774                    {
775                            memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
776    
777                            start_timer();
778                            get_inter_block(bs, &block[i*64]);
779                            stop_coding_timer();
780    
781                            start_timer();
782                            if (dec->quant_type == 0)
783                            {
784                                    dequant_inter(&data[i*64], &block[i*64], iQuant);
785                            }
786                            else
787                            {
788                                    dequant4_inter(&data[i*64], &block[i*64], iQuant);
789                            }
790                            stop_iquant_timer();
791    
792                            start_timer();
793                            idct(&data[i*64]);
794                            stop_idct_timer();
795                    }
796            }
797    
798            if (dec->interlacing && pMB->field_dct)
799            {
800                    next_block = stride;
801                    stride *= 2;
802            }
803    
804            start_timer();
805            if (cbp & 32)
806                    transfer_16to8add(pY_Cur,                  &data[0*64], stride);
807            if (cbp & 16)
808                    transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);
809            if (cbp & 8)
810                    transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);
811            if (cbp & 4)
812                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);
813            if (cbp & 2)
814                    transfer_16to8add(pU_Cur,                  &data[4*64], stride2);
815            if (cbp & 1)
816                    transfer_16to8add(pV_Cur,                  &data[5*64], stride2);
817            stop_transfer_timer();
818    }
819    
820    
821    // add by MinChen <chenm001@163.com>
822    // decode an B-frame direct &  inter macroblock
823    void decoder_bf_interpolate_mbinter(DECODER * dec,
824                             IMAGE forward, IMAGE backward,
825                         const MACROBLOCK * pMB,
826                         const uint32_t x_pos,
827                         const uint32_t y_pos,
828                         const uint32_t cbp,
829                         Bitstream * bs )
830    {
831    
832            DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
833            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
834    
835            uint32_t        stride = dec->edged_width;
836            uint32_t        stride2 = stride / 2;
837            uint32_t        next_block = stride * 8;
838            uint32_t        iQuant = pMB->quant;
839            int                     uv_dx, uv_dy;
840            int                     b_uv_dx, b_uv_dy;
841            uint32_t        i;
842            uint8_t         *pY_Cur, *pU_Cur, *pV_Cur;
843    
844            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
845            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
846            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
847    
848            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
849            {
850                    uv_dx = pMB->mvs[0].x;
851                    uv_dy = pMB->mvs[0].y;
852    
853                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
854                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
855    
856                    b_uv_dx = pMB->b_mvs[0].x;
857                    b_uv_dy = pMB->b_mvs[0].y;
858    
859                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
860                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
861            }
862            else
863            {
864                    int sum;
865                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
866                    uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
867    
868                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
869                    uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
870    
871                    sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
872                    b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
873    
874                    sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
875                    b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
876            }
877    
878    
879            start_timer();
880            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);
881            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);
882            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);
883            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  0);
884            interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
885            interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
886    
887    
888            interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos,     16*y_pos    , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride,  0);
889            interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos    , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,  0);
890            interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos,     16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride,  0);
891            interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride,  0);
892            interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);
893            interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);
894    
895            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos    , stride);
896            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos    , stride);
897            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos + 8, stride);
898            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride);
899            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos,      8*y_pos,      stride2);
900            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos,      8*y_pos,      stride2);
901    
902            stop_comp_timer();
903    
904            for (i = 0; i < 6; i++)
905            {
906                    if (cbp & (1 << (5-i)))                 // coded
907                    {
908                            memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
909    
910                            start_timer();
911                            get_inter_block(bs, &block[i*64]);
912                            stop_coding_timer();
913    
914                            start_timer();
915                            if (dec->quant_type == 0)
916                            {
917                                    dequant_inter(&data[i*64], &block[i*64], iQuant);
918                            }
919                            else
920                            {
921                                    dequant4_inter(&data[i*64], &block[i*64], iQuant);
922                            }
923                            stop_iquant_timer();
924    
925                            start_timer();
926                            idct(&data[i*64]);
927                            stop_idct_timer();
928                    }
929            }
930    
931            if (dec->interlacing && pMB->field_dct)
932            {
933                    next_block = stride;
934                    stride *= 2;
935            }
936    
937            start_timer();
938            if (cbp & 32)
939                    transfer_16to8add(pY_Cur,                  &data[0*64], stride);
940            if (cbp & 16)
941                    transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);
942            if (cbp & 8)
943                    transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);
944            if (cbp & 4)
945                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);
946            if (cbp & 2)
947                    transfer_16to8add(pU_Cur,                  &data[4*64], stride2);
948            if (cbp & 1)
949                    transfer_16to8add(pV_Cur,                  &data[5*64], stride2);
950            stop_transfer_timer();
951    }
952    
953    
954    // add by MinChen <chenm001@163.com>
955    // for decode B-frame dbquant
956    int32_t __inline get_dbquant(Bitstream * bs)
957    {
958            if (!BitstreamGetBit(bs))               // '0'
959                    return(0);
960            else if (!BitstreamGetBit(bs))  // '10'
961                    return(-2);
962            else
963                    return(2);                                      // '11'
964    }
965    
966    // add by MinChen <chenm001@163.com>
967    // for decode B-frame mb_type
968    // bit   ret_value
969    // 1        0
970    // 01       1
971    // 001      2
972    // 0001     3
973    int32_t __inline get_mbtype(Bitstream * bs)
974    {
975            int32_t mb_type;
976            for(mb_type=0;mb_type<=3;mb_type++){
977                    if  (BitstreamGetBit(bs))
978                            break;
979            }
980    
981            if (mb_type<=3)
982                    return(mb_type);
983            else
984                    return(-1);
985    }
986    
987    void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward)
988    {
989    
990            uint32_t        x, y;
991            VECTOR          mv, zeromv;
992    
993            start_timer();
994            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
995            //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
996            stop_edges_timer();
997    
998    
999            for (y = 0; y < dec->mb_height; y++)
1000            {
1001                    // Initialize Pred Motion Vector
1002                    dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;
1003                    for (x = 0; x < dec->mb_width; x++)
1004                    {
1005                            MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
1006                            MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];
1007    
1008                            mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0;
1009    
1010                            // the last P_VOP is skip macroblock ?
1011                            if (last_mb->mode == MODE_NOT_CODED){
1012                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1013                                    mb->mb_type = MODE_FORWARD;
1014                                    mb->cbp = 0;
1015                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1016                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1017                                    mb->quant = 8;
1018    
1019                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1020                                    continue;
1021                            }
1022    
1023                            //t=BitstreamShowBits(bs,32);
1024    
1025                            if (!BitstreamGetBit(bs)){      // modb=='0'
1026                                    const uint8_t modb2=BitstreamGetBit(bs);
1027    
1028                                    mb->mb_type = get_mbtype(bs);
1029    
1030                                    if (!modb2){    // modb=='00'
1031                                            mb->cbp = BitstreamGetBits(bs,6);
1032                                    } else {
1033                                            mb->cbp = 0;
1034                                    }
1035                                    if (mb->mb_type && mb->cbp){
1036                                            quant += get_dbquant(bs);
1037    
1038                                            if (quant > 31)
1039                                            {
1040                                                    quant = 31;
1041                                            }
1042                                            else if (mb->quant < 1)
1043                                            {
1044                                                    quant = 1;
1045                                            }
1046                                    } else {
1047                                            quant = 8;
1048                                    }
1049                                    mb->quant = quant;
1050                            } else {
1051                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1052                                    mb->cbp = 0;
1053                            }
1054    
1055                            mb->mode = MODE_INTER;
1056                            //DEBUG1("Switch bm_type=",mb->mb_type);
1057    
1058                            switch(mb->mb_type)
1059                            {
1060                            case MODE_DIRECT:
1061                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);
1062    
1063                            case MODE_DIRECT_NONE_MV:
1064                                    {       // Because this file is a C file not C++ so I use '{' to define var
1065                                            const int64_t   TRB=dec->time_pp-dec->time_bp,
1066                                                                            TRD=dec->time_pp;
1067                                            int i;
1068                                            for(i=0;i<4;i++){
1069                                                    mb->mvs[i].x    = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x);
1070                                                    mb->b_mvs[i].x  = (int32_t)((mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x);
1071                                                    mb->mvs[i].y    = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y);
1072                                                    mb->b_mvs[i].y  = (int32_t)((mb->mvs[0].y==0)?((TRB-TRD)*last_mb->mvs[i].y)/TRD:mb->mvs[i].y-last_mb->mvs[i].y);
1073                                            }
1074                                            //DEBUG("B-frame Direct!\n");
1075                                    }
1076                                    mb->mode = MODE_INTER4V;
1077                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);
1078                                    break;
1079    
1080                            case MODE_INTERPOLATE:
1081                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);
1082                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1083                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1084    
1085                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1086                                    dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x;
1087                                    dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y;
1088    
1089                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);
1090                                    //DEBUG("B-frame Bidir!\n");
1091                                    break;
1092    
1093                            case MODE_BACKWARD:
1094                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv);
1095                                    dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1096                                    dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1097    
1098                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1099                                    //DEBUG("B-frame Backward!\n");
1100                                    break;
1101    
1102                            case MODE_FORWARD:
1103                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);
1104                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1105                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1106    
1107                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1108                                    //DEBUG("B-frame Forward!\n");
1109                                    break;
1110    
1111                            default:
1112                                    DEBUG1("Not support B-frame mb_type =",mb->mb_type);
1113                            }
1114    
1115                    }       // end of FOR
1116            }
1117    }
1118    
1119    // swap two MACROBLOCK array
1120    void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2)
1121    {
1122            MACROBLOCK *temp=*mb1;
1123            *mb1=*mb2;
1124            *mb2=temp;
1125    }
1126    
1127  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)
1128  {  {
1129    
1130          Bitstream bs;          Bitstream bs;
1131          uint32_t rounding;          uint32_t rounding;
1132          uint32_t quant;          uint32_t quant;
1133          uint32_t fcode;          uint32_t fcode_forward;
1134            uint32_t fcode_backward;
1135          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1136            uint32_t vop_type;
1137    
1138          start_global_timer();          start_global_timer();
1139    
1140          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1141    
1142          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          // add by chenm001 <chenm001@163.com>
1143            // for support B-frame to reference last 2 frame
1144            dec->frames++;
1145            vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold);
1146    
1147            dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0;          // init pred vector to 0
1148    
1149            switch (vop_type)
1150          {          {
1151          case P_VOP :          case P_VOP :
1152                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold);
1153                    DEBUG1("P_VOP  Time=",dec->time);
1154                  break;                  break;
1155    
1156          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1157                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1158                    DEBUG1("I_VOP  Time=",dec->time);
1159                  break;                  break;
1160    
1161          case B_VOP :    // ignore          case B_VOP :
1162    #ifdef BFRAMES
1163                    if (dec->time_pp > dec->time_bp){
1164                            DEBUG1("B_VOP  Time=",dec->time);
1165                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1166                    } else {
1167                            DEBUG("broken B-frame!");
1168                    }
1169    #endif
1170                  break;                  break;
1171    
1172          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
# Line 655  Line 1178 
1178    
1179          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1180    
1181    #ifdef BFRAMES
1182            // test if no B_VOP
1183            if (dec->low_delay){
1184    #endif
1185                image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1186                                         frame->image, frame->stride, frame->colorspace);
1187    #ifdef BFRAMES
1188            } else {
1189                    if (dec->frames >= 1){
1190          start_timer();          start_timer();
1191                            if ((vop_type == I_VOP || vop_type == P_VOP))
1192                            {
1193                                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1194                                                     frame->image, frame->stride, frame->colorspace);
1195                            } else if (vop_type == B_VOP) {
1196          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1197                       frame->image, frame->stride, frame->colorspace);                       frame->image, frame->stride, frame->colorspace);
1198                            }
1199          stop_conv_timer();          stop_conv_timer();
1200                    }
1201            }
1202    #endif
1203    
1204            if (vop_type==I_VOP || vop_type==P_VOP){
1205                    image_swap(&dec->refn[0], &dec->refn[1]);
1206                    image_swap(&dec->cur, &dec->refn[0]);
1207                    // swap MACROBLOCK
1208                    if (dec->low_delay && vop_type==P_VOP)
1209                            mb_swap(&dec->mbs, &dec->last_mbs);
1210            }
1211    
1212          emms();          emms();
1213    
1214          stop_global_timer();          stop_global_timer();
1215    
1216          return XVID_ERR_OK;          return XVID_ERR_OK;
   
1217  }  }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.18

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