[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, Wed Feb 19 21:59:30 2003 UTC revision 1.49.2.25, Sat Dec 13 13:52:25 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Decoder Module -   *  - Decoder Module -
5   *   *
6   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7     *               2002-2003 Peter Ross <pross@xvid.org>
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 39  Line 40 
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
# Line 53  Line 54 
54  #include "utils/timer.h"  #include "utils/timer.h"
55  #include "utils/emms.h"  #include "utils/emms.h"
56  #include "motion/motion.h"  #include "motion/motion.h"
57    #include "motion/gmc.h"
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  int  static int
65  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
66  {  {
67          /* free existing */          /* free existing */
# Line 158  Line 161 
161    
162          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);
163    
164          return XVID_ERR_OK;          return 0;
165  }  }
166    
167    
168  int  int
169  decoder_create(XVID_DEC_PARAM * param)  decoder_create(xvid_dec_create_t * create)
170  {  {
171          DECODER *dec;          DECODER *dec;
172    
173            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
174                    return XVID_ERR_VERSION;
175    
176          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
177          if (dec == NULL) {          if (dec == NULL) {
178                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
179          }          }
180    
181          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
182    
183          param->handle = dec;          dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
184            if (dec->mpeg_quant_matrices == NULL) {
185                    xvid_free(dec);
186                    return XVID_ERR_MEMORY;
187            }
188    
189            create->handle = dec;
190    
191          dec->width = param->width;          dec->width = create->width;
192          dec->height = param->height;          dec->height = create->height;
193    
194          image_null(&dec->cur);          image_null(&dec->cur);
195          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
# Line 192  Line 205 
205          dec->last_mbs = NULL;          dec->last_mbs = NULL;
206    
207          init_timer();          init_timer();
208            init_mpeg_matrix(dec->mpeg_quant_matrices);
209    
210          /* For B-frame support (used to save reference frame's time */          /* For B-frame support (used to save reference frame's time */
211          dec->frames = 0;          dec->frames = 0;
# Line 204  Line 218 
218          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
219                  return decoder_resize(dec);                  return decoder_resize(dec);
220          else          else
221                  return XVID_ERR_OK;                  return 0;
222  }  }
223    
224    
# Line 222  Line 236 
236          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
237          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
238          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
239            xvid_free(dec->mpeg_quant_matrices);
240          xvid_free(dec);          xvid_free(dec);
241    
242          write_timer();          write_timer();
243          return XVID_ERR_OK;          return 0;
244  }  }
245    
   
   
246  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
247          -1, -2, 1, 2          -1, -2, 1, 2
248  };  };
249    
   
   
   
250  /* decode an intra macroblock */  /* decode an intra macroblock */
251  void  static void
252  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
253                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
254                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 301  Line 311 
311                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
312                          start_coeff = 1;                          start_coeff = 1;
313    
314                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
315                  } else {                  } else {
316                          start_coeff = 0;                          start_coeff = 0;
317                  }                  }
# Line 322  Line 332 
332    
333                  start_timer();                  start_timer();
334                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
335                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
336                  } else {                  } else {
337                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
338                  }                  }
339                  stop_iquant_timer();                  stop_iquant_timer();
340    
# Line 361  Line 371 
371          stop_transfer_timer();          stop_transfer_timer();
372  }  }
373    
374    static void
375    decoder_mb_decode(DECODER * dec,
376                                    const uint32_t cbp,
377                                    Bitstream * bs,
378                                    uint8_t * pY_Cur,
379                                    uint8_t * pU_Cur,
380                                    uint8_t * pV_Cur,
381                                    const int reduced_resolution,
382                                    const MACROBLOCK * pMB)
383    {
384            DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);
385            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
386    
387            int stride = dec->edged_width;
388            int next_block = stride * (reduced_resolution ? 16 : 8);
389            const int stride2 = stride/2;
390            int i;
391            const uint32_t iQuant = pMB->quant;
392            const int direction = dec->alternate_vertical_scan ? 2 : 0;
393            const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;
394    
395            for (i = 0; i < 6; i++) {
396    
397                    if (cbp & (1 << (5 - i))) {     /* coded */
398    
399                            memset(block, 0, 64 * sizeof(int16_t)); /* clear */
400    
401                            start_timer();
402                            get_inter_block(bs, block, direction);
403                            stop_coding_timer();
404    
405                            start_timer();
406                            dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices);
407                            stop_iquant_timer();
408    
409                            start_timer();
410                            idct(&data[i * 64]);
411                            stop_idct_timer();
412                    }
413            }
414    
415            if (dec->interlacing && pMB->field_dct) {
416                    next_block = stride;
417                    stride *= 2;
418            }
419    
420            start_timer();
421            if (reduced_resolution) {
422                    if (cbp & 32)
423                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
424                    if (cbp & 16)
425                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
426                    if (cbp & 8)
427                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
428                    if (cbp & 4)
429                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
430                    if (cbp & 2)
431                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
432                    if (cbp & 1)
433                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
434            } else {
435                    if (cbp & 32)
436                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
437                    if (cbp & 16)
438                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
439                    if (cbp & 8)
440                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
441                    if (cbp & 4)
442                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
443                    if (cbp & 2)
444                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
445                    if (cbp & 1)
446                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
447            }
448            stop_transfer_timer();
449    }
450    
451  /* decode an inter macroblock */  /* decode an inter macroblock */
452  void  static void
453  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
454                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
455                                  const uint32_t x_pos,                                  const uint32_t x_pos,
456                                  const uint32_t y_pos,                                  const uint32_t y_pos,
                                 const uint32_t fcode,  
457                                  const uint32_t cbp,                                  const uint32_t cbp,
458                                  Bitstream * bs,                                  Bitstream * bs,
                                 const uint32_t quant,  
459                                  const uint32_t rounding,                                  const uint32_t rounding,
460                                  const int reduced_resolution)                                  const int reduced_resolution,
461                                    const int ref)
462  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
463          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
464          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
465          uint32_t i;          uint32_t i;
466          uint32_t iQuant = pMB->quant;  
467          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
468    
469          int uv_dx, uv_dy;          int uv_dx, uv_dy;
# Line 407  Line 485 
485                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
486          }          }
487    
488          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          start_timer();
489    
490                  uv_dx = mv[0].x / (1 + dec->quarterpel);          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
                 uv_dy = mv[0].y / (1 + dec->quarterpel);  
491    
492                    uv_dx = mv[0].x;
493                    uv_dy = mv[0].y;
494                    if (dec->quarterpel) {
495                            uv_dx /= 2;
496                            uv_dy /= 2;
497                    }
498                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
499                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
500    
                 start_timer();  
501                  if (reduced_resolution)                  if (reduced_resolution)
                 {  
502                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
503                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
504                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,                  else if (dec->quarterpel)
505                                                                    uv_dx, uv_dy, stride2, rounding);                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
                         interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
   
                 }  
                 else  
                 {  
                         if(dec->quarterpel) {  
                                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,  
506                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
507                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
508                          }                  else
509                          else {                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
                                 interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,  
510                                                                            mv[0].x, mv[0].y, stride, rounding);                                                                            mv[0].x, mv[0].y, stride, rounding);
                         }  
   
                         interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                         interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,  
                                                                   uv_dx, uv_dy, stride2, rounding);  
                 }  
                 stop_comp_timer();  
511    
512          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);  
                 else  
                         sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
513    
514                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                  if(dec->quarterpel) {
515                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
516                  if(dec->quarterpel)                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
517                          sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);                  } else {
518                  else                          uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
519                          sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;                          uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
520                    }
521    
522                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
523                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
524    
525                  start_timer();                  if (reduced_resolution) {
                 if (reduced_resolution)  
                 {  
526                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
527                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                    mv[0].x, mv[0].y, stride,  rounding);
528                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,                          interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
# Line 478  Line 536 
536                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,                          interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
537                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
538    
539                          /* set_block(pY_Cur, stride, 32, 32, 127); */                  } else if (dec->quarterpel) {
                 }  
                 else  
                 {  
                         if(dec->quarterpel) {  
540                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
541                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
542                                                                                    mv[0].x, mv[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
# Line 495  Line 549 
549                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
550                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
551                                                                                    mv[3].x, mv[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
552                          }                  } else {
                         else {  
553                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
554                                                                            mv[0].x, mv[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
555                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
# Line 506  Line 559 
559                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
560                                                                            mv[3].x, mv[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
561                          }                          }
562            }
563    
564                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,          /* chroma */
565            if (reduced_resolution) {
566                    interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
567                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
568                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,                  interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
569                                                                    uv_dx, uv_dy, stride2, rounding);                                                                    uv_dx, uv_dy, stride2, rounding);
                 }  
                 stop_comp_timer();  
         }  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       /* coded */  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
570                          } else {                          } else {
571                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
572                          }                                                                  uv_dx, uv_dy, stride2, rounding);
573                          stop_iquant_timer();                  interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
574                                                                    uv_dx, uv_dy, stride2, rounding);
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (reduced_resolution)  
         {  
                 if (cbp & 32)  
                         add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);  
         }  
         else  
         {  
                 if (cbp & 32)  
                         transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
                 if (cbp & 16)  
                         transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
                 if (cbp & 8)  
                         transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
                 if (cbp & 4)  
                         transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
                 if (cbp & 2)  
                         transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
                 if (cbp & 1)  
                         transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         }  
         stop_transfer_timer();  
575  }  }
576    
577  static __inline int gmc_sanitize(int value, int quarterpel, int fcode)          stop_comp_timer();
 {  
         int length = 1 << (fcode+4);  
   
 /*      if (quarterpel) value *= 2; */  
578    
579          if (value < -length)          if (cbp)
580                  return -length;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
581          else if (value >= length)                                                          reduced_resolution, pMB);
                 return length-1;  
         else return value;  
582  }  }
583    
   
584  static void  static void
585  decoder_mbgmc(DECODER * dec,  decoder_mbgmc(DECODER * dec,
586                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
# Line 601  Line 589 
589                                  const uint32_t fcode,                                  const uint32_t fcode,
590                                  const uint32_t cbp,                                  const uint32_t cbp,
591                                  Bitstream * bs,                                  Bitstream * bs,
592                                  const uint32_t quant,                                  const uint32_t rounding)
                                 const uint32_t rounding,  
                                 const int reduced_resolution)   /* no reduced res support */  
593  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
594          const uint32_t stride = dec->edged_width;          const uint32_t stride = dec->edged_width;
595          const uint32_t stride2 = stride / 2;          const uint32_t stride2 = stride / 2;
596          const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);  
         uint32_t i;  
         const uint32_t iQuant = pMB->quant;  
597          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
598          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
599          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
600    
601            NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
602    
603          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
604    
605          start_timer();          start_timer();
606    
607  /* this is where the calculations are done */  /* this is where the calculations are done */
608    
609          {          gmc_data->predict_16x16(gmc_data,
610                  pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
611                                          stride, stride2, dec->quarterpel, rounding, &dec->cur);                          stride, stride, x_pos, y_pos, rounding);
612    
613            gmc_data->predict_8x8(gmc_data,
614                            dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
615                            dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
616                            stride2, stride2, x_pos, y_pos, rounding);
617    
618            gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
619    
620                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);                  pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
621                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);                  pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
         }  
         pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;  
   
 /*  
         transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);  
         transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);  
         transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);  
 */  
622    
623            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
624    
625          stop_transfer_timer();          stop_transfer_timer();
626    
627          if (!cbp) return;          if (cbp)
628                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       /* coded */  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
629    
 /* interlace + GMC is this possible ??? */  
 /*  
   if (dec->interlacing && pMB->field_dct) {  
           next_block = stride;  
           stride *= 2;  
   }  
 */  
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
630  }  }
631    
632    
633  void  static void
634  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
635                             Bitstream * bs,                             Bitstream * bs,
636                             int reduced_resolution,                             int reduced_resolution,
# Line 705  Line 642 
642          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
643          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
644    
645          if (reduced_resolution)          if (reduced_resolution) {
         {  
646                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
647                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
648          }          }
# Line 734  Line 670 
670                          }                          }
671                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
672    
673                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
674    
675                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
676                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 761  Line 697 
697    
698                          if (dec->interlacing) {                          if (dec->interlacing) {
699                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
700                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
701                          }                          }
702    
703                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 775  Line 711 
711  }  }
712    
713    
714  void  static void
715  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
716                                    Bitstream * bs,                                    Bitstream * bs,
717                                    int x,                                    int x,
# Line 786  Line 722 
722                                    const int bound)                                    const int bound)
723  {  {
724    
725          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
726          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
727          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
728          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
729    
730          VECTOR pmv;          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
731          VECTOR mv;          VECTOR mv;
732    
         pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);  
   
733          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
734          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
735    
736          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
737    
738          mv.x += pmv.x;          mv.x += pmv.x;
739          mv.y += pmv.y;          mv.y += pmv.y;
# Line 820  Line 754 
754          ret_mv->y = mv.y;          ret_mv->y = mv.y;
755  }  }
756    
   
   
   
   
757  /* for P_VOP set gmc_warp to NULL */  /* for P_VOP set gmc_warp to NULL */
758  void  static void
759  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
760                             Bitstream * bs,                             Bitstream * bs,
761                             int rounding,                             int rounding,
# Line 835  Line 765 
765                             int intra_dc_threshold,                             int intra_dc_threshold,
766                             const WARPPOINTS *const gmc_warp)                             const WARPPOINTS *const gmc_warp)
767  {  {
   
768          uint32_t x, y;          uint32_t x, y;
769          uint32_t bound;          uint32_t bound;
770          int cp_mb, st_mb;          int cp_mb, st_mb;
771          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
772          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
773    
774          if (reduced_resolution)          if (reduced_resolution) {
         {  
775                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
776                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
777          }          }
# Line 853  Line 781 
781                                     dec->width, dec->height);                                     dec->width, dec->height);
782          stop_edges_timer();          stop_edges_timer();
783    
784          if (gmc_warp)          if (gmc_warp) {
         {  
   
785                  /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */                  /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
                 if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )  
                 {  
                         fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",  
                                 dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),  
                                 dec->sprite_warping_points);  
                 }  
   
786                  generate_GMCparameters( dec->sprite_warping_points,                  generate_GMCparameters( dec->sprite_warping_points,
787                                  (2 << dec->sprite_warping_accuracy), gmc_warp,                                  dec->sprite_warping_accuracy, gmc_warp,
788                                  dec->width, dec->height, &dec->gmc_data);                                  dec->width, dec->height, &dec->new_gmc_data);
789    
790  /* image warping is done block-based  in decoder_mbgmc(), now */  /* image warping is done block-based  in decoder_mbgmc(), now */
 /*  
         generate_GMCimage(&dec->gmc_data, &dec->refn[0],  
                                         mb_width, mb_height,  
                                         dec->edged_width, dec->edged_width/2,  
                                         fcode, dec->quarterpel, 0,  
                                         rounding, dec->mbs, &dec->gmc);  
 */  
791          }          }
792    
793          bound = 0;          bound = 0;
# Line 889  Line 801 
801                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
802                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
803    
804                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1)) {
                         {  
805                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
806                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
807                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 898  Line 809 
809                          }                          }
810                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
811    
812                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
813    
814                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
815                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */                                  uint32_t mcbpc, cbpc, cbpy, cbp;
816                          {                                  uint32_t intra, acpred_flag = 0;
                                 uint32_t mcbpc;  
                                 uint32_t cbpc;  
                                 uint32_t acpred_flag;  
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
817                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
818    
819                                  cp_mb++;                                  cp_mb++;
# Line 916  Line 821 
821                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
822                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
823    
824                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
825                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
                                 acpred_flag = 0;  
826    
827                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
828    
                                 if (intra) {  
                                         acpred_flag = BitstreamGetBit(bs);  
                                 }  
   
829                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
                                 {  
830                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
831                                  }                                  else if (intra)
832                                            acpred_flag = BitstreamGetBit(bs);
833    
834                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
835                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
836    
837                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
838    
839                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
840                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
841                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
842                                          quant += dquant;                                          quant += dquant;
843                                          if (quant > 31) {                                          if (quant > 31) {
844                                                  quant = 31;                                                  quant = 31;
845                                          } else if (quant < 1) {                                          } else if (quant < 1) {
846                                                  quant = 1;                                                  quant = 1;
847                                          }                                          }
848                                          DPRINTF(DPRINTF_MB, "quant %i", quant);                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
849                                  }                                  }
850                                  mb->quant = quant;                                  mb->quant = quant;
851    
852                                  if (dec->interlacing) {                                  if (dec->interlacing) {
853                                          if (cbp || intra) {                                          if ((cbp || intra) && !mcsel) {
854                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
855                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
856                                          }                                          }
857    
858                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
859                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
860                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
861    
862                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
863                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
864                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
865                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
866                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
867                                                  }                                                  }
868                                          }                                          }
869                                  }                                  }
870    
871                                  if (mcsel) {                                  if (mcsel) {
872                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
                                                                 rounding, reduced_resolution);  
873                                          continue;                                          continue;
874    
875                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
876    
877                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
878                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
879                                                                                    fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
                                                 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],  
                                                                                   fcode, bound);  
880                                          } else {                                          } else {
881                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
                                                                                   fcode, bound);  
882                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
883                                          }                                          }
884                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
   
885                                          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, bound);
886                                          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, bound);
887                                          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, bound);
888                                          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, bound);
889                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */
890                                  {                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
891                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;
                                                 0;  
                                         mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                 0;  
892                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
893                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound, reduced_resolution);
894                                          continue;                                          continue;
895                                  }                                  }
896    
897                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
898                                                                  rounding, reduced_resolution);                                                                  rounding, reduced_resolution, 0);
899    
900                          }                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
                         else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */  
                         {  
901                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
902                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
                                 start_timer();  
   
                                 decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,  
                                                                 rounding, reduced_resolution);  
   
                                 stop_transfer_timer();  
903    
904                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
905                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
906                                    cp_mb = 0;                                    cp_mb = 0;
907                                  }                                  }
908                                  st_mb = x+1;                                  st_mb = x+1;
909                          }                          } else {        /* not coded P_VOP macroblock */
                         else    /* not coded P_VOP macroblock */  
                         {  
910                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
911    
912                                  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;
913                                  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;
                                 /* copy macroblock directly from ref to cur */  
   
                                 start_timer();  
   
                                 if (reduced_resolution)  
                                 {  
                                         transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->refn[0].y + (32*y)*dec->edged_width + (32*x),  
                                                                          dec->edged_width);  
   
                                         transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),  
                                                                          dec->edged_width/2);  
                                 }  
                                 else  
                                 {  
                                         transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->refn[0].y + (16*y)*dec->edged_width + (16*x),  
                                                                          dec->edged_width);  
   
                                         transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),  
                                                                         dec->edged_width/2);  
   
                                         transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),  
                                                                          dec->edged_width/2);  
                                 }  
914    
915                                  stop_transfer_timer();                                  decoder_mbinter(dec, mb, x, y, 0, bs,
916                                                                    rounding, reduced_resolution, 0);
917    
918                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
919                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
# Line 1071  Line 922 
922                                  st_mb = x+1;                                  st_mb = x+1;
923                          }                          }
924                  }                  }
925    
926                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
927                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
928          }          }
# Line 1078  Line 930 
930    
931    
932  /* decode B-frame motion vector */  /* decode B-frame motion vector */
933  void  static void
934  get_b_motion_vector(DECODER * dec,  get_b_motion_vector(Bitstream * bs,
                                         Bitstream * bs,  
                                         int x,  
                                         int y,  
935                                          VECTOR * mv,                                          VECTOR * mv,
936                                          int fcode,                                          int fcode,
937                                          const VECTOR pmv)                                          const VECTOR pmv)
938  {  {
939          int scale_fac = 1 << (fcode - 1);          const int scale_fac = 1 << (fcode - 1);
940          int high = (32 * scale_fac) - 1;          const int high = (32 * scale_fac) - 1;
941          int low = ((-32) * scale_fac);          const int low = ((-32) * scale_fac);
942          int range = (64 * scale_fac);          const int range = (64 * scale_fac);
   
         int mv_x, mv_y;  
         int pmv_x, pmv_y;  
   
         pmv_x = pmv.x;  
         pmv_y = pmv.y;  
943    
944          mv_x = get_mv(bs, fcode);          int mv_x = get_mv(bs, fcode);
945          mv_y = get_mv(bs, fcode);          int mv_y = get_mv(bs, fcode);
946    
947          mv_x += pmv_x;          mv_x += pmv.x;
948          mv_y += pmv_y;          mv_y += pmv.y;
949    
950          if (mv_x < low) {          if (mv_x < low)
951                  mv_x += range;                  mv_x += range;
952          } else if (mv_x > high) {          else if (mv_x > high)
953                  mv_x -= range;                  mv_x -= range;
         }  
954    
955          if (mv_y < low) {          if (mv_y < low)
956                  mv_y += range;                  mv_y += range;
957          } else if (mv_y > high) {          else if (mv_y > high)
958                  mv_y -= range;                  mv_y -= range;
         }  
959    
960          mv->x = mv_x;          mv->x = mv_x;
961          mv->y = mv_y;          mv->y = mv_y;
962  }  }
963    
964    /* decode an B-frame direct & interpolate macroblock */
965  /* decode an B-frame forward & backward inter macroblock */  static void
 void  
 decoder_bf_mbinter(DECODER * dec,  
                                    const MACROBLOCK * pMB,  
                                    const uint32_t x_pos,  
                                    const uint32_t y_pos,  
                                    const uint32_t cbp,  
                                    Bitstream * bs,  
                                    const uint32_t quant,  
                                    const uint8_t ref)  
 {  
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         uint32_t stride = dec->edged_width;  
         uint32_t stride2 = stride / 2;  
         uint32_t next_block = stride * 8;  
         uint32_t i;  
         uint32_t iQuant = pMB->quant;  
         uint8_t *pY_Cur, *pU_Cur, *pV_Cur;  
         int uv_dx, uv_dy;  
   
         pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);  
         pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);  
         pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);  
   
   
         if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
   
                 if (dec->quarterpel)  
                 {  
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
   
                 uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];  
                 uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];  
         } else {  
                 int sum;  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                 else  
                         sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
   
                 uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                 else  
                         sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
         }  
   
         start_timer();  
         if(dec->quarterpel) {  
                 interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,  
                                                                     dec->qtmp.y + 128, 16*x_pos, 16*y_pos,  
                                                                     pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
         }  
         else {  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,  
                                                           pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,  
                                                       pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,  
                                                           pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);  
                 interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                           pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);  
         }  
   
         interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                   uv_dx, uv_dy, stride2, 0);  
         stop_comp_timer();  
   
         for (i = 0; i < 6; i++) {  
                 int direction = dec->alternate_vertical_scan ? 2 : 0;  
   
                 if (cbp & (1 << (5 - i)))       /* coded */  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
 }  
   
 /* decode an B-frame direct &  inter macroblock */  
 void  
966  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
967                                                             IMAGE forward,                                                             IMAGE forward,
968                                                             IMAGE backward,                                                             IMAGE backward,
969                                                             const MACROBLOCK * pMB,                                                             const MACROBLOCK * pMB,
970                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
971                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
972                                                             Bitstream * bs)                                                                  Bitstream * bs,
973                                                                    const int direct)
974  {  {
   
         DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
975          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
976          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
         uint32_t next_block = stride * 8;  
         uint32_t iQuant = pMB->quant;  
977          int uv_dx, uv_dy;          int uv_dx, uv_dy;
978          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
         uint32_t i;  
979          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
980      const uint32_t cbp = pMB->cbp;      const uint32_t cbp = pMB->cbp;
981    
# Line 1276  Line 983 
983          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
984          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
985    
986            if (!direct) {
         if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {  
987                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
988                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
989    
990                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
991                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
992    
993                  if (dec->quarterpel)                  if (dec->quarterpel) {
                 {  
994                          uv_dx /= 2;                          uv_dx /= 2;
995                          uv_dy /= 2;                          uv_dy /= 2;
   
996                          b_uv_dx /= 2;                          b_uv_dx /= 2;
997                          b_uv_dy /= 2;                          b_uv_dy /= 2;
998                  }                  }
# Line 1298  Line 1002 
1002    
1003                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];                  b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1004                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];                  b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
         } else {  
                 int sum;  
1005    
1006                  if(dec->quarterpel)          } else {
1007                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);                  if(dec->quarterpel) {
1008                  else                          uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1009                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1010                            b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);
1011                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                          b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);
1012                    } else {
1013                  if(dec->quarterpel)                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1014                          sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1015                  else                          b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1016                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
   
                 uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
   
   
                 if(dec->quarterpel)  
                         sum = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2);  
                 else  
                         sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;  
   
                 b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];  
   
                 if(dec->quarterpel)  
                         sum = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2);  
                 else  
                         sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;  
   
                 b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];  
1017          }          }
1018    
1019                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1020                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1021                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1022                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1023            }
1024    
1025          start_timer();          start_timer();
1026          if(dec->quarterpel) {          if(dec->quarterpel) {
1027                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1028                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1029                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1030                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1031                  else {                  } else {
1032                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1033                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1034                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
# Line 1352  Line 1042 
1042                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1043                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1044                  }                  }
1045          }          } else {
         else {  
1046                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1047                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1048                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
# Line 1361  Line 1050 
1050                  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,
1051                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1052                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1053                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
                                                           0);  
1054          }          }
1055    
1056          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 1372  Line 1060 
1060    
1061    
1062          if(dec->quarterpel) {          if(dec->quarterpel) {
1063                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))                  if(!direct) {
1064                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1065                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1066                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1067                  else {                  } else {
1068                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1069                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                              dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1070                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
# Line 1390  Line 1078 
1078                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                              dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1079                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                              pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1080                  }                  }
1081          }          } else {
         else {  
1082                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1083                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1084                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1085                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                          16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
                                                           0);  
1086                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1087                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                          16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
                                                           stride, 0);  
1088                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1089                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,                                                          16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
                                                           stride, 0);  
1090          }          }
1091    
1092          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
# Line 1442  Line 1126 
1126    
1127          stop_comp_timer();          stop_comp_timer();
1128    
1129          for (i = 0; i < 6; i++) {          if (cbp)
1130                  int direction = dec->alternate_vertical_scan ? 2 : 0;                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
   
                 if (cbp & (1 << (5 - i)))       /* coded */  
                 {  
                         memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, &block[i * 64], direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         if (dec->quant_type == 0) {  
                                 dequant_inter(&data[i * 64], &block[i * 64], iQuant);  
                         } else {  
                                 dequant4_inter(&data[i * 64], &block[i * 64], iQuant);  
                         }  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
   
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
   
         start_timer();  
         if (cbp & 32)  
                 transfer_16to8add(pY_Cur, &data[0 * 64], stride);  
         if (cbp & 16)  
                 transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);  
         if (cbp & 8)  
                 transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);  
         if (cbp & 4)  
                 transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);  
         if (cbp & 2)  
                 transfer_16to8add(pU_Cur, &data[4 * 64], stride2);  
         if (cbp & 1)  
                 transfer_16to8add(pV_Cur, &data[5 * 64], stride2);  
         stop_transfer_timer();  
1131  }  }
1132    
   
1133  /* for decode B-frame dbquant */  /* for decode B-frame dbquant */
1134  int32_t __inline  static __inline int32_t
1135  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1136  {  {
1137          if (!BitstreamGetBit(bs))      /*  '0' */          if (!BitstreamGetBit(bs))      /*  '0' */
# Line 1502  Line 1143 
1143  }  }
1144    
1145  /*  /*
1146   * For decode B-frame mb_type   * decode B-frame mb_type
1147   * bit   ret_value   * bit   ret_value
1148   * 1        0   * 1        0
1149   * 01       1   * 01       1
1150   * 001      2   * 001      2
1151   * 0001     3   * 0001     3
1152   */   */
1153  int32_t __inline  static int32_t __inline
1154  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1155  {  {
1156          int32_t mb_type;          int32_t mb_type;
1157    
1158          for (mb_type = 0; mb_type <= 3; mb_type++) {          for (mb_type = 0; mb_type <= 3; mb_type++)
1159                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
                         break;  
         }  
   
         if (mb_type <= 3)  
1160                  return (mb_type);                  return (mb_type);
1161          else  
1162                  return (-1);          return -1;
1163  }  }
1164    
1165  void  static void
1166  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1167                             Bitstream * bs,                             Bitstream * bs,
1168                             int quant,                             int quant,
# Line 1535  Line 1172 
1172          uint32_t x, y;          uint32_t x, y;
1173          VECTOR mv;          VECTOR mv;
1174          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1175  #ifdef BFRAMES_DEC_DEBUG          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1176          FILE *fp;          int i;
         static char first=0;  
 #define BFRAME_DEBUG    if (!first && fp){ \  
                 fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \  
         }  
 #endif  
1177    
1178          start_timer();          start_timer();
1179          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
# Line 1550  Line 1182 
1182                                     dec->width, dec->height);                                     dec->width, dec->height);
1183          stop_edges_timer();          stop_edges_timer();
1184    
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 fp=fopen("C:\\XVIDDBG.TXT","w");  
         }  
 #endif  
   
1185          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1186                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
1187                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1188                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1189                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1190                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1191                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1192                            uint32_t intra_dc_threshold; /* fake variable */
1193    
1194                            if (check_resync_marker(bs, fcode_max  - 1)) {
1195                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1196                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1197                                    x = bound % dec->mb_width;
1198                                    y = bound / dec->mb_width;
1199                                    /* reset predicted macroblocks */
1200                                    dec->p_fmv = dec->p_bmv = zeromv;
1201                            }
1202    
1203                          mv =                          mv =
1204                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1205                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1206                            mb->quant = quant;
1207    
1208                          /*                          /*
1209                           * skip if the co-located P_VOP macroblock is not coded                           * skip if the co-located P_VOP macroblock is not coded
# Line 1574  Line 1212 
1212                           */                           */
1213    
1214                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
                                 /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */  
1215                                  mb->cbp = 0;                                  mb->cbp = 0;
1216  #ifdef BFRAMES_DEC_DEBUG                                  mb->mode = MODE_FORWARD;
1217                                  mb->mb_type = MODE_NOT_CODED;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
         BFRAME_DEBUG  
 #endif  
                                 mb->mb_type = MODE_FORWARD;  
                                 mb->quant = last_mb->quant;  
                                 /*  
                                   mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  
                                   mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;  
                                 */  
   
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);  
1218                                  continue;                                  continue;
1219                          }                          }
1220    
1221                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1222                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1223    
1224                                  mb->mb_type = get_mbtype(bs);                                  mb->mode = get_mbtype(bs);
1225    
1226                                  if (!modb2) {   /* modb=='00' */                                  if (!modb2)             /* modb=='00' */
1227                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1228                                  } else {                                  else
1229                                          mb->cbp = 0;                                          mb->cbp = 0;
                                 }  
                                 if (mb->mb_type && mb->cbp) {  
                                         quant += get_dbquant(bs);  
1230    
1231                                          if (quant > 31) {                                  if (mb->mode && mb->cbp) {
1232                                            quant += get_dbquant(bs);
1233                                            if (quant > 31)
1234                                                  quant = 31;                                                  quant = 31;
1235                                          } else if (quant < 1) {                                          else if (quant < 1)
1236                                                  quant = 1;                                                  quant = 1;
1237                                          }                                          }
1238                                    mb->quant = quant;
1239    
1240                                    if (dec->interlacing) {
1241                                            if (mb->cbp) {
1242                                                    mb->field_dct = BitstreamGetBit(bs);
1243                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1244                                  }                                  }
1245    
1246                                            if (mb->mode) {
1247                                                    mb->field_pred = BitstreamGetBit(bs);
1248                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1249    
1250                                                    if (mb->field_pred) {
1251                                                            mb->field_for_top = BitstreamGetBit(bs);
1252                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1253                                                            mb->field_for_bot = BitstreamGetBit(bs);
1254                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1255                                                    }
1256                                            }
1257                                    }
1258    
1259                          } else {                          } else {
1260                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mode = MODE_DIRECT_NONE_MV;
1261                                  mb->cbp = 0;                                  mb->cbp = 0;
1262                          }                          }
1263    
1264                          mb->quant = quant;                          switch (mb->mode) {
                         mb->mode = MODE_INTER4V;  
                         /* DEBUG1("Switch bm_type=",mb->mb_type); */  
   
 #ifdef BFRAMES_DEC_DEBUG  
         BFRAME_DEBUG  
 #endif  
   
                         switch (mb->mb_type) {  
1265                          case MODE_DIRECT:                          case MODE_DIRECT:
1266                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(bs, &mv, 1, zeromv);
1267    
1268                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
                                 {  
                                         const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  
                                         int i;  
   
1269                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1270                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);
                                                                       / TRD + mv.x);  
1271                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1272                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD
                                                                                   / TRD  
1273                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);
1274                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);
                                                                       / TRD + mv.y);  
1275                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1276                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD
                                                                                   / TRD  
1277                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
1278                                          }                                          }
1279                                          /* DEBUG("B-frame Direct!\n"); */  
                                 }  
1280                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1281                                                                                             mb, x, y, bs);                                                                                                  mb, x, y, bs, 1);
1282                                  break;                                  break;
1283    
1284                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1285                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
                                                                         dec->p_fmv);  
1286                                  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];
1287    
1288                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);
1289                                                                          fcode_backward, dec->p_bmv);                                  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];  
1290    
1291                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1292                                                                                             mb, x, y, bs);                                                                                          mb, x, y, bs, 0);
                                 /* DEBUG("B-frame Bidir!\n"); */  
1293                                  break;                                  break;
1294    
1295                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1296                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);
                                                                         dec->p_bmv);  
1297                                  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];
1298    
1299                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);  
                                 /* DEBUG("B-frame Backward!\n"); */  
1300                                  break;                                  break;
1301    
1302                          case MODE_FORWARD:                          case MODE_FORWARD:
1303                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);
                                                                         dec->p_fmv);  
1304                                  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];
1305    
1306                                  mb->mode = MODE_INTER;                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
                                 decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);  
                                 /* DEBUG("B-frame Forward!\n"); */  
1307                                  break;                                  break;
1308    
1309                          default:                          default:
1310                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1311                          }                          }
1312                  } /* End of for */                  } /* End of for */
1313          }          }
   
 #ifdef BFRAMES_DEC_DEBUG  
         if (!first){  
                 first=1;  
                 if (fp)  
                         fclose(fp);  
         }  
 #endif  
 }  
   
 /* swap two MACROBLOCK array */  
 void  
 mb_swap(MACROBLOCK ** mb1,  
                 MACROBLOCK ** mb2)  
 {  
         MACROBLOCK *temp = *mb1;  
   
         *mb1 = *mb2;  
         *mb2 = temp;  
1314  }  }
1315    
   
1316  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1317  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1318                                          const XVID_DEC_FRAME * frame, int pp_disable)                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type)
1319  {  {
1320            if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV) && mbs != NULL)     /* post process */
         if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */  
1321          {          {
1322                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1323                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1324                  image_deblock_rrv(&dec->tmp, dec->edged_width,                  image_deblock(&dec->tmp, dec->edged_width,
1325                                                  mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                                  mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1326                                                  8, frame->general);                                            frame->general);
1327                  img = &dec->tmp;                  img = &dec->tmp;
1328          }          }
1329    
1330          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1331                                   dec->edged_width, frame->image, frame->stride,                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1332                                   frame->colorspace, dec->interlacing);                                   frame->output.csp, dec->interlacing);
1333    
1334            if (stats) {
1335                    stats->type = coding2type(coding_type);
1336                    stats->data.vop.time_base = (int)dec->time_base;
1337                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1338            }
1339  }  }
1340    
1341    
1342  int  int
1343  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1344                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1345  {  {
1346    
1347          Bitstream bs;          Bitstream bs;
# Line 1748  Line 1352 
1352          uint32_t fcode_backward;          uint32_t fcode_backward;
1353          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1354          WARPPOINTS gmc_warp;          WARPPOINTS gmc_warp;
1355          int vop_type;          int coding_type;
1356          int success = 0;          int success, output, seen_something;
         int output = 0;  
         int seen_something = 0;  
1357    
1358          start_global_timer();          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1359                    return XVID_ERR_VERSION;
1360    
1361          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);          start_global_timer();
         dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;  
1362    
1363          if ((frame->general & XVID_DEC_DISCONTINUITY))          dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1364            if ((frame->general & XVID_DISCONTINUITY))
1365                  dec->frames = 0;                  dec->frames = 0;
1366            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1367    
1368          if (frame->length < 0)  /* decoder flush */          if (frame->length < 0) {        /* decoder flush */
1369          {                  int ret;
1370                  /* 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
1371                      we have a reference frame, then outout the reference frame */                      we have a reference frame, then outout the reference frame */
1372                  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) {
1373                  {                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1374                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);                          dec->frames = 0;
1375                          output = 1;                          ret = 0;
1376                  }                  } else {
1377                            if (stats) stats->type = XVID_TYPE_NOTHING;
1378                  frame->length = 0;                          ret = XVID_ERR_END;
                 if (stats)  
                 {  
                         stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;  
                         stats->data.vop.time_base = (int)dec->time_base;  
                         stats->data.vop.time_increment = 0;     /* XXX: todo */  
1379                  }                  }
1380    
1381                  emms();                  emms();
   
1382                  stop_global_timer();                  stop_global_timer();
1383                  return XVID_ERR_OK;                  return ret;
1384          }          }
1385    
1386          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
# Line 1790  Line 1388 
1388          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1389          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1390          {          {
                 if (stats)  
                         stats->notify = XVID_DEC_VOP;  
                 frame->length = 1;  
1391                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1392                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1393                    if (stats) stats->type = XVID_TYPE_NOTHING;
1394                  emms();                  emms();
1395                  return XVID_ERR_OK;                  return 1;       /* one byte consumed */
1396          }          }
1397    
1398            success = 0;
1399            output = 0;
1400            seen_something = 0;
1401    
1402  repeat:  repeat:
1403    
1404          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1405                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1406    
1407          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1408                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1409    
1410          if (vop_type == -1)          if (coding_type == -1) { /* nothing */
         {  
1411                  if (success) goto done;                  if (success) goto done;
1412                    if (stats) stats->type = XVID_TYPE_NOTHING;
1413                  emms();                  emms();
1414                  return XVID_ERR_FAIL;                  return BitstreamPos(&bs)/8;
1415          }          }
1416    
1417          if (vop_type == -2 || vop_type == -3)          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1418          {  
1419                  if (vop_type == -3)                  if (coding_type == -3)
1420                          decoder_resize(dec);                          decoder_resize(dec);
1421    
1422                  if (stats)                  if (stats) {
1423                  {                          stats->type = XVID_TYPE_VOL;
                         stats->notify = XVID_DEC_VOL;  
1424                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1425                          if (dec->interlacing)                          /*XXX: if (dec->interlacing)
1426                                  stats->data.vol.general |= XVID_INTERLACING;                                  stats->data.vol.general |= ++INTERLACING; */
1427                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1428                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1429                          stats->data.vol.aspect_ratio = dec->aspect_ratio;                          stats->data.vol.par = dec->aspect_ratio;
1430                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1431                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
                         frame->length = BitstreamPos(&bs) / 8;  
1432                          emms();                          emms();
1433                          return XVID_ERR_OK;                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1434                  }                  }
1435                  goto repeat;                  goto repeat;
1436          }          }
1437    
1438          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 */
1439    
   
1440          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1441          if (dec->packed_mode && vop_type == N_VOP)          if (dec->packed_mode && coding_type == N_VOP) {
1442          {                  if (dec->low_delay_default && dec->frames > 0) {
1443                  if (dec->low_delay_default && dec->frames > 0)                          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, dec->last_reduced_resolution);  
1444                          output = 1;                          output = 1;
1445                  }                  }
1446                  /* ignore otherwise */                  /* ignore otherwise */
1447          }          } else if (coding_type != B_VOP) {
1448          else if (vop_type != B_VOP)                  switch(coding_type) {
         {  
                 switch(vop_type)  
                 {  
1449                  case I_VOP :                  case I_VOP :
1450                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1451                          break;                          break;
# Line 1866  Line 1458 
1458                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1459                          break;                          break;
1460                  case N_VOP :                  case N_VOP :
1461                            /* XXX: not_coded vops are not used for forward prediction */
1462                            /* we should not swap(last_mbs,mbs) */
1463                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1464                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1465                          break;                          break;
1466                  }                  }
1467    
1468                  if (reduced_resolution)                  if (reduced_resolution) {
                 {  
1469                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1470                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1471                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);                                  16, 0);
1472                  }                  }
1473    
1474                  /* 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 */
1475                  if (!(dec->low_delay_default && dec->packed_mode))                  if (!(dec->low_delay_default && dec->packed_mode)) {
1476                  {                          if (dec->low_delay) {
1477                          if (dec->low_delay)                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
                         {  
                                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
1478                                  output = 1;                                  output = 1;
1479                          }                          } else if (dec->frames > 0)     { /* is the reference frame valid? */
                         else if (dec->frames > 0)       /* is the reference frame valid? */  
                         {  
1480                                  /* output the reference frame */                                  /* output the reference frame */
1481                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1482                                  output = 1;                                  output = 1;
1483                          }                          }
1484                  }                  }
1485    
1486                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1487                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1488                  mb_swap(&dec->mbs, &dec->last_mbs);                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1489                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
1490                    dec->last_coding_type = coding_type;
1491    
1492                  dec->frames++;                  dec->frames++;
1493                  seen_something = 1;                  seen_something = 1;
1494    
1495          }else{  /* B_VOP */          }else{  /* B_VOP */
1496    
1497                  if (dec->low_delay)                  if (dec->low_delay) {
1498                  {                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
                         DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");  
1499                          dec->low_delay = 1;                          dec->low_delay = 1;
1500                  }                  }
1501    
1502                  if (dec->frames < 2)                  if (dec->frames < 2) {
                 {  
1503                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1504                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1505                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
1506                            stats->type = XVID_TYPE_NOTHING;
1507                  }else if (dec->time_pp <= dec->time_bp) {                  }else if (dec->time_pp <= dec->time_bp) {
1508                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1509                          decoded in vfw. */                          decoded in vfw. */
1510                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1511                                                  "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);
1512                            stats->type = XVID_TYPE_NOTHING;
1513                  }else{                  }else{
1514                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1515                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type);
1516                  }                  }
1517    
                 decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);  
1518                  output = 1;                  output = 1;
1519                  dec->frames++;                  dec->frames++;
1520          }          }
# Line 1931  Line 1522 
1522          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1523    
1524          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1525          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
         {  
1526                  success = 1;                  success = 1;
1527                  goto repeat;                  goto repeat;
1528          }          }
# Line 1941  Line 1531 
1531    
1532          /* low_delay_default mode: if we've gotten here without outputting anything,          /* low_delay_default mode: if we've gotten here without outputting anything,
1533             then output the recently decoded frame, or print an error message  */             then output the recently decoded frame, or print an error message  */
1534          if (dec->low_delay_default && output == 0)          if (dec->low_delay_default && output == 0) {
1535          {                  if (dec->packed_mode && seen_something) {
                 if (dec->packed_mode && seen_something)  
                 {  
1536                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1537                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type);
1538                          output = 1;                  } else {
                 }  
                 else  
                 {  
1539                          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);
1540                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1541                                  "warning: nothing to output");                                  "warning: nothing to output");
1542                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1543                                  "bframe decoder lag");                                  "bframe decoder lag");
1544    
1545                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP);
1546                            if (stats) stats->type = XVID_TYPE_NOTHING;
1547                  }                  }
1548          }          }
1549    
         frame->length = BitstreamPos(&bs) / 8;  
   
         if (stats)  
         {  
                 stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;  
                 stats->data.vop.time_base = (int)dec->time_base;  
                 stats->data.vop.time_increment = 0;     /* XXX: todo */  
         }  
   
1550          emms();          emms();
   
1551          stop_global_timer();          stop_global_timer();
1552    
1553          return XVID_ERR_OK;          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */
1554  }  }

Legend:
Removed from v.1.49  
changed lines
  Added in v.1.49.2.25

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