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

Diff of /xvidcore/src/decoder.c

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

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

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

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