[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.16, Sun Dec 8 06:43:33 2002 UTC revision 1.37.2.20, Fri Dec 13 11:26:41 2002 UTC
# Line 80  Line 80 
80  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
81  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
82  #include "image/reduced.h"  #include "image/reduced.h"
83    #include "image/font.h"
84    
85  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
86  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 216  Line 217 
217    
218          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
219          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
220          dec->frames = -1;          dec->frames = 0;
221          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
222          dec->low_delay = 0;          dec->low_delay = 0;
223          dec->packed_mode = 0;          dec->packed_mode = 0;
# Line 389  Line 390 
390    
391  // decode an inter macroblock  // decode an inter macroblock
392    
 static void  
 rrv_mv_scaleup(VECTOR * mv)  
 {  
         if (mv->x > 0) {  
                 mv->x = 2*mv->x - 1;  
         } else if (mv->x < 0) {  
                 mv->x = 2*mv->x + 1;  
         }  
   
         if (mv->y > 0) {  
                 mv->y = 2*mv->y - 1;  
         } else if (mv->y < 0) {  
                 mv->y = 2*mv->y + 1;  
         }  
 }  
   
   
   
393  void  void
394  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
395                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
# Line 431  Line 414 
414          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
415    
416          int uv_dx, uv_dy;          int uv_dx, uv_dy;
417          VECTOR mv[4];          VECTOR mv[4];   /* local copy of mvs */
   
         for (i = 0; i < 4; i++)  
         {  
                 mv[i] = pMB->mvs[i];  
                 //DPRINTF(DPRINTF_MB, "mv[%i]   orig=%i,%i   local=%i", i, pMB->mvs[i].x, pMB->mvs[i].y,                                                mv[i].x, mv[i].y);  
         }  
418    
419          if (reduced_resolution) {          if (reduced_resolution) {
420                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
421                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
422                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
423                  DPRINTF(DPRINTF_MB,"[%i,%i] %i,%i  %i,%i  %i,%i  %i,%i",                  for (i = 0; i < 4; i++) {
424                          x_pos, y_pos,                          mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
425                          mv[0].x, mv[0].y,                          mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
426                          mv[1].x, mv[1].y,                  }
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
   
                 rrv_mv_scaleup(&mv[0]);  
                 rrv_mv_scaleup(&mv[1]);  
                 rrv_mv_scaleup(&mv[2]);  
                 rrv_mv_scaleup(&mv[3]);  
   
                 DPRINTF(DPRINTF_MB,"        %i,%i  %i,%i  %i,%i  %i,%i",  
                         mv[0].x, mv[0].y,  
                         mv[1].x, mv[1].y,  
                         mv[2].x, mv[2].y,  
                         mv[3].x, mv[3].y);  
427          }else{          }else{
428                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);                  pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
429                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);                  pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
430                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);                  pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
431                    for (i = 0; i < 4; i++)
432                            mv[i] = pMB->mvs[i];
433          }          }
434    
435          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
# Line 658  Line 624 
624    
625          if (reduced_resolution)          if (reduced_resolution)
626          {          {
627                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
628                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
629          }          }
630    
631          bound = 0;          bound = 0;
# Line 807  Line 773 
773    
774          if (reduced_resolution)          if (reduced_resolution)
775          {          {
776                  mb_width /= 2;                  mb_width = (dec->width + 31) / 32;
777                  mb_height /= 2;                  mb_height = (dec->height + 31) / 32;
778          }          }
779    
780          start_timer();          start_timer();
# Line 1640  Line 1606 
1606          *mb2 = temp;          *mb2 = temp;
1607  }  }
1608    
1609    
1610  int  int
1611  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1612                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
# Line 1655  Line 1622 
1622          VECTOR gmc_mv[5];          VECTOR gmc_mv[5];
1623          uint32_t vop_type;          uint32_t vop_type;
1624          int success = 0;          int success = 0;
1625            int output = 0;
1626            int seen_something = 0;
1627    
1628          start_global_timer();          start_global_timer();
1629    
1630            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1631    
1632          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1633    
1634          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
# Line 1677  Line 1648 
1648  start:  start:
1649          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
1650          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
         dec->frames++;  
1651    
1652  xxx:  xxx:
1653          vop_type =          vop_type =
1654                  BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,                  BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1655                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);
1656    
1657          //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",
1658                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1659    
1660          if (vop_type == -1 && success)          if (vop_type == - 1)
1661                  goto done;          {
1662                    if (success) goto done;
1663                    return XVID_ERR_FAIL;
1664            }
1665    
1666          if (vop_type == -2 || vop_type == -3)          if (vop_type == -2 || vop_type == -3)
1667          {          {
# Line 1713  Line 1687 
1687    
1688          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0          dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  // init pred vector to 0
1689    
         switch (vop_type) {  
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,  
                                                 fcode_forward, intra_dc_threshold, NULL);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("P_VOP  Time=", dec->time);  
 #endif  
                 break;  
1690    
1691            /* packed_mode: special-N_VOP treament */
1692            if (dec->packed_mode && vop_type == N_VOP)
1693            {
1694                    if (dec->low_delay_default)
1695                    {
1696                            image_output(&dec->refn[0], dec->width, dec->height,
1697                                                     dec->edged_width, frame->image, frame->stride,
1698                                                     frame->colorspace, dec->interlacing);
1699                            output = 1;
1700                    }
1701                    /* ignore otherwise */
1702            }
1703            else if (vop_type != B_VOP)
1704            {
1705                    switch(vop_type)
1706                    {
1707          case I_VOP:          case I_VOP:
1708                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
1709                  break;                  break;
1710                    case P_VOP :
1711          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1712  #ifdef BFRAMES_DEC                                                  fcode_forward, intra_dc_threshold, NULL);
                 if (dec->time_pp > dec->time_bp) {  
                         DEBUG1("B_VOP  Time=", dec->time);  
                         decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);  
                 } else {  
                         DEBUG("broken B-frame!");  
                 }  
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
1713                  break;                  break;
   
1714          case S_VOP :          case S_VOP :
1715                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1716                                                  fcode_forward, intra_dc_threshold, gmc_mv);                                                  fcode_forward, intra_dc_threshold, gmc_mv);
1717                  break;                  break;
1718                    case N_VOP :
         case N_VOP:                             // vop not coded  
                 // when low_delay==0, N_VOP's should interpolate between the past and future frames  
1719                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
 #ifdef BFRAMES_DEC  
                 DEBUG1("N_VOP  Time=", dec->time);  
 #endif  
1720                  break;                  break;
   
         default:  
                 if (stats)  
                         stats->notify = 0;  
   
                 emms();  
                 return XVID_ERR_FAIL;  
1721          }          }
1722    
   
         /* reduced resolution deblocking filter */  
   
1723          if (reduced_resolution)          if (reduced_resolution)
1724          {          {
1725                  const int rmb_height = dec->mb_height / 2;                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1726                  const int rmb_width = dec->mb_width / 2;                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width);
1727                  const int edged_width2 = dec->edged_width /2;                  }
                 int i,j;  
   
                 /* horizontal deblocking */  
1728    
1729                  for (j = 1; j < rmb_height*2; j++)      // luma: j,i in block units                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1730                  for (i = 0; i < rmb_width*2; i++)                  if (!(dec->low_delay_default && dec->packed_mode))
1731                  {                  {
1732                          if (dec->mbs[(j-1)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED ||                          if (dec->low_delay)
                                 dec->mbs[(j+0)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED)  
1733                          {                          {
1734                                  xvid_HFilter_31_C(dec->cur.y + (j*16 - 1)*dec->edged_width + i*16,                                  image_output(&dec->cur, dec->width, dec->height,
1735                                                                dec->cur.y + (j*16 + 0)*dec->edged_width + i*16, 2);                                                           dec->edged_width, frame->image, frame->stride,
1736                          }                                                           frame->colorspace, dec->interlacing);
1737                                    output = 1;
1738                  }                  }
1739                            else if (dec->frames > 0)       /* is the reference frame valid? */
                 for (j = 1; j < rmb_height; j++)        // chroma  
                 for (i = 0; i < rmb_width; i++)  
                 {  
                         if (dec->mbs[(j-1)*dec->mb_width + i].mode != MODE_NOT_CODED ||  
                                 dec->mbs[(j+0)*dec->mb_width + i].mode != MODE_NOT_CODED)  
1740                          {                          {
1741                                  hfilter_31(dec->cur.u + (j*16 - 1)*edged_width2 + i*16,                                  image_output(&dec->refn[0], dec->width, dec->height,
1742                                                                    dec->cur.u + (j*16 + 0)*edged_width2 + i*16, 2);                                                           dec->edged_width, frame->image, frame->stride,
1743                                  hfilter_31(dec->cur.v + (j*16 - 1)*edged_width2 + i*16,                                                           frame->colorspace, dec->interlacing);
1744                                                                    dec->cur.v + (j*16 + 0)*edged_width2 + i*16, 2);                                  output = 1;
1745                          }                          }
1746                  }                  }
1747    
1748                  /* vertical deblocking */                  image_swap(&dec->refn[0], &dec->refn[1]);
1749                    image_swap(&dec->cur, &dec->refn[0]);
1750                    mb_swap(&dec->mbs, &dec->last_mbs);
1751    
1752                  for (j = 0; j < rmb_height*2; j++)              // luma: i,j in block units                  dec->frames++;
1753                  for (i = 1; i < rmb_width*2; i++)                  seen_something = 1;
                 {  
                         if (dec->mbs[(j/2)*dec->mb_width + (i-1)/2].mode != MODE_NOT_CODED ||  
                                 dec->mbs[(j/2)*dec->mb_width + (i+0)/2].mode != MODE_NOT_CODED)  
                         {  
                                 vfilter_31(dec->cur.y + (j*16)*dec->edged_width + i*16 - 1,  
                                                               dec->cur.y + (j*16)*dec->edged_width + i*16 + 0,  
                                                                   dec->edged_width, 2);  
                         }  
                 }  
1754    
1755                  for (j = 0; j < rmb_height; j++)        // chroma          }else{  /* B_VOP */
1756                  for (i = 1; i < rmb_width; i++)  
1757                  {                  if (dec->low_delay)
                         if (dec->mbs[j*dec->mb_width + i - 1].mode != MODE_NOT_CODED ||  
                                 dec->mbs[j*dec->mb_width + i + 0].mode != MODE_NOT_CODED)  
1758                          {                          {
1759                                  vfilter_31(dec->cur.u + (j*16)*edged_width2 + i*16 - 1,                          DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1760                                                                    dec->cur.u + (j*16)*edged_width2 + i*16 + 0,                          dec->low_delay = 1;
                                                                   edged_width2, 2);  
                                 vfilter_31(dec->cur.v + (j*16)*edged_width2 + i*16 - 1,  
                                                                   dec->cur.v + (j*16)*edged_width2 + i*16 + 0,  
                                                                   edged_width2, 2);  
1761                          }                          }
                 }  
         }  
   
         BitstreamByteAlign(&bs);  
1762    
1763  #ifdef BFRAMES_DEC                  if (dec->time_pp <= dec->time_bp) {
1764          // test if no B_VOP                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1765          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {                          decoded in vfw. */
1766  #endif                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1767                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
                                          frame->image, frame->stride, frame->colorspace, dec->interlacing);  
   
 #ifdef BFRAMES_DEC  
1768          } else {          } else {
1769                  if (dec->frames >= 1 && !(dec->packed_mode)) {                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1770                          start_timer();                  }
1771                          if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace, dec->interlacing);  
                         } else if (vop_type == B_VOP) {  
1772                                  image_output(&dec->cur, dec->width, dec->height,                                  image_output(&dec->cur, dec->width, dec->height,
1773                                                           dec->edged_width, frame->image, frame->stride,                                                           dec->edged_width, frame->image, frame->stride,
1774                                                           frame->colorspace, dec->interlacing);                                                           frame->colorspace, dec->interlacing);
1775                    output = 1;
1776                    dec->frames++;
1777                          }                          }
                         stop_conv_timer();  
                 }  
         }  
 #endif  
1778    
1779          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {          emms();
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
   
                 // swap MACROBLOCK  
                 // the Divx will not set the low_delay flage some times  
                 // so follow code will wrong to not swap at that time  
                 // this will broken bitstream! so I'm change it,  
                 // But that is not the best way! can anyone tell me how  
                 // to do another way?  
                 // 18-07-2002   MinChen<chenm001@163.com>  
                 //if (!dec->low_delay && vop_type == P_VOP)  
                 if (vop_type == P_VOP)  
                         mb_swap(&dec->mbs, &dec->last_mbs);  
         }  
1780    
1781            BitstreamByteAlign(&bs);
1782    
1783          if (success == 0 && dec->packed_mode)          /* low_delay_default mode: repeat in packed_mode */
1784            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1785          {          {
1786                  success = 1;                  success = 1;
         //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together  
1787                  goto start;                  goto start;
1788          }          }
1789    
1790  done :  done :
1791    
1792            /* low_delay_default mode: if we've gotten here without outputting anything,
1793               then output the recently decoded frame, or print an error message  */
1794            if (dec->low_delay_default && output == 0)
1795            {
1796                    if (dec->packed_mode && seen_something)
1797                    {
1798                            /* output the recently decoded frame */
1799                            image_output(&dec->refn[0], dec->width, dec->height,
1800                                                             dec->edged_width, frame->image, frame->stride,
1801                                                             frame->colorspace, dec->interlacing);
1802                    }
1803                    else
1804                    {
1805                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1806                                    "warning: nothing to output");
1807                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1808                                    "bframe decoder lag");
1809    
1810                            image_output(&dec->cur, dec->width, dec->height,
1811                                                             dec->edged_width, frame->image, frame->stride,
1812                                                             frame->colorspace, dec->interlacing);
1813                    }
1814                    output = 1;
1815            }
1816    
1817          frame->length = BitstreamPos(&bs) / 8;          frame->length = BitstreamPos(&bs) / 8;
1818    
1819          if (stats)          if (stats)
1820          {          {
1821                  stats->notify = XVID_DEC_VOP;                  stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1822                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1823                  stats->data.vop.time_increment = 0;     //XXX: todo                  stats->data.vop.time_increment = 0;     //XXX: todo
1824          }          }

Legend:
Removed from v.1.37.2.16  
changed lines
  Added in v.1.37.2.20

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