[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.49.2.23, Wed Dec 10 12:09:34 2003 UTC revision 1.49.2.35, Wed Mar 3 13:18:08 2004 UTC
# Line 58  Line 58 
58    
59  #include "image/image.h"  #include "image/image.h"
60  #include "image/colorspace.h"  #include "image/colorspace.h"
61    #include "image/postprocessing.h"
62  #include "utils/mem_align.h"  #include "utils/mem_align.h"
63    
64  static int  static int
# Line 76  Line 77 
77                  xvid_free(dec->last_mbs);                  xvid_free(dec->last_mbs);
78          if (dec->mbs)          if (dec->mbs)
79                  xvid_free(dec->mbs);                  xvid_free(dec->mbs);
80            if (dec->qscale)
81                    xvid_free(dec->qscale);
82    
83          /* realloc */          /* realloc */
84          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
# Line 160  Line 163 
163    
164          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
165    
166            /* nothing happens if that fails */
167            dec->qscale =
168                    xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);
169    
170            if (dec->qscale)
171                    memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
172    
173          return 0;          return 0;
174  }  }
175    
# Line 199  Line 209 
209          /* image based GMC */          /* image based GMC */
210          image_null(&dec->gmc);          image_null(&dec->gmc);
211    
   
212          dec->mbs = NULL;          dec->mbs = NULL;
213          dec->last_mbs = NULL;          dec->last_mbs = NULL;
214            dec->qscale = NULL;
215    
216          init_timer();          init_timer();
217            init_postproc(&dec->postproc);
218          init_mpeg_matrix(dec->mpeg_quant_matrices);          init_mpeg_matrix(dec->mpeg_quant_matrices);
219    
220          /* For B-frame support (used to save reference frame's time */          /* For B-frame support (used to save reference frame's time */
# Line 226  Line 237 
237  {  {
238          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
239          xvid_free(dec->mbs);          xvid_free(dec->mbs);
240            xvid_free(dec->qscale);
241    
242          /* image based GMC */          /* image based GMC */
243          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
# Line 749  Line 761 
761                  mv.y -= range;                  mv.y -= range;
762          }          }
763    
764            /* clip to valid range */
765    
766            if (mv.x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) )
767                    mv.x = (int)(dec->mb_width - x) << (5 + dec->quarterpel);
768    
769            else if (mv.x < (int)(-x-1) << (5 + dec->quarterpel))
770                    mv.x = (int)(-x-1) << (5 + dec->quarterpel);
771    
772            if (mv.y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) )
773                    mv.y = (int)(dec->mb_height - y) << (5 + dec->quarterpel);
774    
775            else if (mv.y < ((int)(-y-1)) << (5 + dec->quarterpel) )
776                    mv.y = (int)(-y-1) << (5 + dec->quarterpel);
777    
778          ret_mv->x = mv.x;          ret_mv->x = mv.x;
779          ret_mv->y = mv.y;          ret_mv->y = mv.y;
780  }  }
# Line 777  Line 803 
803    
804          start_timer();          start_timer();
805          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
806                                          dec->width, dec->height);                                          dec->width, dec->height, dec->bs_version);
807          stop_edges_timer();          stop_edges_timer();
808    
809          if (gmc_warp) {          if (gmc_warp) {
# Line 849  Line 875 
875                                  mb->quant = quant;                                  mb->quant = quant;
876    
877                                  if (dec->interlacing) {                                  if (dec->interlacing) {
878                                          if ((cbp || intra) && !mcsel) {                                          if (cbp || intra) {
879                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
880                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
881                                          }                                          }
882    
883                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
884                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
885                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
886    
# Line 898  Line 924 
924    
925                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
926                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
927                                    mb->quant = quant;
928                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
929    
930                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
# Line 907  Line 934 
934                                  st_mb = x+1;                                  st_mb = x+1;
935                          } else {        /* not coded P_VOP macroblock */                          } else {        /* not coded P_VOP macroblock */
936                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
937                                    mb->quant = quant;
938    
939                                  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;
940                                  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 933  Line 961 
961  get_b_motion_vector(Bitstream * bs,  get_b_motion_vector(Bitstream * bs,
962                                          VECTOR * mv,                                          VECTOR * mv,
963                                          int fcode,                                          int fcode,
964                                          const VECTOR pmv)                                          const VECTOR pmv,
965                                            const DECODER * const dec,
966                                            const int x, const int y)
967  {  {
968          const int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
969          const int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
# Line 956  Line 986 
986          else if (mv_y > high)          else if (mv_y > high)
987                  mv_y -= range;                  mv_y -= range;
988    
989    
990            /* clip to valid range */
991            if (mv_x > ((int)(dec->mb_width - x) << (5 + dec->quarterpel)) )
992                    mv_x = (int)(dec->mb_width - x) << (5 + dec->quarterpel);
993    
994            else if (mv_x < (int)(-x-1) << (5 + dec->quarterpel))
995                    mv_x = (int)(-x-1) << (5 + dec->quarterpel);
996    
997            if (mv_y > ((int)(dec->mb_height - y) << (5 + dec->quarterpel)) )
998                    mv_y = (int)(dec->mb_height - y) << (5 + dec->quarterpel);
999    
1000            else if (mv_y < ((int)(-y-1)) << (5 + dec->quarterpel) )
1001                    mv_y = (int)(-y-1) << (5 + dec->quarterpel);
1002    
1003          mv->x = mv_x;          mv->x = mv_x;
1004          mv->y = mv_y;          mv->y = mv_y;
1005  }  }
# Line 1048  Line 1092 
1092                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1093                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1094                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1095                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1096                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                          pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1097          }          }
1098    
1099          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
# Line 1176  Line 1220 
1220    
1221          start_timer();          start_timer();
1222          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1223                                          dec->width, dec->height);                                          dec->width, dec->height, dec->bs_version);
1224          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1225                                          dec->width, dec->height);                                          dec->width, dec->height, dec->bs_version);
1226          stop_edges_timer();          stop_edges_timer();
1227    
1228          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
# Line 1262  Line 1306 
1306    
1307                          switch (mb->mode) {                          switch (mb->mode) {
1308                          case MODE_DIRECT:                          case MODE_DIRECT:
1309                                  get_b_motion_vector(bs, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1310    
1311                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1312                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
# Line 1281  Line 1325 
1325                                  break;                                  break;
1326    
1327                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1328                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1329                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1330    
1331                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1332                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
1333    
1334                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
# Line 1292  Line 1336 
1336                                  break;                                  break;
1337    
1338                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1339                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1340                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1341    
1342                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
1343                                  break;                                  break;
1344    
1345                          case MODE_FORWARD:                          case MODE_FORWARD:
1346                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1347                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1348    
1349                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
# Line 1314  Line 1358 
1358    
1359  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1360  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1361                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1362                                            int coding_type, int quant)
1363  {  {
1364          if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV))    /* post process */          if (dec->cartoon_mode)
1365                    frame->general &= ~XVID_FILMEFFECT;
1366    
1367            if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL)     /* post process */
1368          {          {
1369                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1370                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1371                  image_deblock(&dec->tmp, dec->edged_width,                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1372                                            mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                            mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1373                                            frame->general);                                             frame->general, dec->frames, (coding_type == B_VOP));
1374                  img = &dec->tmp;                  img = &dec->tmp;
1375          }          }
1376    
# Line 1334  Line 1382 
1382                  stats->type = coding2type(coding_type);                  stats->type = coding2type(coding_type);
1383                  stats->data.vop.time_base = (int)dec->time_base;                  stats->data.vop.time_base = (int)dec->time_base;
1384                  stats->data.vop.time_increment = 0;     /* XXX: todo */                  stats->data.vop.time_increment = 0;     /* XXX: todo */
1385                    stats->data.vop.qscale_stride = dec->mb_width;
1386                    stats->data.vop.qscale = dec->qscale;
1387                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1388                            int i;
1389                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1390                                    stats->data.vop.qscale[i] = mbs[i].quant;
1391                    } else
1392                            stats->data.vop.qscale = NULL;
1393          }          }
1394  }  }
1395    
# Line 1346  Line 1402 
1402          Bitstream bs;          Bitstream bs;
1403          uint32_t rounding;          uint32_t rounding;
1404          uint32_t reduced_resolution;          uint32_t reduced_resolution;
1405          uint32_t quant;          uint32_t quant = 2;
1406          uint32_t fcode_forward;          uint32_t fcode_forward;
1407          uint32_t fcode_backward;          uint32_t fcode_backward;
1408          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
# Line 1369  Line 1425 
1425                  /* if not decoding "low_delay/packed", and this isn't low_delay and                  /* if not decoding "low_delay/packed", and this isn't low_delay and
1426                          we have a reference frame, then outout the reference frame */                          we have a reference frame, then outout the reference frame */
1427                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {                  if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1428                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1429                          dec->frames = 0;                          dec->frames = 0;
1430                          ret = 0;                          ret = 0;
1431                  } else {                  } else {
# Line 1439  Line 1495 
1495          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1496          if (dec->packed_mode && coding_type == N_VOP) {          if (dec->packed_mode && coding_type == N_VOP) {
1497                  if (dec->low_delay_default && dec->frames > 0) {                  if (dec->low_delay_default && dec->frames > 0) {
1498                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1499                          output = 1;                          output = 1;
1500                  }                  }
1501                  /* ignore otherwise */                  /* ignore otherwise */
# Line 1473  Line 1529 
1529                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1530                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode)) {
1531                          if (dec->low_delay) {                          if (dec->low_delay) {
1532                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1533                                  output = 1;                                  output = 1;
1534                          } else if (dec->frames > 0)     { /* is the reference frame valid? */                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
1535                                  /* output the reference frame */                                  /* output the reference frame */
1536                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1537                                  output = 1;                                  output = 1;
1538                          }                          }
1539                  }                  }
# Line 1502  Line 1558 
1558                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1559                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1560                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
1561                          stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1562                  } else if (dec->time_pp <= dec->time_bp) {                  } else if (dec->time_pp <= dec->time_bp) {
1563                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1564                          decoded in vfw. */                          decoded in vfw. */
1565                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1566                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);                                                  "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1567                          stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1568                  } else {                  } else {
1569                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1570                          decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);                          decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1571                  }                  }
1572    
1573                  output = 1;                  output = 1;
1574                  dec->frames++;                  dec->frames++;
1575          }          }
1576    
1577    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1578          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1579    #endif
1580    
1581          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1582          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
# Line 1533  Line 1591 
1591          if (dec->low_delay_default && output == 0) {          if (dec->low_delay_default && output == 0) {
1592                  if (dec->packed_mode && seen_something) {                  if (dec->packed_mode && seen_something) {
1593                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1594                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1595                  } else {                  } else {
1596                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);                          image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1597                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
# Line 1541  Line 1599 
1599                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1600                                  "bframe decoder lag");                                  "bframe decoder lag");
1601    
1602                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1603                          if (stats) stats->type = XVID_TYPE_NOTHING;                          if (stats) stats->type = XVID_TYPE_NOTHING;
1604                  }                  }
1605          }          }
# Line 1549  Line 1607 
1607          emms();          emms();
1608          stop_global_timer();          stop_global_timer();
1609    
1610          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1611  }  }

Legend:
Removed from v.1.49.2.23  
changed lines
  Added in v.1.49.2.35

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