[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.25, Tue Jul 9 01:09:33 2002 UTC revision 1.26, Tue Jul 9 01:37:22 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History:   *  History:
34   *   *
35     *  28.06.2002  added basic resync support to iframe/pframe_decode()
36   *      22.06.2002      added primative N_VOP support   *      22.06.2002      added primative N_VOP support
37   *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder   *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder
38   *  08.05.2002  add low_delay support for B_VOP decode   *  08.05.2002  add low_delay support for B_VOP decode
# Line 168  Line 169 
169  }  }
170    
171    
   
 /* ****************************************************************  
 NIC 28.06.2002  
         riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla  
         usare nella dll caricata dall'IM1 player  
         IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)  
 **************************************************************** */  
 //XVID_DEC_FRAME * frame  
   
 int  
 IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)  
 {  
         DECODER *dec;  
         Bitstream bs;  
         uint32_t rounding;  
         uint32_t quant;  
         uint32_t fcode_forward;  
         uint32_t fcode_backward;  
         uint32_t intra_dc_threshold;  
   
         dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);  
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
   
   
         BitstreamInit(&bs, frame->bitstream, frame->length);//NIC  
   
         //NIC  
         BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,  
                                                          &fcode_backward, &intra_dc_threshold);  
   
   
         param->width = dec->width;                      //NIC added  
         param->height = dec->height;            //NIC added  
         //dec->width = param->width;            //NIC commentate  
         //dec->height = param->height;          //NIC commentate  
   
         dec->mb_width = (dec->width + 15) / 16;  
         dec->mb_height = (dec->height + 15) / 16;  
   
         dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;  
         dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;  
         dec->low_delay = 0;  
   
         if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to reference last 2 frame  
         if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
         if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         dec->mbs =  
                 xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,  
                                         CACHE_LINE);  
         if (dec->mbs == NULL) {  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);  
   
         // add by chenm001 <chenm001@163.com>  
         // for skip MB flag  
         dec->last_mbs =  
                 xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,  
                                         CACHE_LINE);  
         if (dec->last_mbs == NULL) {  
                 xvid_free(dec->mbs);  
                 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);  
                 image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);  
   
         init_timer();  
   
         // add by chenm001 <chenm001@163.com>  
         // for support B-frame to save reference frame's time  
         dec->frames = -1;  
         dec->time = dec->time_base = dec->last_time_base = 0;  
   
         return XVID_ERR_OK;  
 }  
 /* ****************************************************************  
                                                         END NIC  
 **************************************************************** */  
   
172  int  int
173  decoder_destroy(DECODER * dec)  decoder_destroy(DECODER * dec)
174  {  {
# Line 319  Line 204 
204                                  const uint32_t cbp,                                  const uint32_t cbp,
205                                  Bitstream * bs,                                  Bitstream * bs,
206                                  const uint32_t quant,                                  const uint32_t quant,
207                                  const uint32_t intra_dc_threshold)                                  const uint32_t intra_dc_threshold,
208                                    const unsigned int bound)
209  {  {
210    
211          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 345  Line 231 
231    
232                  start_timer();                  start_timer();
233                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
234                                           iQuant, iDcScaler, predictors);                                           iQuant, iDcScaler, predictors, bound);
235                  if (!acpred_flag) {                  if (!acpred_flag) {
236                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
237                  }                  }
# Line 364  Line 250 
250    
251                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
252                          start_coeff = 1;                          start_coeff = 1;
253    
254                            DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
255                  } else {                  } else {
256                          start_coeff = 0;                          start_coeff = 0;
257                  }                  }
# Line 538  Line 426 
426                             int quant,                             int quant,
427                             int intra_dc_threshold)                             int intra_dc_threshold)
428  {  {
429            uint32_t bound;
430          uint32_t x, y;          uint32_t x, y;
431    
432            bound = 0;
433    
434          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
435                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
436                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb;
   
437                          uint32_t mcbpc;                          uint32_t mcbpc;
438                          uint32_t cbpc;                          uint32_t cbpc;
439                          uint32_t acpred_flag;                          uint32_t acpred_flag;
440                          uint32_t cbpy;                          uint32_t cbpy;
441                          uint32_t cbp;                          uint32_t cbp;
442    
443                            while (BitstreamShowBits(bs, 9) == 1)
444                                    BitstreamSkip(bs, 9);
445    
446                            if (check_resync_marker(bs, 0))
447                            {
448                                    bound = read_video_packet_header(bs, 0, &quant);
449                                    x = bound % dec->mb_width;
450                                    y = bound / dec->mb_width;
451                            }
452                            mb = &dec->mbs[y * dec->mb_width + x];
453    
454                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
455    
456                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
457                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
458                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
459    
460                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
461    
                         if (mb->mode == MODE_STUFFING) {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
462                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
463                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
464    
# Line 581  Line 478 
478                          }                          }
479    
480                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
481                                                          intra_dc_threshold);                                                          intra_dc_threshold, bound);
482                  }                  }
483          }          }
484    
# Line 595  Line 492 
492                                    int y,                                    int y,
493                                    int k,                                    int k,
494                                    VECTOR * mv,                                    VECTOR * mv,
495                                    int fcode)                                    int fcode,
496                                      const int bound)
497  {  {
498    
499          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
# Line 603  Line 501 
501          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
502          int range = (64 * scale_fac);          int range = (64 * scale_fac);
503    
504          VECTOR pmv[4];          VECTOR pmv;
         int32_t psad[4];  
   
505          int mv_x, mv_y;          int mv_x, mv_y;
         int pmv_x, pmv_y;  
   
   
         get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);  
506    
507          pmv_x = pmv[0].x;          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
         pmv_y = pmv[0].y;  
508    
509          mv_x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
510          mv_y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
511    
512          mv_x += pmv_x;          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);
513          mv_y += pmv_y;  
514            mv_x += pmv.x;
515            mv_y += pmv.y;
516    
517          if (mv_x < low) {          if (mv_x < low) {
518                  mv_x += range;                  mv_x += range;
# Line 649  Line 542 
542  {  {
543    
544          uint32_t x, y;          uint32_t x, y;
545            uint32_t bound;
546    
547          start_timer();          start_timer();
548          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
549                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height, dec->interlacing);
550          stop_edges_timer();          stop_edges_timer();
551    
552            bound = 0;
553    
554          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
555                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
556                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb;
557    
558                            // skip stuffing
559                            while (BitstreamShowBits(bs, 10) == 1)
560                                    BitstreamSkip(bs, 10);
561    
562                            if (check_resync_marker(bs, fcode - 1))
563                            {
564                                    bound = read_video_packet_header(bs, fcode - 1, &quant);
565                                    x = bound % dec->mb_width;
566                                    y = bound / dec->mb_width;
567                            }
568                            mb = &dec->mbs[y * dec->mb_width + x];
569    
570                            DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
571    
572                          //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
573                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // not_coded
# Line 672  Line 582 
582                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
583                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
584                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
585    
586                                    DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
587                                    DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
588                                  acpred_flag = 0;                                  acpred_flag = 0;
589    
590                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
# Line 680  Line 593 
593                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
594                                  }                                  }
595    
                                 if (mb->mode == MODE_STUFFING) {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
   
596                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
597                                    DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
598    
599                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
600    
601                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
602                                          quant += dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
603                                            DPRINTF(DPRINTF_MB, "dquant %i", dquant);
604                                            quant += dquant;
605                                          if (quant > 31) {                                          if (quant > 31) {
606                                                  quant = 31;                                                  quant = 31;
607                                          } else if (mb->quant < 1) {                                          } else if (quant < 1) {
608                                                  quant = 1;                                                  quant = 1;
609                                          }                                          }
610                                            DPRINTF(DPRINTF_MB, "quant %i", quant);
611                                  }                                  }
612                                  mb->quant = quant;                                  mb->quant = quant;
613    
# Line 718  Line 631 
631                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
632                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
633                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
634                                                                                    fcode);                                                                                    fcode, bound);
635                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
636                                                                                    fcode);                                                                                    fcode, bound);
637                                          } else {                                          } else {
638                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
639                                                                                    fcode);                                                                                    fcode, bound);
640                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
641                                                          mb->mvs[0].x;                                                          mb->mvs[0].x;
642                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
# Line 731  Line 644 
644                                          }                                          }
645                                  } else if (mb->mode ==                                  } else if (mb->mode ==
646                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {
647                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
648                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
649                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
650                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
651                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  // MODE_INTRA, MODE_INTRA_Q
652                                  {                                  {
653                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
# Line 742  Line 655 
655                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
656                                                  0;                                                  0;
657                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
658                                                                          intra_dc_threshold);                                                                          intra_dc_threshold, bound);
659                                          continue;                                          continue;
660                                  }                                  }
661    
# Line 1216  Line 1129 
1129    
1130                                          if (quant > 31) {                                          if (quant > 31) {
1131                                                  quant = 31;                                                  quant = 31;
1132                                          } else if (mb->quant < 1) {                                          } else if (quant < 1) {
1133                                                  quant = 1;                                                  quant = 1;
1134                                          }                                          }
1135                                  } else {                                  } else {
# Line 1359  Line 1272 
1272          case P_VOP:          case P_VOP:
1273                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1274                                             intra_dc_threshold);                                             intra_dc_threshold);
1275                  printf("P_VOP  Time=%ld\n",dec->time);  #ifdef BFRAMES_DEC
1276                  DEBUG1("P_VOP  Time=", dec->time);                  DEBUG1("P_VOP  Time=", dec->time);
1277    #endif
1278                  break;                  break;
1279    
1280          case I_VOP:          case I_VOP:
1281                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1282                  printf("I_VOP  Time=%ld\n",dec->time);  #ifdef BFRAMES_DEC
1283                  DEBUG1("I_VOP  Time=", dec->time);                  DEBUG1("I_VOP  Time=", dec->time);
1284    #endif
1285                  break;                  break;
1286    
1287          case B_VOP:          case B_VOP:
1288  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1289                  if (dec->time_pp > dec->time_bp) {                  if (dec->time_pp > dec->time_bp) {
                         printf("I_VOP  Time=%ld\n",dec->time);  
1290                          DEBUG1("B_VOP  Time=", dec->time);                          DEBUG1("B_VOP  Time=", dec->time);
1291                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1292                  } else {                  } else {
1293                          DEBUG("broken B-frame!");                          DEBUG("broken B-frame!");
1294                  }                  }
1295  #else  #else
                 printf("Che minchia ne so\n");  
1296                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1297  #endif  #endif
1298                  break;                  break;
# Line 1387  Line 1300 
1300          case N_VOP:                             // vop not coded          case N_VOP:                             // vop not coded
1301                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                  // when low_delay==0, N_VOP's should interpolate between the past and future frames
1302                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
                 printf("N_VOP vop not coded\n");  
1303                  break;                  break;
1304    
1305          default:          default:

Legend:
Removed from v.1.25  
changed lines
  Added in v.1.26

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