[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.11, Tue Apr 23 00:04:03 2002 UTC revision 1.16, Mon May 6 03:58:09 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     *  02.05.2002  add B-frame decode support(have some problem);
36     *              MinChen <chenm001@163.com>
37   *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>   *  22.04.2002  add some B-frame decode support;  chenm001 <chenm001@163.com>
38   *  29.03.2002  interlacing fix - compensated block wasn't being used when   *  29.03.2002  interlacing fix - compensated block wasn't being used when
39   *              reconstructing blocks, thus artifacts   *              reconstructing blocks, thus artifacts
# Line 39  Line 41 
41   *              interlaced decoding should be as fast as progressive now   *              interlaced decoding should be as fast as progressive now
42   *  26.03.2002  interlacing support - moved transfers outside decode loop   *  26.03.2002  interlacing support - moved transfers outside decode loop
43   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block   *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block
44   *      22.12.2001      block based interpolation   *  22.12.2001  lock based interpolation
45   *      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>
46   *   *
47     *  $Id$
48     *
49   *************************************************************************/   *************************************************************************/
50    
51  #include <stdlib.h>  #include <stdlib.h>
52  #include <string.h>  // memset  #include <string.h>
53    
54  #include "xvid.h"  #include "xvid.h"
55  #include "portab.h"  #include "portab.h"
# Line 111  Line 115 
115                  xvid_free(dec);                  xvid_free(dec);
116                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
117          }          }
118            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height))
119            {
120                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
121                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
122                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
123                    xvid_free(dec);
124                    return XVID_ERR_MEMORY;
125            }
126    
127          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);
128          if (dec->mbs == NULL)          if (dec->mbs == NULL)
129          {          {
130                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
131                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
132                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
133                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
134                    xvid_free(dec);
135                    return XVID_ERR_MEMORY;
136            }
137            // add by chenm001 <chenm001@163.com>
138            // for skip MB flag
139            dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);
140            if (dec->last_mbs == NULL)
141            {
142                    xvid_free(dec->mbs);
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
147                  xvid_free(dec);                  xvid_free(dec);
148                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
149          }          }
150    
151          init_timer();          init_timer();
152    
153            // add by chenm001 <chenm001@163.com>
154            // for support B-frame to save reference frame's time
155            dec->frames = -1;
156            dec->time = dec->time_base = dec->last_time_base = 0;
157    
158          return XVID_ERR_OK;          return XVID_ERR_OK;
159  }  }
160    
161    
162  int decoder_destroy(DECODER * dec)  int decoder_destroy(DECODER * dec)
163  {  {
164            xvid_free(dec->last_mbs);
165          xvid_free(dec->mbs);          xvid_free(dec->mbs);
166          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
167            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
168            image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
169          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
170          xvid_free(dec);          xvid_free(dec);
171    
# Line 145  Line 181 
181  };  };
182    
183    
184    
185    
186  // decode an intra macroblock  // decode an intra macroblock
187    
188  void decoder_mbintra(DECODER * dec,  void decoder_mbintra(DECODER * dec,
# Line 435  Line 473 
473          int range = (64 * scale_fac);          int range = (64 * scale_fac);
474    
475          VECTOR pmv[4];          VECTOR pmv[4];
476          uint32_t psad[4];          int32_t psad[4];
477    
478          int mv_x, mv_y;          int mv_x, mv_y;
479          int pmv_x, pmv_y;          int pmv_x, pmv_y;
# Line 491  Line 529 
529                  {                  {
530                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
531    
532                          if (!BitstreamGetBit(bs))                       // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))                 // not_coded
533                            if (!(BitstreamGetBit(bs)))                     // not_coded
534                          {                          {
535                                  uint32_t mcbpc;                                  uint32_t mcbpc;
536                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 588  Line 627 
627                          }                          }
628                          else    // not coded                          else    // not coded
629                          {                          {
630                                    //DEBUG2("P-frame MB at (X,Y)=",x,y);
631                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
632                                  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;
633                                  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 627  Line 666 
666          }          }
667  }  }
668    
669    
670    // add by MinChen <chenm001@163.com>
671    // decode B-frame motion vector
672    void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv)
673    {
674            int scale_fac = 1 << (fcode - 1);
675            int high = (32 * scale_fac) - 1;
676            int low = ((-32) * scale_fac);
677            int range = (64 * scale_fac);
678    
679            int mv_x, mv_y;
680            int pmv_x, pmv_y;
681    
682            pmv_x = pmv.x;
683            pmv_y = pmv.y;
684    
685            mv_x = get_mv(bs, fcode);
686            mv_y = get_mv(bs, fcode);
687    
688            mv_x += pmv_x;
689            mv_y += pmv_y;
690    
691            if (mv_x < low)
692            {
693                    mv_x += range;
694            }
695            else if (mv_x > high)
696            {
697                    mv_x -= range;
698            }
699    
700            if (mv_y < low)
701            {
702                    mv_y += range;
703            }
704            else if (mv_y > high)
705            {
706                    mv_y -= range;
707            }
708    
709            mv->x = mv_x;
710            mv->y = mv_y;
711    }
712    
713    
714    // add by MinChen <chenm001@163.com>
715    // decode an B-frame forward & backward inter macroblock
716    void decoder_bf_mbinter(DECODER * dec,
717                         const MACROBLOCK * pMB,
718                         const uint32_t x_pos,
719                         const uint32_t y_pos,
720                         const uint32_t cbp,
721                         Bitstream * bs,
722                         const uint32_t quant,
723                             const uint8_t ref)
724    {
725    
726            DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
727            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
728    
729            uint32_t stride = dec->edged_width;
730            uint32_t stride2 = stride / 2;
731            uint32_t next_block = stride * 8;
732            uint32_t i;
733            uint32_t iQuant = pMB->quant;
734            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
735            int uv_dx, uv_dy;
736    
737            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
738            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
739            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
740    
741            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
742            {
743                    uv_dx = pMB->mvs[0].x;
744                    uv_dy = pMB->mvs[0].y;
745    
746                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
747                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
748            }
749            else
750            {
751                    int sum;
752                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
753                    uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
754    
755                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
756                    uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
757            }
758    
759            start_timer();
760            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);
761            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);
762            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);
763            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);
764            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
765            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
766            stop_comp_timer();
767    
768            for (i = 0; i < 6; i++)
769            {
770                    if (cbp & (1 << (5-i)))                 // coded
771                    {
772                            memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
773    
774                            start_timer();
775                            get_inter_block(bs, &block[i*64]);
776                            stop_coding_timer();
777    
778                            start_timer();
779                            if (dec->quant_type == 0)
780                            {
781                                    dequant_inter(&data[i*64], &block[i*64], iQuant);
782                            }
783                            else
784                            {
785                                    dequant4_inter(&data[i*64], &block[i*64], iQuant);
786                            }
787                            stop_iquant_timer();
788    
789                            start_timer();
790                            idct(&data[i*64]);
791                            stop_idct_timer();
792                    }
793            }
794    
795            if (dec->interlacing && pMB->field_dct)
796            {
797                    next_block = stride;
798                    stride *= 2;
799            }
800    
801            start_timer();
802            if (cbp & 32)
803                    transfer_16to8add(pY_Cur,                  &data[0*64], stride);
804            if (cbp & 16)
805                    transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);
806            if (cbp & 8)
807                    transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);
808            if (cbp & 4)
809                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);
810            if (cbp & 2)
811                    transfer_16to8add(pU_Cur,                  &data[4*64], stride2);
812            if (cbp & 1)
813                    transfer_16to8add(pV_Cur,                  &data[5*64], stride2);
814            stop_transfer_timer();
815    }
816    
817    
818    // add by MinChen <chenm001@163.com>
819    // decode an B-frame direct &  inter macroblock
820    void decoder_bf_interpolate_mbinter(DECODER * dec,
821                             IMAGE forward, IMAGE backward,
822                         const MACROBLOCK * pMB,
823                         const uint32_t x_pos,
824                         const uint32_t y_pos,
825                         const uint32_t cbp,
826                         Bitstream * bs )
827    {
828    
829            DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE);
830            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
831    
832            uint32_t        stride = dec->edged_width;
833            uint32_t        stride2 = stride / 2;
834            uint32_t        next_block = stride * 8;
835            uint32_t        iQuant = pMB->quant;
836            int                     uv_dx, uv_dy;
837            int                     b_uv_dx, b_uv_dy;
838            uint32_t        i;
839            uint8_t         *pY_Cur, *pU_Cur, *pV_Cur;
840    
841            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
842            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
843            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
844    
845            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
846            {
847                    uv_dx = pMB->mvs[0].x;
848                    uv_dy = pMB->mvs[0].y;
849    
850                    uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
851                    uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
852    
853                    b_uv_dx = pMB->b_mvs[0].x;
854                    b_uv_dy = pMB->b_mvs[0].y;
855    
856                    b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
857                    b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
858            }
859            else
860            {
861                    int sum;
862                    sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
863                    uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
864    
865                    sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
866                    uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
867    
868                    sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
869                    b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
870    
871                    sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
872                    b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
873            }
874    
875    
876            start_timer();
877            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos    , pMB->mvs[0].x, pMB->mvs[0].y, stride,  0);
878            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos    , pMB->mvs[1].x, pMB->mvs[1].y, stride,  0);
879            interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos,     16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,  0);
880            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);
881            interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
882            interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos,      8*y_pos,      uv_dx,         uv_dy,         stride2, 0);
883    
884    
885            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);
886            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);
887            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);
888            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);
889            interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);
890            interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos,      8*y_pos,      b_uv_dx,         b_uv_dy,         stride2, 0);
891    
892            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos    , stride);
893            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos    , stride);
894            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos,     16*y_pos + 8, stride);
895            interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride);
896            interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos,      8*y_pos,      stride2);
897            interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos,      8*y_pos,      stride2);
898    
899            stop_comp_timer();
900    
901            for (i = 0; i < 6; i++)
902            {
903                    if (cbp & (1 << (5-i)))                 // coded
904                    {
905                            memset(&block[i*64], 0, 64 * sizeof(int16_t));          // clear
906    
907                            start_timer();
908                            get_inter_block(bs, &block[i*64]);
909                            stop_coding_timer();
910    
911                            start_timer();
912                            if (dec->quant_type == 0)
913                            {
914                                    dequant_inter(&data[i*64], &block[i*64], iQuant);
915                            }
916                            else
917                            {
918                                    dequant4_inter(&data[i*64], &block[i*64], iQuant);
919                            }
920                            stop_iquant_timer();
921    
922                            start_timer();
923                            idct(&data[i*64]);
924                            stop_idct_timer();
925                    }
926            }
927    
928            if (dec->interlacing && pMB->field_dct)
929            {
930                    next_block = stride;
931                    stride *= 2;
932            }
933    
934            start_timer();
935            if (cbp & 32)
936                    transfer_16to8add(pY_Cur,                  &data[0*64], stride);
937            if (cbp & 16)
938                    transfer_16to8add(pY_Cur + 8,              &data[1*64], stride);
939            if (cbp & 8)
940                    transfer_16to8add(pY_Cur + next_block,     &data[2*64], stride);
941            if (cbp & 4)
942                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride);
943            if (cbp & 2)
944                    transfer_16to8add(pU_Cur,                  &data[4*64], stride2);
945            if (cbp & 1)
946                    transfer_16to8add(pV_Cur,                  &data[5*64], stride2);
947            stop_transfer_timer();
948    }
949    
950    
951    // add by MinChen <chenm001@163.com>
952    // for decode B-frame dbquant
953    int32_t __inline get_dbquant(Bitstream * bs)
954    {
955            if (!BitstreamGetBit(bs))               // '0'
956                    return(0);
957            else if (!BitstreamGetBit(bs))  // '10'
958                    return(-2);
959            else
960                    return(2);                                      // '11'
961    }
962    
963    // add by MinChen <chenm001@163.com>
964    // for decode B-frame mb_type
965    // bit   ret_value
966    // 1        0
967    // 01       1
968    // 001      2
969    // 0001     3
970    int32_t __inline get_mbtype(Bitstream * bs)
971    {
972            int32_t mb_type;
973            for(mb_type=0;mb_type<=3;mb_type++){
974                    if  (BitstreamGetBit(bs))
975                            break;
976            }
977    
978            if (mb_type<=3)
979                    return(mb_type);
980            else
981                    return(-1);
982    }
983    
984    void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward)
985    {
986    
987            uint32_t        x, y;
988            VECTOR          mv, zeromv;
989    
990            start_timer();
991            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
992            //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);
993            stop_edges_timer();
994    
995    
996            for (y = 0; y < dec->mb_height; y++)
997            {
998                    // Initialize Pred Motion Vector
999                    dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;
1000                    for (x = 0; x < dec->mb_width; x++)
1001                    {
1002                            MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
1003                            MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x];
1004    
1005                            mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0;
1006    
1007                            // the last P_VOP is skip macroblock ?
1008                            if (last_mb->mode == MODE_NOT_CODED){
1009                                    //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1010                                    mb->mb_type = MODE_FORWARD;
1011                                    mb->cbp = 0;
1012                                    mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1013                                    mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1014                                    mb->quant = 8;
1015    
1016                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1017                                    continue;
1018                            }
1019    
1020                            //t=BitstreamShowBits(bs,32);
1021    
1022                            if (!BitstreamGetBit(bs)){      // modb=='0'
1023                                    const uint8_t modb2=BitstreamGetBit(bs);
1024    
1025                                    mb->mb_type = get_mbtype(bs);
1026    
1027                                    if (!modb2){    // modb=='00'
1028                                            mb->cbp = BitstreamGetBits(bs,6);
1029                                    } else {
1030                                            mb->cbp = 0;
1031                                    }
1032                                    if (mb->mb_type && mb->cbp){
1033                                            quant += get_dbquant(bs);
1034    
1035                                            if (quant > 31)
1036                                            {
1037                                                    quant = 31;
1038                                            }
1039                                            else if (mb->quant < 1)
1040                                            {
1041                                                    quant = 1;
1042                                            }
1043                                    } else {
1044                                            quant = 8;
1045                                    }
1046                                    mb->quant = quant;
1047                            } else {
1048                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1049                                    mb->cbp = 0;
1050                            }
1051    
1052                            mb->mode = MODE_INTER;
1053                            //DEBUG1("Switch bm_type=",mb->mb_type);
1054    
1055                            switch(mb->mb_type)
1056                            {
1057                            case MODE_DIRECT:
1058                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);
1059    
1060                            case MODE_DIRECT_NONE_MV:
1061                                    {       // Because this file is a C file not C++ so I use '{' to define var
1062                                            const int64_t   TRB=dec->time_pp-dec->time_bp,
1063                                                                            TRD=dec->time_pp;
1064                                            int i;
1065                                            for(i=0;i<4;i++){
1066                                                    mb->mvs[i].x    = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x);
1067                                                    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);
1068                                                    mb->mvs[i].y    = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y);
1069                                                    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);
1070                                            }
1071                                            //DEBUG("B-frame Direct!\n");
1072                                    }
1073                                    mb->mode = MODE_INTER4V;
1074                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);
1075                                    break;
1076    
1077                            case MODE_INTERPOLATE:
1078                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);
1079                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1080                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1081    
1082                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1083                                    dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x;
1084                                    dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y;
1085    
1086                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs);
1087                                    //DEBUG("B-frame Bidir!\n");
1088                                    break;
1089    
1090                            case MODE_BACKWARD:
1091                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv);
1092                                    dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1093                                    dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1094    
1095                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1096                                    //DEBUG("B-frame Backward!\n");
1097                                    break;
1098    
1099                            case MODE_FORWARD:
1100                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv);
1101                                    dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1102                                    dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1103    
1104                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1105                                    //DEBUG("B-frame Forward!\n");
1106                                    break;
1107    
1108                            default:
1109                                    DEBUG1("Not support B-frame mb_type =",mb->mb_type);
1110                            }
1111    
1112                    }       // end of FOR
1113            }
1114    }
1115    
1116    // swap two MACROBLOCK array
1117    void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2)
1118    {
1119            MACROBLOCK *temp=*mb1;
1120            *mb1=*mb2;
1121            *mb2=temp;
1122    }
1123    
1124  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)
1125  {  {
1126    
1127          Bitstream bs;          Bitstream bs;
1128          uint32_t rounding;          uint32_t rounding;
1129          uint32_t quant;          uint32_t quant;
1130          uint32_t fcode;          uint32_t fcode_forward;
1131            uint32_t fcode_backward;
1132          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1133          uint32_t vop_type;          uint32_t vop_type;
1134    
# Line 643  Line 1138 
1138    
1139          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
1140          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
1141          vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold);          dec->frames ++;
1142            vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold);
         if (vop_type==I_VOP || vop_type==P_VOP){  
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
         }  
1143    
1144            dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0;          // init pred vector to 0
1145          switch (vop_type)          switch (vop_type)
1146          {          {
1147          case P_VOP :          case P_VOP :
1148                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold);
1149                    DEBUG1("P_VOP  Time=",dec->time);
1150                  break;                  break;
1151    
1152          case I_VOP :          case I_VOP :
                 //DEBUG1("",intra_dc_threshold);  
1153                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1154                    DEBUG1("I_VOP  Time=",dec->time);
1155                  break;                  break;
1156    
1157          case B_VOP :    // ignore          case B_VOP :
1158                    if (dec->time_pp > dec->time_bp){
1159                            DEBUG1("B_VOP  Time=",dec->time);
1160                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1161                    } else {
1162                            DEBUG("broken B-frame!");
1163                    }
1164                  break;                  break;
1165    
1166          case N_VOP :    // vop not coded          case N_VOP :    // vop not coded
# Line 673  Line 1172 
1172    
1173          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1174    
1175            if (dec->frames >= 1){
1176          start_timer();          start_timer();
1177                    if ((vop_type == I_VOP || vop_type == P_VOP))
1178                    {
1179                            image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1180                                         frame->image, frame->stride, frame->colorspace);
1181                    } else if (vop_type == B_VOP) {
1182          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1183                       frame->image, frame->stride, frame->colorspace);                       frame->image, frame->stride, frame->colorspace);
1184                    }
1185          stop_conv_timer();          stop_conv_timer();
1186            }
1187            if (vop_type==I_VOP || vop_type==P_VOP){
1188                    image_swap(&dec->refn[0], &dec->refn[1]);
1189                    image_swap(&dec->cur, &dec->refn[0]);
1190                    // swap MACROBLOCK
1191                    if (vop_type==P_VOP)
1192                            mb_swap(&dec->mbs, &dec->last_mbs);
1193            }
1194    
1195          emms();          emms();
1196    
1197          stop_global_timer();          stop_global_timer();
1198    
1199          return XVID_ERR_OK;          return XVID_ERR_OK;
   
1200  }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.16

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