[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.23, Mon Dec 16 08:54:44 2002 UTC revision 1.47, Sat Feb 15 15:22:17 2003 UTC
# Line 59  Line 59 
59   *   *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  #include <string.h>
65    
# Line 68  Line 69 
69    
70  #include "xvid.h"  #include "xvid.h"
71  #include "portab.h"  #include "portab.h"
72    #include "global.h"
73    
74  #include "decoder.h"  #include "decoder.h"
75  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 103  Line 105 
105          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
106          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
107    
108            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
109    
110          if (dec->last_mbs)          if (dec->last_mbs)
111                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
112          if (dec->mbs)          if (dec->mbs)
# Line 152  Line 156 
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          }          }
158    
159            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
160                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
161                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
162                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
163                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
164                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
165                    xvid_free(dec);
166                    return XVID_ERR_MEMORY;
167            }
168    
169          dec->mbs =          dec->mbs =
170                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
171                                          CACHE_LINE);                                          CACHE_LINE);
# Line 210  Line 224 
224          image_null(&dec->tmp);          image_null(&dec->tmp);
225          image_null(&dec->qtmp);          image_null(&dec->qtmp);
226    
227    /* image based GMC */
228            image_null(&dec->gmc);
229    
230    
231          dec->mbs = NULL;          dec->mbs = NULL;
232          dec->last_mbs = NULL;          dec->last_mbs = NULL;
233    
# Line 236  Line 254 
254  {  {
255          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
256          xvid_free(dec->mbs);          xvid_free(dec->mbs);
257    
258            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */
259    
260          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
261          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
262          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
# Line 384  Line 405 
405    
406    
407    
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
408  // decode an inter macroblock  // decode an inter macroblock
409    
410  void  void
# Line 395  Line 412 
412                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
413                                  const uint32_t x_pos,                                  const uint32_t x_pos,
414                                  const uint32_t y_pos,                                  const uint32_t y_pos,
415                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
416                                  const uint32_t cbp,                                  const uint32_t cbp,
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
# Line 433  Line 450 
450          }          }
451    
452          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;  
453    
454                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
455                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
456    
457                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
458                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
# Line 609  Line 621 
621          stop_transfer_timer();          stop_transfer_timer();
622  }  }
623    
624    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
625    {
626            int length = 1 << (fcode+4);
627    
628    //      if (quarterpel) value *= 2;
629    
630            if (value < -length)
631                    return -length;
632            else if (value >= length)
633                    return length-1;
634            else return value;
635    }
636    
637    
638    static void
639    decoder_mbgmc(DECODER * dec,
640                                    MACROBLOCK * const pMB,
641                                    const uint32_t x_pos,
642                                    const uint32_t y_pos,
643                                    const uint32_t fcode,
644                                    const uint32_t cbp,
645                                    Bitstream * bs,
646                                    const uint32_t quant,
647                                    const uint32_t rounding,
648                                    const int reduced_resolution)   /* no reduced res support */
649    {
650    
651            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
652            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
653    
654            const uint32_t stride = dec->edged_width;
655            const uint32_t stride2 = stride / 2;
656            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
657            uint32_t i;
658            const uint32_t iQuant = pMB->quant;
659            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
660            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
661            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
662    
663            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
664    
665            start_timer();
666    
667    /* this is where the calculations are done */
668    
669            {
670                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
671                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
672    
673                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
674                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
675            }
676            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
677    
678    /*      transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
679            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
680            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
681    */
682    
683    
684            stop_transfer_timer();
685    
686            if (!cbp) return;
687    
688            for (i = 0; i < 6; i++) {
689                    int direction = dec->alternate_vertical_scan ? 2 : 0;
690    
691                    if (cbp & (1 << (5 - i)))       // coded
692                    {
693                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
694    
695                            start_timer();
696                            get_inter_block(bs, &block[i * 64], direction);
697                            stop_coding_timer();
698    
699                            start_timer();
700                            if (dec->quant_type == 0) {
701                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
702                            } else {
703                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
704                            }
705                            stop_iquant_timer();
706    
707                            start_timer();
708                            idct(&data[i * 64]);
709                            stop_idct_timer();
710                    }
711            }
712    
713    /* interlace + GMC is this possible ??? */
714    /*      if (dec->interlacing && pMB->field_dct) {
715                    next_block = stride;
716                    stride *= 2;
717            }
718    */
719            start_timer();
720            if (cbp & 32)
721                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
722            if (cbp & 16)
723                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
724            if (cbp & 8)
725                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
726            if (cbp & 4)
727                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
728            if (cbp & 2)
729                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
730            if (cbp & 1)
731                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
732            stop_transfer_timer();
733    }
734    
735    
736  void  void
737  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
# Line 716  Line 839 
839          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
840          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
841    
842          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);
843    
844          mv.x += pmv.x;          mv.x += pmv.x;
845          mv.y += pmv.y;          mv.y += pmv.y;
# Line 739  Line 862 
862    
863    
864    
 static __inline int gmc_sanitize(int value, int quarterpel, int fcode)  
 {  
         int length = 1 << (fcode+4);  
   
         if (quarterpel) value *= 2;  
   
         if (value < -length)  
                 return -length;  
         else if (value >= length)  
                 return length-1;  
         else return value;  
 }  
865    
866    
867  /* for P_VOP set gmc_mv to NULL */  /* for P_VOP set gmc_warp to NULL */
868  void  void
869  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
870                             Bitstream * bs,                             Bitstream * bs,
# Line 762  Line 873 
873                             int quant,                             int quant,
874                             int fcode,                             int fcode,
875                             int intra_dc_threshold,                             int intra_dc_threshold,
876                             VECTOR * gmc_mv)                             const WARPPOINTS *const gmc_warp)
877  {  {
878    
879          uint32_t x, y;          uint32_t x, y;
# Line 782  Line 893 
893                                     dec->width, dec->height);                                     dec->width, dec->height);
894          stop_edges_timer();          stop_edges_timer();
895    
896            if (gmc_warp)
897            {
898    
899                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
900                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
901                    {
902                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
903                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
904                                    dec->sprite_warping_points);
905                    }
906    
907                    generate_GMCparameters( dec->sprite_warping_points,
908                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
909                                    dec->width, dec->height, &dec->gmc_data);
910    
911    /* image warping is done block-based  in decoder_mbgmc(), now */
912    /*
913            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
914                                            mb_width, mb_height,
915                                            dec->edged_width, dec->edged_width/2,
916                                            fcode, dec->quarterpel, 0,
917                                            rounding, dec->mbs, &dec->gmc);
918    */
919            }
920    
921          bound = 0;          bound = 0;
922    
923          for (y = 0; y < mb_height; y++) {          for (y = 0; y < mb_height; y++) {
# Line 805  Line 941 
941                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
942    
943                          //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
944                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // block _is_ coded
945                          {                          {
946                                  uint32_t mcbpc;                                  uint32_t mcbpc;
947                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 830  Line 966 
966                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
967                                  }                                  }
968    
969                                  if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
970                                  {                                  {
971                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
972                                  }                                  }
973    
974                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
975                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
976    
977                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
978    
# Line 872  Line 1008 
1008                                          }                                          }
1009                                  }                                  }
1010    
1011                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
1012                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
1013                                                                    rounding, reduced_resolution);
1014                                            continue;
1015    
1016                                          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);  
1017    
1018                                          } else if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
1019                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1020                                                                                    fcode, bound);                                                                                    fcode, bound);
1021                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 887  Line 1023 
1023                                          } else {                                          } else {
1024                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
1025                                                                                    fcode, bound);                                                                                    fcode, bound);
1026                                                  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;  
1027                                          }                                          }
1028                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
1029    
# Line 909  Line 1042 
1042                                          continue;                                          continue;
1043                                  }                                  }
1044    
1045                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1046                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution);
1047    
1048                          }                          }
1049                          else if (gmc_mv)        /* not coded S_VOP macroblock */                          else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1050                          {                          {
1051                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED_GMC;
1052                                  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);  
1053                                  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();
1054                                  decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);  
1055                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1056                                                                    rounding, reduced_resolution);
1057    
1058                                    stop_transfer_timer();
1059    
1060                                    if(dec->out_frm && cp_mb > 0) {
1061                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1062                                      cp_mb = 0;
1063                                    }
1064                                    st_mb = x+1;
1065                          }                          }
1066                          else    /* not coded P_VOP macroblock */                          else    /* not coded P_VOP macroblock */
1067                          {                          {
# Line 1467  Line 1610 
1610                          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] =
1611                          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;
1612    
1613                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1614                            // if not codec in co-located S_VOP macroblock is _not_ automatically skipped
1615    
1616                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1617                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1618                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1640  Line 1785 
1785          uint32_t fcode_forward;          uint32_t fcode_forward;
1786          uint32_t fcode_backward;          uint32_t fcode_backward;
1787          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1788          VECTOR gmc_mv[5];          WARPPOINTS gmc_warp;
1789          uint32_t vop_type;          uint32_t vop_type;
1790          int success = 0;          int success = 0;
1791          int output = 0;          int output = 0;
# Line 1695  Line 1840 
1840  repeat:  repeat:
1841    
1842          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1843                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1844    
1845          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1846                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1847    
1848          if (vop_type == - 1)          if (vop_type == - 1)
1849          {          {
1850                  if (success) goto done;                  if (success) goto done;
1851                    emms();
1852                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1853          }          }
1854    
# Line 1723  Line 1869 
1869                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1870                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1871                          frame->length = BitstreamPos(&bs) / 8;                          frame->length = BitstreamPos(&bs) / 8;
1872                            emms();
1873                          return XVID_ERR_OK;                          return XVID_ERR_OK;
1874                  }                  }
1875                  goto repeat;                  goto repeat;
# Line 1754  Line 1901 
1901                          break;                          break;
1902                  case S_VOP :                  case S_VOP :
1903                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1904                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1905                          break;                          break;
1906                  case N_VOP :                  case N_VOP :
1907                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);

Legend:
Removed from v.1.37.2.23  
changed lines
  Added in v.1.47

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