[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.24, Wed Jul 3 12:32:50 2002 UTC revision 1.25, Tue Jul 9 01:09:33 2002 UTC
# Line 32  Line 32 
32   *   *
33   *  History:   *  History:
34   *   *
  *  28.06.2002  added basic resync support to iframe/pframe_decode()  
35   *      22.06.2002      added primative N_VOP support   *      22.06.2002      added primative N_VOP support
36   *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder   *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder
37   *  08.05.2002  add low_delay support for B_VOP decode   *  08.05.2002  add low_delay support for B_VOP decode
# Line 169  Line 168 
168  }  }
169    
170    
171    
172    /* ****************************************************************
173    NIC 28.06.2002
174            riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla
175            usare nella dll caricata dall'IM1 player
176            IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)
177    **************************************************************** */
178    //XVID_DEC_FRAME * frame
179    
180    int
181    IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame)
182    {
183            DECODER *dec;
184            Bitstream bs;
185            uint32_t rounding;
186            uint32_t quant;
187            uint32_t fcode_forward;
188            uint32_t fcode_backward;
189            uint32_t intra_dc_threshold;
190    
191            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
192            if (dec == NULL) {
193                    return XVID_ERR_MEMORY;
194            }
195            param->handle = dec;
196    
197    
198            BitstreamInit(&bs, frame->bitstream, frame->length);//NIC
199    
200            //NIC
201            BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,
202                                                             &fcode_backward, &intra_dc_threshold);
203    
204    
205            param->width = dec->width;                      //NIC added
206            param->height = dec->height;            //NIC added
207            //dec->width = param->width;            //NIC commentate
208            //dec->height = param->height;          //NIC commentate
209    
210            dec->mb_width = (dec->width + 15) / 16;
211            dec->mb_height = (dec->height + 15) / 16;
212    
213            dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
214            dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
215            dec->low_delay = 0;
216    
217            if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
218                    xvid_free(dec);
219                    return XVID_ERR_MEMORY;
220            }
221    
222            if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
223                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
224                    xvid_free(dec);
225                    return XVID_ERR_MEMORY;
226            }
227            // add by chenm001 <chenm001@163.com>
228            // for support B-frame to reference last 2 frame
229            if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
230                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
231                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
232                    xvid_free(dec);
233                    return XVID_ERR_MEMORY;
234            }
235            if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {
236                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
237                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
238                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
239                    xvid_free(dec);
240                    return XVID_ERR_MEMORY;
241            }
242    
243            dec->mbs =
244                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
245                                            CACHE_LINE);
246            if (dec->mbs == NULL) {
247                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
248                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
249                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
250                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
251                    xvid_free(dec);
252                    return XVID_ERR_MEMORY;
253            }
254    
255            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
256    
257            // add by chenm001 <chenm001@163.com>
258            // for skip MB flag
259            dec->last_mbs =
260                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
261                                            CACHE_LINE);
262            if (dec->last_mbs == NULL) {
263                    xvid_free(dec->mbs);
264                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
265                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
266                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
267                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
268                    xvid_free(dec);
269                    return XVID_ERR_MEMORY;
270            }
271    
272            memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
273    
274            init_timer();
275    
276            // add by chenm001 <chenm001@163.com>
277            // for support B-frame to save reference frame's time
278            dec->frames = -1;
279            dec->time = dec->time_base = dec->last_time_base = 0;
280    
281            return XVID_ERR_OK;
282    }
283    /* ****************************************************************
284                                                            END NIC
285    **************************************************************** */
286    
287  int  int
288  decoder_destroy(DECODER * dec)  decoder_destroy(DECODER * dec)
289  {  {
# Line 204  Line 319 
319                                  const uint32_t cbp,                                  const uint32_t cbp,
320                                  Bitstream * bs,                                  Bitstream * bs,
321                                  const uint32_t quant,                                  const uint32_t quant,
322                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold)
                                 const unsigned int bound)  
323  {  {
324    
325          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 231  Line 345 
345    
346                  start_timer();                  start_timer();
347                  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],
348                                           iQuant, iDcScaler, predictors, bound);                                           iQuant, iDcScaler, predictors);
349                  if (!acpred_flag) {                  if (!acpred_flag) {
350                          pMB->acpred_directions[i] = 0;                          pMB->acpred_directions[i] = 0;
351                  }                  }
# Line 250  Line 364 
364    
365                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
366                          start_coeff = 1;                          start_coeff = 1;
   
                         DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);  
367                  } else {                  } else {
368                          start_coeff = 0;                          start_coeff = 0;
369                  }                  }
# Line 426  Line 538 
538                             int quant,                             int quant,
539                             int intra_dc_threshold)                             int intra_dc_threshold)
540  {  {
         uint32_t bound;  
         uint32_t x, y;  
541    
542          bound = 0;          uint32_t x, y;
543    
544          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
545                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
546                          MACROBLOCK *mb;                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
547    
548                          uint32_t mcbpc;                          uint32_t mcbpc;
549                          uint32_t cbpc;                          uint32_t cbpc;
550                          uint32_t acpred_flag;                          uint32_t acpred_flag;
551                          uint32_t cbpy;                          uint32_t cbpy;
552                          uint32_t cbp;                          uint32_t cbp;
553    
                         while (BitstreamShowBits(bs, 9) == 1)  
                                 BitstreamSkip(bs, 9);  
   
                         if (check_resync_marker(bs, 0))  
                         {  
                                 bound = read_video_packet_header(bs, 0, &quant);  
                                 x = bound % dec->mb_width;  
                                 y = bound / dec->mb_width;  
                         }  
                         mb = &dec->mbs[y * dec->mb_width + x];  
   
                         DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));  
   
554                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
555                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
556                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
557    
558                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
559    
560                            if (mb->mode == MODE_STUFFING) {
561                                    DEBUG("-- STUFFING ?");
562                                    continue;
563                            }
564    
565                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
566                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
567    
# Line 478  Line 581 
581                          }                          }
582    
583                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
584                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold);
585                  }                  }
586          }          }
587    
# Line 492  Line 595 
595                                    int y,                                    int y,
596                                    int k,                                    int k,
597                                    VECTOR * mv,                                    VECTOR * mv,
598                                    int fcode,                                    int fcode)
                                   const int bound)  
599  {  {
600    
601          int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
# Line 501  Line 603 
603          int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
604          int range = (64 * scale_fac);          int range = (64 * scale_fac);
605    
606          VECTOR pmv;          VECTOR pmv[4];
607            int32_t psad[4];
608    
609          int mv_x, mv_y;          int mv_x, mv_y;
610            int pmv_x, pmv_y;
611    
612    
613            get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);
614    
615          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv_x = pmv[0].x;
616            pmv_y = pmv[0].y;
617    
618          mv_x = get_mv(bs, fcode);          mv_x = get_mv(bs, fcode);
619          mv_y = get_mv(bs, fcode);          mv_y = get_mv(bs, fcode);
620    
621          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          mv_x += pmv_x;
622            mv_y += pmv_y;
         mv_x += pmv.x;  
         mv_y += pmv.y;  
623    
624          if (mv_x < low) {          if (mv_x < low) {
625                  mv_x += range;                  mv_x += range;
# Line 542  Line 649 
649  {  {
650    
651          uint32_t x, y;          uint32_t x, y;
         uint32_t bound;  
652    
653          start_timer();          start_timer();
654          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
655                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height, dec->interlacing);
656          stop_edges_timer();          stop_edges_timer();
657    
         bound = 0;  
   
658          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
659                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
660                          MACROBLOCK *mb;                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
   
                         // skip stuffing  
                         while (BitstreamShowBits(bs, 10) == 1)  
                                 BitstreamSkip(bs, 10);  
   
                         if (check_resync_marker(bs, fcode - 1))  
                         {  
                                 bound = read_video_packet_header(bs, fcode - 1, &quant);  
                                 x = bound % dec->mb_width;  
                                 y = bound / dec->mb_width;  
                         }  
                         mb = &dec->mbs[y * dec->mb_width + x];  
   
                         DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));  
661    
662                          //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
663                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // not_coded
# Line 582  Line 672 
672                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
673                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
674                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
   
                                 DPRINTF(DPRINTF_MB, "mode %i", mb->mode);  
                                 DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);  
675                                  acpred_flag = 0;                                  acpred_flag = 0;
676    
677                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
# Line 593  Line 680 
680                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
681                                  }                                  }
682    
683                                  cbpy = get_cbpy(bs, intra);                                  if (mb->mode == MODE_STUFFING) {
684                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                          DEBUG("-- STUFFING ?");
685                                            continue;
686                                    }
687    
688                                    cbpy = get_cbpy(bs, intra);
689                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
690    
691                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
692                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          quant += dquant_table[BitstreamGetBits(bs, 2)];
                                         DPRINTF(DPRINTF_MB, "dquant %i", dquant);  
                                         quant += dquant;  
693                                          if (quant > 31) {                                          if (quant > 31) {
694                                                  quant = 31;                                                  quant = 31;
695                                          } else if (quant < 1) {                                          } else if (mb->quant < 1) {
696                                                  quant = 1;                                                  quant = 1;
697                                          }                                          }
                                         DPRINTF(DPRINTF_MB, "quant %i", quant);  
698                                  }                                  }
699                                  mb->quant = quant;                                  mb->quant = quant;
700    
# Line 631  Line 718 
718                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
719                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
720                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
721                                                                                    fcode, bound);                                                                                    fcode);
722                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
723                                                                                    fcode, bound);                                                                                    fcode);
724                                          } else {                                          } else {
725                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
726                                                                                    fcode, bound);                                                                                    fcode);
727                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
728                                                          mb->mvs[0].x;                                                          mb->mvs[0].x;
729                                                  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 644  Line 731 
731                                          }                                          }
732                                  } else if (mb->mode ==                                  } else if (mb->mode ==
733                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {
734                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);
735                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);
736                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);
737                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);
738                                  } else                  // MODE_INTRA, MODE_INTRA_Q                                  } else                  // MODE_INTRA, MODE_INTRA_Q
739                                  {                                  {
740                                          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 655  Line 742 
742                                          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 =
743                                                  0;                                                  0;
744                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
745                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold);
746                                          continue;                                          continue;
747                                  }                                  }
748    
# Line 1129  Line 1216 
1216    
1217                                          if (quant > 31) {                                          if (quant > 31) {
1218                                                  quant = 31;                                                  quant = 31;
1219                                          } else if (quant < 1) {                                          } else if (mb->quant < 1) {
1220                                                  quant = 1;                                                  quant = 1;
1221                                          }                                          }
1222                                  } else {                                  } else {
# Line 1272  Line 1359 
1359          case P_VOP:          case P_VOP:
1360                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,
1361                                             intra_dc_threshold);                                             intra_dc_threshold);
1362  #ifdef BFRAMES_DEC                  printf("P_VOP  Time=%ld\n",dec->time);
1363                  DEBUG1("P_VOP  Time=", dec->time);                  DEBUG1("P_VOP  Time=", dec->time);
 #endif  
1364                  break;                  break;
1365    
1366          case I_VOP:          case I_VOP:
1367                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);
1368  #ifdef BFRAMES_DEC                  printf("I_VOP  Time=%ld\n",dec->time);
1369                  DEBUG1("I_VOP  Time=", dec->time);                  DEBUG1("I_VOP  Time=", dec->time);
 #endif  
1370                  break;                  break;
1371    
1372          case B_VOP:          case B_VOP:
1373  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1374                  if (dec->time_pp > dec->time_bp) {                  if (dec->time_pp > dec->time_bp) {
1375                            printf("I_VOP  Time=%ld\n",dec->time);
1376                          DEBUG1("B_VOP  Time=", dec->time);                          DEBUG1("B_VOP  Time=", dec->time);
1377                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1378                  } else {                  } else {
1379                          DEBUG("broken B-frame!");                          DEBUG("broken B-frame!");
1380                  }                  }
1381  #else  #else
1382                    printf("Che minchia ne so\n");
1383                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1384  #endif  #endif
1385                  break;                  break;
# Line 1300  Line 1387 
1387          case N_VOP:                             // vop not coded          case N_VOP:                             // vop not coded
1388                  // 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
1389                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1390                    printf("N_VOP vop not coded\n");
1391                  break;                  break;
1392    
1393          default:          default:

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

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