[cvs] / xvidcore / src / decoder.c Repository:
ViewVC logotype

Diff of /xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.49.2.30, Tue Jan 27 09:24:49 2004 UTC revision 1.50, Mon Jun 9 17:02:38 2003 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Decoder Module -   *  - Decoder Module -
5   *   *
6   *  Copyright(C) 2002      MinChen <chenm001@163.com>   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
  *               2002-2003 Peter Ross <pross@xvid.org>  
7   *   *
8   *  This program is free software ; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
9   *  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 40  Line 39 
39  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
40  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
41    
42  #include "quant/quant.h"  #include "quant/quant_h263.h"
43  #include "quant/quant_matrix.h"  #include "quant/quant_mpeg4.h"
44  #include "dct/idct.h"  #include "dct/idct.h"
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
# Line 54  Line 53 
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55  #include "motion/motion.h"  #include "motion/motion.h"
 #include "motion/gmc.h"  
56    
57  #include "image/image.h"  #include "image/image.h"
58  #include "image/colorspace.h"  #include "image/colorspace.h"
 #include "image/postprocessing.h"  
59  #include "utils/mem_align.h"  #include "utils/mem_align.h"
60    
61  static int  int
62  decoder_resize(DECODER * dec)  decoder_resize(DECODER * dec)
63  {  {
64          /* free existing */          /* free existing */
# Line 161  Line 158 
158    
159          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);
160    
161          return 0;          return XVID_ERR_OK;
162  }  }
163    
164    
165  int  int
166  decoder_create(xvid_dec_create_t * create)  decoder_create(XVID_DEC_PARAM * param)
167  {  {
168          DECODER *dec;          DECODER *dec;
169    
         if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */  
                 return XVID_ERR_VERSION;  
   
170          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
171          if (dec == NULL) {          if (dec == NULL) {
172                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
173          }          }
   
174          memset(dec, 0, sizeof(DECODER));          memset(dec, 0, sizeof(DECODER));
175    
176          dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);          param->handle = dec;
         if (dec->mpeg_quant_matrices == NULL) {  
                 xvid_free(dec);  
                 return XVID_ERR_MEMORY;  
         }  
   
         create->handle = dec;  
177    
178          dec->width = create->width;          dec->width = param->width;
179          dec->height = create->height;          dec->height = param->height;
180    
181          image_null(&dec->cur);          image_null(&dec->cur);
182          image_null(&dec->refn[0]);          image_null(&dec->refn[0]);
# Line 205  Line 192 
192          dec->last_mbs = NULL;          dec->last_mbs = NULL;
193    
194          init_timer();          init_timer();
         init_postproc(&dec->postproc);  
         init_mpeg_matrix(dec->mpeg_quant_matrices);  
195    
196          /* For B-frame support (used to save reference frame's time */          /* For B-frame support (used to save reference frame's time */
197          dec->frames = 0;          dec->frames = 0;
# Line 219  Line 204 
204          if (dec->fixed_dimensions)          if (dec->fixed_dimensions)
205                  return decoder_resize(dec);                  return decoder_resize(dec);
206          else          else
207                  return 0;                  return XVID_ERR_OK;
208  }  }
209    
210    
# Line 237  Line 222 
222          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
223          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
         xvid_free(dec->mpeg_quant_matrices);  
225          xvid_free(dec);          xvid_free(dec);
226    
227          write_timer();          write_timer();
228          return 0;          return XVID_ERR_OK;
229  }  }
230    
231    
232    
233  static const int32_t dquant_table[4] = {  static const int32_t dquant_table[4] = {
234          -1, -2, 1, 2          -1, -2, 1, 2
235  };  };
236    
237    
238    
239    
240  /* decode an intra macroblock */  /* decode an intra macroblock */
241  static void  void
242  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
243                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
244                                  const uint32_t x_pos,                                  const uint32_t x_pos,
# Line 312  Line 301 
301                          block[i * 64 + 0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
302                          start_coeff = 1;                          start_coeff = 1;
303    
304                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);                          DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif);
305                  } else {                  } else {
306                          start_coeff = 0;                          start_coeff = 0;
307                  }                  }
# Line 333  Line 322 
322    
323                  start_timer();                  start_timer();
324                  if (dec->quant_type == 0) {                  if (dec->quant_type == 0) {
325                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);                          dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
326                  } else {                  } else {
327                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);                          dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler);
328                  }                  }
329                  stop_iquant_timer();                  stop_iquant_timer();
330    
# Line 372  Line 361 
361          stop_transfer_timer();          stop_transfer_timer();
362  }  }
363    
 static void  
 decoder_mb_decode(DECODER * dec,  
                                 const uint32_t cbp,  
                                 Bitstream * bs,  
                                 uint8_t * pY_Cur,  
                                 uint8_t * pU_Cur,  
                                 uint8_t * pV_Cur,  
                                 const int reduced_resolution,  
                                 const MACROBLOCK * pMB)  
 {  
         DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE);  
         DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);  
   
         int stride = dec->edged_width;  
         int next_block = stride * (reduced_resolution ? 16 : 8);  
         const int stride2 = stride/2;  
         int i;  
         const uint32_t iQuant = pMB->quant;  
         const int direction = dec->alternate_vertical_scan ? 2 : 0;  
         const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter;  
   
         for (i = 0; i < 6; i++) {  
   
                 if (cbp & (1 << (5 - i))) {     /* coded */  
   
                         memset(block, 0, 64 * sizeof(int16_t)); /* clear */  
   
                         start_timer();  
                         get_inter_block(bs, block, direction);  
                         stop_coding_timer();  
   
                         start_timer();  
                         dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices);  
                         stop_iquant_timer();  
   
                         start_timer();  
                         idct(&data[i * 64]);  
                         stop_idct_timer();  
                 }  
         }  
364    
         if (dec->interlacing && pMB->field_dct) {  
                 next_block = stride;  
                 stride *= 2;  
         }  
365    
         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();  
 }  
366    
367  /* decode an inter macroblock */  /* decode an inter macroblock */
368  static void  void
369  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
370                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
371                                  const uint32_t x_pos,                                  const uint32_t x_pos,
372                                  const uint32_t y_pos,                                  const uint32_t y_pos,
373                                    const uint32_t fcode,
374                                  const uint32_t cbp,                                  const uint32_t cbp,
375                                  Bitstream * bs,                                  Bitstream * bs,
376                                    const uint32_t quant,
377                                  const uint32_t rounding,                                  const uint32_t rounding,
378                                  const int reduced_resolution,                                  const int reduced_resolution)
                                 const int ref)  
379  {  {
380    
381            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
382            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
383    
384          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
385          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
386            uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
387          uint32_t i;          uint32_t i;
388            uint32_t iQuant = pMB->quant;
389          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
390    
391          int uv_dx, uv_dy;          int uv_dx, uv_dy;
# Line 486  Line 407 
407                          mv[i] = pMB->mvs[i];                          mv[i] = pMB->mvs[i];
408          }          }
409    
410          start_timer();          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
411    
412          if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */                  uv_dx = mv[0].x / (1 + dec->quarterpel);
413                    uv_dy = mv[0].y / (1 + dec->quarterpel);
414    
                 uv_dx = mv[0].x;  
                 uv_dy = mv[0].y;  
                 if (dec->quarterpel) {  
                         uv_dx /= 2;  
                         uv_dy /= 2;  
                 }  
415                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
416                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
417    
418                    start_timer();
419                  if (reduced_resolution)                  if (reduced_resolution)
420                    {
421                          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,
422                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
423                  else if (dec->quarterpel)                          interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
424                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,                                                                    uv_dx, uv_dy, stride2, rounding);
425                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
426                                                                      uv_dx, uv_dy, stride2, rounding);
427    
428                    }
429                    else
430                    {
431                            if(dec->quarterpel) {
432                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
433                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
434                                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
435                  else                          }
436                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,                          else {
437                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
438                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
439                            }
440    
441                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
442                                                                      uv_dx, uv_dy, stride2, rounding);
443                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
444                                                                      uv_dx, uv_dy, stride2, rounding);
445                    }
446                    stop_comp_timer();
447    
448          } else {        /* MODE_INTER4V */          } else {        /* MODE_INTER4V */
449                    int sum;
450    
451                  if(dec->quarterpel) {                  if(dec->quarterpel)
452                          uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
453                          uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);                  else
454                  } else {                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
                         uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;  
                         uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;  
                 }  
455    
456                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
                 uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];  
457    
458                  if (reduced_resolution) {                  if(dec->quarterpel)
459                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
460                    else
461                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
462    
463                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
464    
465                    start_timer();
466                    if (reduced_resolution)
467                    {
468                          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,
469                                                                  mv[0].x, mv[0].y, stride, rounding);                                                                  mv[0].x, mv[0].y, stride, rounding);
470                          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 537  Line 478 
478                          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,
479                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
480    
481                  } else if (dec->quarterpel) {                          /* set_block(pY_Cur, stride, 32, 32, 127); */
482                    }
483                    else
484                    {
485                            if(dec->quarterpel) {
486                          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,
487                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
488                                                                          mv[0].x, mv[0].y, stride, rounding);                                                                          mv[0].x, mv[0].y, stride, rounding);
# Line 550  Line 495 
495                          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,
496                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                          dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
497                                                                          mv[3].x, mv[3].y, stride, rounding);                                                                          mv[3].x, mv[3].y, stride, rounding);
498                  } else {                          }
499                            else {
500                          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,
501                                                                  mv[0].x, mv[0].y, stride, rounding);                                                                  mv[0].x, mv[0].y, stride, rounding);
502                          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 560  Line 506 
506                          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,
507                                                                  mv[3].x, mv[3].y, stride, rounding);                                                                  mv[3].x, mv[3].y, stride, rounding);
508                  }                  }
         }  
509    
510          /* chroma */                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u , 8 * x_pos, 8 * y_pos,
         if (reduced_resolution) {  
                 interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,  
511                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
512                  interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,                          interpolate8x8_switch(dec->cur.v, dec->refn[0].v , 8 * x_pos, 8 * y_pos,
513                                                                  uv_dx, uv_dy, stride2, rounding);                                                                  uv_dx, uv_dy, stride2, rounding);
514                    }
515                    stop_comp_timer();
516            }
517    
518            for (i = 0; i < 6; i++) {
519                    int direction = dec->alternate_vertical_scan ? 2 : 0;
520    
521                    if (cbp & (1 << (5 - i)))       /* coded */
522                    {
523                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
524    
525                            start_timer();
526                            get_inter_block(bs, &block[i * 64], direction);
527                            stop_coding_timer();
528    
529                            start_timer();
530                            if (dec->quant_type == 0) {
531                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
532          } else {          } else {
533                  interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,                                  dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
                                                                 uv_dx, uv_dy, stride2, rounding);  
                 interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,  
                                                                 uv_dx, uv_dy, stride2, rounding);  
534          }          }
535                            stop_iquant_timer();
536    
537          stop_comp_timer();                          start_timer();
538                            idct(&data[i * 64]);
539                            stop_idct_timer();
540                    }
541            }
542    
543            if (dec->interlacing && pMB->field_dct) {
544                    next_block = stride;
545                    stride *= 2;
546            }
547    
548            start_timer();
549            if (reduced_resolution)
550            {
551                    if (cbp & 32)
552                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
553                    if (cbp & 16)
554                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
555                    if (cbp & 8)
556                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
557                    if (cbp & 4)
558                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
559                    if (cbp & 2)
560                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
561                    if (cbp & 1)
562                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
563            }
564            else
565            {
566                    if (cbp & 32)
567                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
568                    if (cbp & 16)
569                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
570                    if (cbp & 8)
571                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
572                    if (cbp & 4)
573                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
574                    if (cbp & 2)
575                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
576                    if (cbp & 1)
577                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
578            }
579            stop_transfer_timer();
580    }
581    
582    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
583    {
584            int length = 1 << (fcode+4);
585    
586    /*      if (quarterpel) value *= 2; */
587    
588          if (cbp)          if (value < -length)
589                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,                  return -length;
590                                                          reduced_resolution, pMB);          else if (value >= length)
591                    return length-1;
592            else return value;
593  }  }
594    
595    
596  static void  static void
597  decoder_mbgmc(DECODER * dec,  decoder_mbgmc(DECODER * dec,
598                                  MACROBLOCK * const pMB,                                  MACROBLOCK * const pMB,
# Line 590  Line 601 
601                                  const uint32_t fcode,                                  const uint32_t fcode,
602                                  const uint32_t cbp,                                  const uint32_t cbp,
603                                  Bitstream * bs,                                  Bitstream * bs,
604                                  const uint32_t rounding)                                  const uint32_t quant,
605                                    const uint32_t rounding,
606                                    const int reduced_resolution)   /* no reduced res support */
607  {  {
608    
609            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
610            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
611    
612          const uint32_t stride = dec->edged_width;          const uint32_t stride = dec->edged_width;
613          const uint32_t stride2 = stride / 2;          const uint32_t stride2 = stride / 2;
614            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
615            uint32_t i;
616            const uint32_t iQuant = pMB->quant;
617          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);
618          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);
619          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);
620    
         NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;  
   
621          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;
622    
623          start_timer();          start_timer();
624    
625  /* this is where the calculations are done */  /* this is where the calculations are done */
626    
627          gmc_data->predict_16x16(gmc_data,          {
628                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,                  pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
629                          stride, stride, x_pos, y_pos, rounding);                                          stride, stride2, dec->quarterpel, rounding, &dec->cur);
   
         gmc_data->predict_8x8(gmc_data,  
                         dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,  
                         dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,  
                         stride2, stride2, x_pos, y_pos, rounding);  
   
         gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);  
630    
631          pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);          pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632          pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);          pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634          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;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643          stop_transfer_timer();          stop_transfer_timer();
644    
645          if (cbp)          if (!cbp) return;
646                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);  
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680            if (cbp & 32)
681                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682            if (cbp & 16)
683                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
684            if (cbp & 8)
685                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
686            if (cbp & 4)
687                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
688            if (cbp & 2)
689                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
690            if (cbp & 1)
691                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
692            stop_transfer_timer();
693  }  }
694    
695    
696  static void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
698                                  Bitstream * bs,                                  Bitstream * bs,
699                                  int reduced_resolution,                                  int reduced_resolution,
# Line 643  Line 705 
705          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
706          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
707    
708          if (reduced_resolution) {          if (reduced_resolution)
709                  mb_width = (dec->width + 31) / 32;          {
710                    mb_width = (dec->width + 31) / 32;
711                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
712          }          }
713    
# Line 671  Line 734 
734                          }                          }
735                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
736    
737                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
738    
739                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
740                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
# Line 698  Line 761 
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
# Line 712  Line 775 
775  }  }
776    
777    
778  static void  void
779  get_motion_vector(DECODER * dec,  get_motion_vector(DECODER * dec,
780                                  Bitstream * bs,                                  Bitstream * bs,
781                                  int x,                                  int x,
# Line 723  Line 786 
786                                  const int bound)                                  const int bound)
787  {  {
788    
789          const int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
790          const int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
791          const int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
792          const int range = (64 * scale_fac);          int range = (64 * scale_fac);
793    
794          const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          VECTOR pmv;
795          VECTOR mv;          VECTOR mv;
796    
797            pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
798    
799          mv.x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv.y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          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);          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);
803    
804          mv.x += pmv.x;          mv.x += pmv.x;
805          mv.y += pmv.y;          mv.y += pmv.y;
# Line 755  Line 820 
820          ret_mv->y = mv.y;          ret_mv->y = mv.y;
821  }  }
822    
823    
824    
825    
826    
827  /* for P_VOP set gmc_warp to NULL */  /* for P_VOP set gmc_warp to NULL */
828  static void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                                  Bitstream * bs,                                  Bitstream * bs,
831                                  int rounding,                                  int rounding,
# Line 766  Line 835 
835                                  int intra_dc_threshold,                                  int intra_dc_threshold,
836                                  const WARPPOINTS *const gmc_warp)                                  const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840          uint32_t bound;          uint32_t bound;
841          int cp_mb, st_mb;          int cp_mb, st_mb;
842          uint32_t mb_width = dec->mb_width;          uint32_t mb_width = dec->mb_width;
843          uint32_t mb_height = dec->mb_height;          uint32_t mb_height = dec->mb_height;
844    
845          if (reduced_resolution) {          if (reduced_resolution)
846            {
847                  mb_width = (dec->width + 31) / 32;                  mb_width = (dec->width + 31) / 32;
848                  mb_height = (dec->height + 31) / 32;                  mb_height = (dec->height + 31) / 32;
849          }          }
# Line 782  Line 853 
853                                          dec->width, dec->height);                                          dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856          if (gmc_warp) {          if (gmc_warp)
857            {
858    
859                  /* 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 */
860                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
861                    {
862                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
863                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
864                                    dec->sprite_warping_points);
865                    }
866    
867                  generate_GMCparameters( dec->sprite_warping_points,                  generate_GMCparameters( dec->sprite_warping_points,
868                                  dec->sprite_warping_accuracy, gmc_warp,                                  (2 << dec->sprite_warping_accuracy), gmc_warp,
869                                  dec->width, dec->height, &dec->new_gmc_data);                                  dec->width, dec->height, &dec->gmc_data);
870    
871                  /* image warping is done block-based in decoder_mbgmc(), now */                  /* image warping is done block-based in decoder_mbgmc(), now */
872    /*
873            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
874                                            mb_width, mb_height,
875                                            dec->edged_width, dec->edged_width/2,
876                                            fcode, dec->quarterpel, 0,
877                                            rounding, dec->mbs, &dec->gmc);
878    */
879          }          }
880    
881          bound = 0;          bound = 0;
# Line 802  Line 889 
889                          while (BitstreamShowBits(bs, 10) == 1)                          while (BitstreamShowBits(bs, 10) == 1)
890                                  BitstreamSkip(bs, 10);                                  BitstreamSkip(bs, 10);
891    
892                          if (check_resync_marker(bs, fcode - 1)) {                          if (check_resync_marker(bs, fcode - 1))
893                            {
894                                  bound = read_video_packet_header(bs, dec, fcode - 1,                                  bound = read_video_packet_header(bs, dec, fcode - 1,
895                                          &quant, &fcode, NULL, &intra_dc_threshold);                                          &quant, &fcode, NULL, &intra_dc_threshold);
896                                  x = bound % mb_width;                                  x = bound % mb_width;
# Line 810  Line 898 
898                          }                          }
899                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
900    
901                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
902    
903                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */                          /* if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) */ /* not_coded */
904                                  uint32_t mcbpc, cbpc, cbpy, cbp;                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
905                                  uint32_t intra, acpred_flag = 0;                          {
906                                    uint32_t mcbpc;
907                                    uint32_t cbpc;
908                                    uint32_t acpred_flag;
909                                    uint32_t cbpy;
910                                    uint32_t cbp;
911                                    uint32_t intra;
912                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
913    
914                                  cp_mb++;                                  cp_mb++;
# Line 822  Line 916 
916                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
917                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
918    
919                                  DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);                                  DPRINTF(DPRINTF_MB, "mode %i", mb->mode);
920                                  DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);                                  DPRINTF(DPRINTF_MB, "cbpc %i", cbpc);
921                                    acpred_flag = 0;
922    
923                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
924    
925                                    if (intra) {
926                                            acpred_flag = BitstreamGetBit(bs);
927                                    }
928    
929                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
930                                    {
931                                          mcsel = BitstreamGetBit(bs);                                          mcsel = BitstreamGetBit(bs);
932                                  else if (intra)                                  }
                                         acpred_flag = BitstreamGetBit(bs);  
933    
934                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
935                                  DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
936    
937                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
938    
939                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
940                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
941                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);                                          DPRINTF(DPRINTF_MB, "dquant %i", dquant);
942                                          quant += dquant;                                          quant += dquant;
943                                          if (quant > 31) {                                          if (quant > 31) {
944                                                  quant = 31;                                                  quant = 31;
945                                          } else if (quant < 1) {                                          } else if (quant < 1) {
946                                                  quant = 1;                                                  quant = 1;
947                                          }                                          }
948                                          DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);                                          DPRINTF(DPRINTF_MB, "quant %i", quant);
949                                  }                                  }
950                                  mb->quant = quant;                                  mb->quant = quant;
951    
952                                  if (dec->interlacing) {                                  if (dec->interlacing) {
953                                          if ((cbp || intra) && !mcsel) {                                          if (cbp || intra) {
954                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
955                                                  DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
956                                          }                                          }
957    
958                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
959                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
960                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
961    
962                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
963                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
964                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
965                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
966                                                          DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
967                                                  }                                                  }
968                                          }                                          }
969                                  }                                  }
970    
971                                  if (mcsel) {                                  if (mcsel) {
972                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);                                          decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
973                                                                    rounding, reduced_resolution);
974                                          continue;                                          continue;
975    
976                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
977    
978                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
979                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
980                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);                                                                                    fcode, bound);
981                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
982                                                                                      fcode, bound);
983                                          } else {                                          } else {
984                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
985                                                                                      fcode, bound);
986                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
987                                          }                                          }
988                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
989    
990                                          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);
991                                          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);
992                                          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);
993                                          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);
994                                  } else {                /* MODE_INTRA, MODE_INTRA_Q */                                  } else                  /* MODE_INTRA, MODE_INTRA_Q */
995                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  {
996                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =     0;                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =
997                                                    0;
998                                            mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
999                                                    0;
1000                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1001                                                                          intra_dc_threshold, bound, reduced_resolution);                                                                          intra_dc_threshold, bound, reduced_resolution);
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
1005                                  decoder_mbinter(dec, mb, x, y, cbp, bs,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1006                                                                  rounding, reduced_resolution, 0);                                                                  rounding, reduced_resolution);
1007    
1008                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */                          }
1009                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1010                            {
1011                                  mb->mode = MODE_NOT_CODED_GMC;                                  mb->mode = MODE_NOT_CODED_GMC;
1012                                  mb->quant = quant;  
1013                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);                                  start_timer();
1014    
1015                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1016                                                                    rounding, reduced_resolution);
1017    
1018                                    stop_transfer_timer();
1019    
1020                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1021                                          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);
1022                                          cp_mb = 0;                                          cp_mb = 0;
1023                                  }                                  }
1024                                  st_mb = x+1;                                  st_mb = x+1;
1025                          } else {        /* not coded P_VOP macroblock */                          }
1026                            else    /* not coded P_VOP macroblock */
1027                            {
1028                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
                                 mb->quant = quant;  
1029    
1030                                  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;
1031                                  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;
1032                                    /* copy macroblock directly from ref to cur */
1033    
1034                                  decoder_mbinter(dec, mb, x, y, 0, bs,                                  start_timer();
1035                                                                  rounding, reduced_resolution, 0);  
1036                                    if (reduced_resolution)
1037                                    {
1038                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1039                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1040                                                                             dec->edged_width);
1041    
1042                                            transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1043                                                                            dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1044                                                                            dec->edged_width/2);
1045    
1046                                            transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1047                                                                             dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1048                                                                             dec->edged_width/2);
1049                                    }
1050                                    else
1051                                    {
1052                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1053                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1054                                                                             dec->edged_width);
1055    
1056                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1057                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1058                                                                            dec->edged_width/2);
1059    
1060                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1061                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1062                                                                             dec->edged_width/2);
1063                                    }
1064    
1065                                    stop_transfer_timer();
1066    
1067                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1068                                          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 925  Line 1071 
1071                                  st_mb = x+1;                                  st_mb = x+1;
1072                          }                          }
1073                  }                  }
   
1074                  if(dec->out_frm && cp_mb > 0)                  if(dec->out_frm && cp_mb > 0)
1075                          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);
1076          }          }
# Line 933  Line 1078 
1078    
1079    
1080  /* decode B-frame motion vector */  /* decode B-frame motion vector */
1081  static void  void
1082  get_b_motion_vector(Bitstream * bs,  get_b_motion_vector(DECODER * dec,
1083                                            Bitstream * bs,
1084                                            int x,
1085                                            int y,
1086                                          VECTOR * mv,                                          VECTOR * mv,
1087                                          int fcode,                                          int fcode,
1088                                          const VECTOR pmv)                                          const VECTOR pmv)
1089  {  {
1090          const int scale_fac = 1 << (fcode - 1);          int scale_fac = 1 << (fcode - 1);
1091          const int high = (32 * scale_fac) - 1;          int high = (32 * scale_fac) - 1;
1092          const int low = ((-32) * scale_fac);          int low = ((-32) * scale_fac);
1093          const int range = (64 * scale_fac);          int range = (64 * scale_fac);
1094    
1095            int mv_x, mv_y;
1096            int pmv_x, pmv_y;
1097    
1098          int mv_x = get_mv(bs, fcode);          pmv_x = pmv.x;
1099          int mv_y = get_mv(bs, fcode);          pmv_y = pmv.y;
1100    
1101          mv_x += pmv.x;          mv_x = get_mv(bs, fcode);
1102          mv_y += pmv.y;          mv_y = get_mv(bs, fcode);
1103    
1104          if (mv_x < low)          mv_x += pmv_x;
1105            mv_y += pmv_y;
1106    
1107            if (mv_x < low) {
1108                  mv_x += range;                  mv_x += range;
1109          else if (mv_x > high)          } else if (mv_x > high) {
1110                  mv_x -= range;                  mv_x -= range;
1111            }
1112    
1113          if (mv_y < low)          if (mv_y < low) {
1114                  mv_y += range;                  mv_y += range;
1115          else if (mv_y > high)          } else if (mv_y > high) {
1116                  mv_y -= range;                  mv_y -= range;
1117            }
1118    
1119          mv->x = mv_x;          mv->x = mv_x;
1120          mv->y = mv_y;          mv->y = mv_y;
1121  }  }
1122    
1123  /* decode an B-frame direct & interpolate macroblock */  
1124  static void  /* decode an B-frame forward & backward inter macroblock */
1125    void
1126    decoder_bf_mbinter(DECODER * dec,
1127                                       const MACROBLOCK * pMB,
1128                                       const uint32_t x_pos,
1129                                       const uint32_t y_pos,
1130                                       const uint32_t cbp,
1131                                       Bitstream * bs,
1132                                       const uint32_t quant,
1133                                       const uint8_t ref)
1134    {
1135    
1136            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1137            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1138    
1139            uint32_t stride = dec->edged_width;
1140            uint32_t stride2 = stride / 2;
1141            uint32_t next_block = stride * 8;
1142            uint32_t i;
1143            uint32_t iQuant = pMB->quant;
1144            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1145            int uv_dx, uv_dy;
1146    
1147            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1148            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1149            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1150    
1151    
1152            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1153                    uv_dx = pMB->mvs[0].x;
1154                    uv_dy = pMB->mvs[0].y;
1155    
1156                    if (dec->quarterpel)
1157                    {
1158                            uv_dx /= 2;
1159                            uv_dy /= 2;
1160                    }
1161    
1162                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1163                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1164            } else {
1165                    int sum;
1166    
1167                    if(dec->quarterpel)
1168                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1169                    else
1170                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1171    
1172                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1173    
1174                    if(dec->quarterpel)
1175                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1176                    else
1177                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1178    
1179                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1180            }
1181    
1182            start_timer();
1183            if(dec->quarterpel) {
1184                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1185                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1186                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1187            }
1188            else {
1189                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1190                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1191                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1192                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1193                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1194                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1195                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1196                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1197            }
1198    
1199            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1200                                                      uv_dx, uv_dy, stride2, 0);
1201            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1202                                                      uv_dx, uv_dy, stride2, 0);
1203            stop_comp_timer();
1204    
1205            for (i = 0; i < 6; i++) {
1206                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1207    
1208                    if (cbp & (1 << (5 - i)))       /* coded */
1209                    {
1210                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1211    
1212                            start_timer();
1213                            get_inter_block(bs, &block[i * 64], direction);
1214                            stop_coding_timer();
1215    
1216                            start_timer();
1217                            if (dec->quant_type == 0) {
1218                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1219                            } else {
1220                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1221                            }
1222                            stop_iquant_timer();
1223    
1224                            start_timer();
1225                            idct(&data[i * 64]);
1226                            stop_idct_timer();
1227                    }
1228            }
1229    
1230            if (dec->interlacing && pMB->field_dct) {
1231                    next_block = stride;
1232                    stride *= 2;
1233            }
1234    
1235            start_timer();
1236            if (cbp & 32)
1237                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1238            if (cbp & 16)
1239                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1240            if (cbp & 8)
1241                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1242            if (cbp & 4)
1243                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1244            if (cbp & 2)
1245                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1246            if (cbp & 1)
1247                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1248            stop_transfer_timer();
1249    }
1250    
1251    /* decode an B-frame direct &  inter macroblock */
1252    void
1253  decoder_bf_interpolate_mbinter(DECODER * dec,  decoder_bf_interpolate_mbinter(DECODER * dec,
1254                                                                  IMAGE forward,                                                                  IMAGE forward,
1255                                                                  IMAGE backward,                                                                  IMAGE backward,
1256                                                                  const MACROBLOCK * pMB,                                                                  const MACROBLOCK * pMB,
1257                                                                  const uint32_t x_pos,                                                                  const uint32_t x_pos,
1258                                                                  const uint32_t y_pos,                                                                  const uint32_t y_pos,
1259                                                                  Bitstream * bs,                                                             Bitstream * bs)
                                                                 const int direct)  
1260  {  {
1261    
1262            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1263            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1264    
1265          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
1266          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
1267            uint32_t next_block = stride * 8;
1268            uint32_t iQuant = pMB->quant;
1269          int uv_dx, uv_dy;          int uv_dx, uv_dy;
1270          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
1271            uint32_t i;
1272          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1273          const uint32_t cbp = pMB->cbp;          const uint32_t cbp = pMB->cbp;
1274    
# Line 986  Line 1276 
1276          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1277          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1278    
1279          if (!direct) {  
1280            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1281                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1282                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1283    
1284                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1285                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1286    
1287                  if (dec->quarterpel) {                  if (dec->quarterpel)
1288                    {
1289                          uv_dx /= 2;                          uv_dx /= 2;
1290                          uv_dy /= 2;                          uv_dy /= 2;
1291    
1292                          b_uv_dx /= 2;                          b_uv_dx /= 2;
1293                          b_uv_dy /= 2;                          b_uv_dy /= 2;
1294                  }                  }
# Line 1005  Line 1298 
1298    
1299                  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];
1300                  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 {  
                 if(dec->quarterpel) {  
                         uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);  
                         uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);  
                         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);  
                         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);  
1301                  } else {                  } else {
1302                          uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  int sum;
1303                          uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;  
1304                          b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;                  if(dec->quarterpel)
1305                          b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1306                  }                  else
1307                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1308    
1309                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1310    
1311                    if(dec->quarterpel)
1312                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1313                    else
1314                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1315    
1316                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1317    
1318    
1319                  uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];                  if(dec->quarterpel)
1320                  uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];                          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);
1321                  b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];                  else
1322                  b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1323    
1324                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1325    
1326                    if(dec->quarterpel)
1327                            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);
1328                    else
1329                            sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1330    
1331                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1332          }          }
1333    
1334    
1335          start_timer();          start_timer();
1336          if(dec->quarterpel) {          if(dec->quarterpel) {
1337                  if(!direct) {                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1338                          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,
1339                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1340                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1341                  } else {                  else {
1342                          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,
1343                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1344                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                                                  pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
# Line 1045  Line 1352 
1352                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1353                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                                                  pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1354                  }                  }
1355          } else {          }
1356            else {
1357                  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,
1358                                                          pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                          pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1359                  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 1053  Line 1361 
1361                  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,
1362                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);                                                          pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1363                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1364                                                          16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);                                                            16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1365                                                              0);
1366          }          }
1367    
1368          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 1063  Line 1372 
1372    
1373    
1374          if(dec->quarterpel) {          if(dec->quarterpel) {
1375                  if(!direct) {                  if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1376                          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,
1377                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1378                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                                                  pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1379                  } else {                  else {
1380                          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,
1381                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,                                                                                  dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1382                                                                                  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 1081  Line 1390 
1390                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                  dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1391                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                                                  pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1392                  }                  }
1393          } else {          }
1394            else {
1395                  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,
1396                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                          pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1397                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1398                                                          16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);                                                            16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1399                                                              0);
1400                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1401                                                          16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);                                                            16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1402                                                              stride, 0);
1403                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1404                                                          16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);                                                            16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1405                                                              stride, 0);
1406          }          }
1407    
1408          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 1129  Line 1442 
1442    
1443          stop_comp_timer();          stop_comp_timer();
1444    
1445          if (cbp)          for (i = 0; i < 6; i++) {
1446                  decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);                  int direction = dec->alternate_vertical_scan ? 2 : 0;
1447    
1448                    if (cbp & (1 << (5 - i)))       /* coded */
1449                    {
1450                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1451    
1452                            start_timer();
1453                            get_inter_block(bs, &block[i * 64], direction);
1454                            stop_coding_timer();
1455    
1456                            start_timer();
1457                            if (dec->quant_type == 0) {
1458                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1459                            } else {
1460                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1461  }  }
1462                            stop_iquant_timer();
1463    
1464                            start_timer();
1465                            idct(&data[i * 64]);
1466                            stop_idct_timer();
1467                    }
1468            }
1469    
1470            if (dec->interlacing && pMB->field_dct) {
1471                    next_block = stride;
1472                    stride *= 2;
1473            }
1474    
1475            start_timer();
1476            if (cbp & 32)
1477                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1478            if (cbp & 16)
1479                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1480            if (cbp & 8)
1481                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1482            if (cbp & 4)
1483                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1484            if (cbp & 2)
1485                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1486            if (cbp & 1)
1487                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1488            stop_transfer_timer();
1489    }
1490    
1491    
1492  /* for decode B-frame dbquant */  /* for decode B-frame dbquant */
1493  static __inline int32_t  int32_t __inline
1494  get_dbquant(Bitstream * bs)  get_dbquant(Bitstream * bs)
1495  {  {
1496          if (!BitstreamGetBit(bs))               /*  '0' */          if (!BitstreamGetBit(bs))               /*  '0' */
# Line 1146  Line 1502 
1502  }  }
1503    
1504  /*  /*
1505   * decode B-frame mb_type   * For decode B-frame mb_type
1506   * bit          ret_value   * bit          ret_value
1507   * 1            0   * 1            0
1508   * 01           1   * 01           1
1509   * 001          2   * 001          2
1510   * 0001         3   * 0001         3
1511   */   */
1512  static int32_t __inline  int32_t __inline
1513  get_mbtype(Bitstream * bs)  get_mbtype(Bitstream * bs)
1514  {  {
1515          int32_t mb_type;          int32_t mb_type;
1516    
1517          for (mb_type = 0; mb_type <= 3; mb_type++)          for (mb_type = 0; mb_type <= 3; mb_type++) {
1518                  if (BitstreamGetBit(bs))                  if (BitstreamGetBit(bs))
1519                          return (mb_type);                          break;
1520            }
1521    
1522          return -1;          if (mb_type <= 3)
1523                    return (mb_type);
1524            else
1525                    return (-1);
1526  }  }
1527    
1528  static void  void
1529  decoder_bframe(DECODER * dec,  decoder_bframe(DECODER * dec,
1530                                  Bitstream * bs,                                  Bitstream * bs,
1531                                  int quant,                                  int quant,
# Line 1175  Line 1535 
1535          uint32_t x, y;          uint32_t x, y;
1536          VECTOR mv;          VECTOR mv;
1537          const VECTOR zeromv = {0,0};          const VECTOR zeromv = {0,0};
1538          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;  #ifdef BFRAMES_DEC_DEBUG
1539          int i;          FILE *fp;
1540            static char first=0;
1541    #define BFRAME_DEBUG    if (!first && fp){ \
1542                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1543            }
1544    #endif
1545    
1546          start_timer();          start_timer();
1547          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
# Line 1185  Line 1550 
1550                                          dec->width, dec->height);                                          dec->width, dec->height);
1551          stop_edges_timer();          stop_edges_timer();
1552    
1553    #ifdef BFRAMES_DEC_DEBUG
1554            if (!first){
1555                    fp=fopen("C:\\XVIDDBG.TXT","w");
1556            }
1557    #endif
1558    
1559          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1560                  /* Initialize Pred Motion Vector */                  /* Initialize Pred Motion Vector */
1561                  dec->p_fmv = dec->p_bmv = zeromv;                  dec->p_fmv = dec->p_bmv = zeromv;
1562                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1563                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1564                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
                         const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;  
                         uint32_t intra_dc_threshold; /* fake variable */  
   
                         if (check_resync_marker(bs, fcode_max  - 1)) {  
                                 int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,  
                                                                                                          &fcode_forward, &fcode_backward, &intra_dc_threshold);  
                                 x = bound % dec->mb_width;  
                                 y = bound / dec->mb_width;  
                                 /* reset predicted macroblocks */  
                                 dec->p_fmv = dec->p_bmv = zeromv;  
                         }  
1565    
1566                          mv =                          mv =
1567                          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] =
1568                          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;
                         mb->quant = quant;  
1569    
1570                          /*                          /*
1571                           * skip if the co-located P_VOP macroblock is not coded                           * skip if the co-located P_VOP macroblock is not coded
# Line 1215  Line 1574 
1574                           */                           */
1575    
1576                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1577                                    /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1578                                  mb->cbp = 0;                                  mb->cbp = 0;
1579                                  mb->mode = MODE_FORWARD;  #ifdef BFRAMES_DEC_DEBUG
1580                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  mb->mb_type = MODE_NOT_CODED;
1581            BFRAME_DEBUG
1582    #endif
1583                                    mb->mb_type = MODE_FORWARD;
1584                                    mb->quant = last_mb->quant;
1585                                    /*
1586                                      mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1587                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1588                                    */
1589    
1590                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1591                                  continue;                                  continue;
1592                          }                          }
1593    
1594                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1595                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
1596    
1597                                  mb->mode = get_mbtype(bs);                                  mb->mb_type = get_mbtype(bs);
1598    
1599                                  if (!modb2)             /* modb=='00' */                                  if (!modb2) {   /* modb=='00' */
1600                                          mb->cbp = BitstreamGetBits(bs, 6);                                          mb->cbp = BitstreamGetBits(bs, 6);
1601                                  else                                  } else {
1602                                          mb->cbp = 0;                                          mb->cbp = 0;
1603                                    }
1604                                  if (mb->mode && mb->cbp) {                                  if (mb->mb_type && mb->cbp) {
1605                                          quant += get_dbquant(bs);                                          quant += get_dbquant(bs);
1606                                          if (quant > 31)  
1607                                            if (quant > 31) {
1608                                                  quant = 31;                                                  quant = 31;
1609                                          else if (quant < 1)                                          } else if (quant < 1) {
1610                                                  quant = 1;                                                  quant = 1;
1611                                  }                                  }
                                 mb->quant = quant;  
   
                                 if (dec->interlacing) {  
                                         if (mb->cbp) {  
                                                 mb->field_dct = BitstreamGetBit(bs);  
                                                 DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);  
1612                                          }                                          }
   
                                         if (mb->mode) {  
                                                 mb->field_pred = BitstreamGetBit(bs);  
                                                 DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);  
   
                                                 if (mb->field_pred) {  
                                                         mb->field_for_top = BitstreamGetBit(bs);  
                                                         DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);  
                                                         mb->field_for_bot = BitstreamGetBit(bs);  
                                                         DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);  
                                                 }  
                                         }  
                                 }  
   
1613                          } else {                          } else {
1614                                  mb->mode = MODE_DIRECT_NONE_MV;                                  mb->mb_type = MODE_DIRECT_NONE_MV;
1615                                  mb->cbp = 0;                                  mb->cbp = 0;
1616                          }                          }
1617    
1618                          switch (mb->mode) {                          mb->quant = quant;
1619                            mb->mode = MODE_INTER4V;
1620                            /* DEBUG1("Switch bm_type=",mb->mb_type); */
1621    
1622    #ifdef BFRAMES_DEC_DEBUG
1623            BFRAME_DEBUG
1624    #endif
1625    
1626                            switch (mb->mb_type) {
1627                          case MODE_DIRECT:                          case MODE_DIRECT:
1628                                  get_b_motion_vector(bs, &mv, 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1629    
1630                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1631                                    {
1632                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1633                                            int i;
1634    
1635                                  for (i = 0; i < 4; i++) {                                  for (i = 0; i < 4; i++) {
1636                                          mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x);                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1637                                                                          / TRD + mv.x);
1638                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)                                          mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1639                                                                          ? ((TRB - TRD) * last_mb->mvs[i].x) / TRD                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)
1640                                                                                      / TRD
1641                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);                                                                          : mb->mvs[i].x - last_mb->mvs[i].x);
1642                                          mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y);                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1643                                                                          / TRD + mv.y);
1644                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)                                          mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1645                                                                          ? ((TRB - TRD) * last_mb->mvs[i].y) / TRD                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)
1646                                                                                      / TRD
1647                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);                                                                          : mb->mvs[i].y - last_mb->mvs[i].y);
1648                                  }                                  }
1649                                            /* DEBUG("B-frame Direct!\n"); */
1650                                    }
1651                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1652                                                                                                  mb, x, y, bs, 1);                                                                                             mb, x, y, bs);
1653                                  break;                                  break;
1654    
1655                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1656                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1657                                                                            dec->p_fmv);
1658                                  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];
1659    
1660                                  get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1661                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];                                                                          fcode_backward, dec->p_bmv);
1662                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1663                                            mb->b_mvs[3] = mb->b_mvs[0];
1664    
1665                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1666                                                                                          mb, x, y, bs, 0);                                                                                             mb, x, y, bs);
1667                                    /* DEBUG("B-frame Bidir!\n"); */
1668                                  break;                                  break;
1669    
1670                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1671                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1672                                                                            dec->p_bmv);
1673                                  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];
1674    
1675                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);                                  mb->mode = MODE_INTER;
1676                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1677                                    /* DEBUG("B-frame Backward!\n"); */
1678                                  break;                                  break;
1679    
1680                          case MODE_FORWARD:                          case MODE_FORWARD:
1681                                  get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv);                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1682                                                                            dec->p_fmv);
1683                                  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];
1684    
1685                                  decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);                                  mb->mode = MODE_INTER;
1686                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1687                                    /* DEBUG("B-frame Forward!\n"); */
1688                                  break;                                  break;
1689    
1690                          default:                          default:
1691                                  DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1692                          }                          }
1693                  } /* End of for */                  } /* End of for */
1694          }          }
1695    
1696    #ifdef BFRAMES_DEC_DEBUG
1697            if (!first){
1698                    first=1;
1699                    if (fp)
1700                            fclose(fp);
1701            }
1702    #endif
1703  }  }
1704    
1705    /* swap two MACROBLOCK array */
1706    void
1707    mb_swap(MACROBLOCK ** mb1,
1708                    MACROBLOCK ** mb2)
1709    {
1710            MACROBLOCK *temp = *mb1;
1711    
1712            *mb1 = *mb2;
1713            *mb2 = temp;
1714    }
1715    
1716    
1717  /* perform post processing if necessary, and output the image */  /* perform post processing if necessary, and output the image */
1718  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,  void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1719                                          xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,                                          const XVID_DEC_FRAME * frame, int pp_disable)
                                         int coding_type, int quant)  
1720  {  {
         if (dec->cartoon_mode)  
                 frame->general &= ~XVID_FILMEFFECT;  
1721    
1722          if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL)     /* post process */          if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1723          {          {
1724                  /* note: image is stored to tmp */                  /* note: image is stored to tmp */
1725                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);                  image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1726                  image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,                  image_deblock_rrv(&dec->tmp, dec->edged_width,
1727                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,                                             mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1728                                             frame->general, dec->frames, (coding_type == B_VOP));                                                  8, frame->general);
1729                  img = &dec->tmp;                  img = &dec->tmp;
1730          }          }
1731    
1732          image_output(img, dec->width, dec->height,          image_output(img, dec->width, dec->height,
1733                                   dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,                                   dec->edged_width, frame->image, frame->stride,
1734                                   frame->output.csp, dec->interlacing);                                   frame->colorspace, dec->interlacing);
   
         if (stats) {  
                 stats->type = coding2type(coding_type);  
                 stats->data.vop.time_base = (int)dec->time_base;  
                 stats->data.vop.time_increment = 0;     /* XXX: todo */  
         }  
1735  }  }
1736    
1737    
1738  int  int
1739  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1740                                  xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1741  {  {
1742    
1743          Bitstream bs;          Bitstream bs;
# Line 1359  Line 1748 
1748          uint32_t fcode_backward;          uint32_t fcode_backward;
1749          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1750          WARPPOINTS gmc_warp;          WARPPOINTS gmc_warp;
1751          int coding_type;          int vop_type;
1752          int success, output, seen_something;          int success = 0;
1753            int output = 0;
1754          if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */          int seen_something = 0;
1755                  return XVID_ERR_VERSION;          idctFuncPtr idct_save = idct;
1756    
1757          start_global_timer();          start_global_timer();
1758    
1759          dec->low_delay_default = (frame->general & XVID_LOWDELAY);          dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1760          if ((frame->general & XVID_DISCONTINUITY))          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1761    
1762            if ((frame->general & XVID_DEC_DISCONTINUITY))
1763                  dec->frames = 0;                  dec->frames = 0;
         dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;  
1764    
1765          if (frame->length < 0) {        /* decoder flush */          if (frame->length < 0)  /* decoder flush */
1766                  int ret;          {
1767                  /* 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
1768                          we have a reference frame, then outout the reference frame */                          we have a reference frame, then outout the reference frame */
1769                  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)
1770                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                  {
1771                          dec->frames = 0;                          decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1772                          ret = 0;                          output = 1;
1773                  } else {                  }
1774                          if (stats) stats->type = XVID_TYPE_NOTHING;  
1775                          ret = XVID_ERR_END;                  frame->length = 0;
1776                    if (stats)
1777                    {
1778                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1779                            stats->data.vop.time_base = (int)dec->time_base;
1780                            stats->data.vop.time_increment = 0;     /* XXX: todo */
1781                  }                  }
1782    
1783                  emms();                  emms();
1784    
1785                  stop_global_timer();                  stop_global_timer();
1786                  return ret;                  return XVID_ERR_OK;
1787          }          }
1788    
1789          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
# Line 1395  Line 1791 
1791          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1792          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1793          {          {
1794                    if (stats)
1795                            stats->notify = XVID_DEC_VOP;
1796                    frame->length = 1;
1797                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1798                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);
                 if (stats) stats->type = XVID_TYPE_NOTHING;  
1799                  emms();                  emms();
1800                  return 1;       /* one byte consumed */                  return XVID_ERR_OK;
1801          }          }
1802    
         success = 0;  
         output = 0;  
         seen_something = 0;  
   
1803  repeat:  repeat:
1804    
1805          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1806                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1807    
1808          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1809                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);                                                          vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1810    
1811          if (coding_type == -1) { /* nothing */          if (vop_type == -1)
1812            {
1813                  if (success) goto done;                  if (success) goto done;
                 if (stats) stats->type = XVID_TYPE_NOTHING;  
1814                  emms();                  emms();
1815                  return BitstreamPos(&bs)/8;                  return XVID_ERR_FAIL;
1816          }          }
1817    
1818          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */          if (vop_type == -2 || vop_type == -3)
1819            {
1820                  if (coding_type == -3)                  if (vop_type == -3)
1821                          decoder_resize(dec);                          decoder_resize(dec);
1822    
1823                  if (stats) {                  if (stats)
1824                          stats->type = XVID_TYPE_VOL;                  {
1825                            stats->notify = XVID_DEC_VOL;
1826                          stats->data.vol.general = 0;                          stats->data.vol.general = 0;
1827                          /*XXX: if (dec->interlacing)                          if (dec->interlacing)
1828                                  stats->data.vol.general |= ++INTERLACING; */                                  stats->data.vol.general |= XVID_INTERLACING;
1829                          stats->data.vol.width = dec->width;                          stats->data.vol.width = dec->width;
1830                          stats->data.vol.height = dec->height;                          stats->data.vol.height = dec->height;
1831                          stats->data.vol.par = dec->aspect_ratio;                          stats->data.vol.aspect_ratio = dec->aspect_ratio;
1832                          stats->data.vol.par_width = dec->par_width;                          stats->data.vol.par_width = dec->par_width;
1833                          stats->data.vol.par_height = dec->par_height;                          stats->data.vol.par_height = dec->par_height;
1834                            frame->length = BitstreamPos(&bs) / 8;
1835                          emms();                          emms();
1836                          return BitstreamPos(&bs)/8;     /* number of bytes consumed */                          return XVID_ERR_OK;
1837                  }                  }
1838                  goto repeat;                  goto repeat;
1839          }          }
1840    
1841          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 */
1842    
1843            if((idct == simple_idct_mmx) && (dec->bs_version < 10)) /* rather ugly but should work */
1844                    idct = idct_mmx;
1845    
1846          /* packed_mode: special-N_VOP treament */          /* packed_mode: special-N_VOP treament */
1847          if (dec->packed_mode && coding_type == N_VOP) {          if (dec->packed_mode && vop_type == N_VOP)
1848                  if (dec->low_delay_default && dec->frames > 0) {          {
1849                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                  if (dec->low_delay_default && dec->frames > 0)
1850                    {
1851                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1852                          output = 1;                          output = 1;
1853                  }                  }
1854                  /* ignore otherwise */                  /* ignore otherwise */
1855          } else if (coding_type != B_VOP) {          }
1856                  switch(coding_type) {          else if (vop_type != B_VOP)
1857            {
1858                    switch(vop_type)
1859                    {
1860                  case I_VOP :                  case I_VOP :
1861                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1862                          break;                          break;
# Line 1465  Line 1869 
1869                                                  fcode_forward, intra_dc_threshold, &gmc_warp);                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1870                          break;                          break;
1871                  case N_VOP :                  case N_VOP :
                         /* XXX: not_coded vops are not used for forward prediction */  
                         /* we should not swap(last_mbs,mbs) */  
1872                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
                         SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */  
1873                          break;                          break;
1874                  }                  }
1875    
1876                  if (reduced_resolution) {                  if (reduced_resolution)
1877                    {
1878                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,                          image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1879                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,                                  (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1880                                  16, 0);                                  16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1881                  }                  }
1882    
1883                  /* 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 */
1884                  if (!(dec->low_delay_default && dec->packed_mode)) {                  if (!(dec->low_delay_default && dec->packed_mode))
1885                          if (dec->low_delay) {                  {
1886                                  decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);                          if (dec->low_delay)
1887                            {
1888                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1889                                  output = 1;                                  output = 1;
1890                          } else if (dec->frames > 0)     { /* is the reference frame valid? */                          }
1891                            else if (dec->frames > 0)       /* is the reference frame valid? */
1892                            {
1893                                  /* output the reference frame */                                  /* output the reference frame */
1894                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                                  decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1895                                  output = 1;                                  output = 1;
1896                          }                          }
1897                  }                  }
1898    
1899                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1900                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1901                  SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);                  mb_swap(&dec->mbs, &dec->last_mbs);
1902                  dec->last_reduced_resolution = reduced_resolution;                  dec->last_reduced_resolution = reduced_resolution;
                 dec->last_coding_type = coding_type;  
1903    
1904                  dec->frames++;                  dec->frames++;
1905                  seen_something = 1;                  seen_something = 1;
1906    
1907          } else {        /* B_VOP */          } else {        /* B_VOP */
1908    
1909                  if (dec->low_delay) {                  if (dec->low_delay)
1910                          DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");                  {
1911                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1912                          dec->low_delay = 1;                          dec->low_delay = 1;
1913                  }                  }
1914    
1915                  if (dec->frames < 2) {                  if (dec->frames < 2)
1916                    {
1917                          /* attemping to decode a bvop without atleast 2 reference frames */                          /* attemping to decode a bvop without atleast 2 reference frames */
1918                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1919                                                  "broken b-frame, mising ref frames");                                                  "broken b-frame, mising ref frames");
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1920                  } else if (dec->time_pp <= dec->time_bp) {                  } else if (dec->time_pp <= dec->time_bp) {
1921                          /* this occurs when dx50_bvop_compatibility==0 sequences are                          /* this occurs when dx50_bvop_compatibility==0 sequences are
1922                          decoded in vfw. */                          decoded in vfw. */
1923                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1924                                                  "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);
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1925                  } else {                  } else {
1926                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
                         decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);  
1927                  }                  }
1928    
1929                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1930                  output = 1;                  output = 1;
1931                  dec->frames++;                  dec->frames++;
1932          }          }
# Line 1529  Line 1934 
1934          BitstreamByteAlign(&bs);          BitstreamByteAlign(&bs);
1935    
1936          /* low_delay_default mode: repeat in packed_mode */          /* low_delay_default mode: repeat in packed_mode */
1937          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {          if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1938            {
1939                  success = 1;                  success = 1;
1940                  goto repeat;                  goto repeat;
1941          }          }
# Line 1538  Line 1944 
1944    
1945          /* low_delay_default mode: if we've gotten here without outputting anything,          /* low_delay_default mode: if we've gotten here without outputting anything,
1946             then output the recently decoded frame, or print an error message  */             then output the recently decoded frame, or print an error message  */
1947          if (dec->low_delay_default && output == 0) {          if (dec->low_delay_default && output == 0)
1948                  if (dec->packed_mode && seen_something) {          {
1949                    if (dec->packed_mode && seen_something)
1950                    {
1951                          /* output the recently decoded frame */                          /* output the recently decoded frame */
1952                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1953                  } else {                          output = 1;
1954                    }
1955                    else
1956                    {
1957                          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);
1958                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1959                                  "warning: nothing to output");                                  "warning: nothing to output");
1960                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,                          image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1961                                  "bframe decoder lag");                                  "bframe decoder lag");
1962    
1963                          decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);                          decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
                         if (stats) stats->type = XVID_TYPE_NOTHING;  
1964                  }                  }
1965          }          }
1966    
1967            idct = idct_save;
1968    
1969            frame->length = BitstreamPos(&bs) / 8;
1970    
1971            if (stats)
1972            {
1973                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1974                    stats->data.vop.time_base = (int)dec->time_base;
1975                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1976            }
1977    
1978          emms();          emms();
1979    
1980          stop_global_timer();          stop_global_timer();
1981    
1982          return BitstreamPos(&bs) / 8;   /* number of bytes consumed */          return XVID_ERR_OK;
1983  }  }

Legend:
Removed from v.1.49.2.30  
changed lines
  Added in v.1.50

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