[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.32, Fri Jul 19 11:15:21 2002 UTC revision 1.37.2.16, Sun Dec 8 06:43:33 2002 UTC
# Line 79  Line 79 
79  #include "dct/fdct.h"  #include "dct/fdct.h"
80  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
81  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
82    #include "image/reduced.h"
83    
84  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
85  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
86  #include "utils/timer.h"  #include "utils/timer.h"
87  #include "utils/emms.h"  #include "utils/emms.h"
88    #include "motion/motion.h"
89    
90  #include "image/image.h"  #include "image/image.h"
91  #include "image/colorspace.h"  #include "image/colorspace.h"
92  #include "utils/mem_align.h"  #include "utils/mem_align.h"
93    
94  int  int
95  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
96  {  {
97          DECODER *dec;          /* free existing */
98    
99          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
100          if (dec == NULL) {          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
101                  return XVID_ERR_MEMORY;          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
102          }          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);
103          param->handle = dec;          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
104    
105          dec->width = param->width;          if (dec->last_mbs)
106          dec->height = param->height;                  xvid_free(dec->last_mbs);
107            if (dec->mbs)
108                    xvid_free(dec->mbs);
109    
110            /* realloc */
111    
112          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
113          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
114    
115          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
116          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
117    
118          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
119                  xvid_free(dec);                  xvid_free(dec);
# Line 120  Line 125 
125                  xvid_free(dec);                  xvid_free(dec);
126                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
127          }          }
128    
129          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
130          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
131          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
# Line 136  Line 142 
142                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
143          }          }
144    
145            if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {
146                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
147                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
148                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
149                    image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
150                    xvid_free(dec);
151                    return XVID_ERR_MEMORY;
152            }
153    
154          dec->mbs =          dec->mbs =
155                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
156                                          CACHE_LINE);                                          CACHE_LINE);
# Line 144  Line 159 
159                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
160                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
162                    image_destroy(&dec->refh, dec->edged_width, dec->edged_height);
163                  xvid_free(dec);                  xvid_free(dec);
164                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
165          }          }
   
166          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
167    
168          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
# Line 161  Line 176 
176                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
177                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
178                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
179                    image_destroy(&dec->refh, dec->edged_width, dec->edged_height);
180                  xvid_free(dec);                  xvid_free(dec);
181                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
182          }          }
183    
184          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);
185    
186            return XVID_ERR_OK;
187    }
188    
189    
190    int
191    decoder_create(XVID_DEC_PARAM * param)
192    {
193            DECODER *dec;
194    
195            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
196            if (dec == NULL) {
197                    return XVID_ERR_MEMORY;
198            }
199            memset(dec, 0, sizeof(DECODER));
200    
201            param->handle = dec;
202    
203            dec->width = param->width;
204            dec->height = param->height;
205    
206            image_null(&dec->cur);
207            image_null(&dec->refn[0]);
208            image_null(&dec->refn[1]);
209            image_null(&dec->refn[2]);
210            image_null(&dec->refh);
211    
212            dec->mbs = NULL;
213            dec->last_mbs = NULL;
214    
215          init_timer();          init_timer();
216    
217          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
218          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
219          dec->frames = -1;          dec->frames = -1;
220          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
221            dec->low_delay = 0;
222            dec->packed_mode = 0;
223    
224            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
225    
226            if (dec->fixed_dimensions)
227                    return decoder_resize(dec);
228            else
229          return XVID_ERR_OK;          return XVID_ERR_OK;
230  }  }
231    
# Line 186  Line 238 
238          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
239          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
240          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);
241            image_destroy(&dec->refh, dec->edged_width, dec->edged_height);
242          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
243          xvid_free(dec);          xvid_free(dec);
244    
# Line 214  Line 267 
267                                  Bitstream * bs,                                  Bitstream * bs,
268                                  const uint32_t quant,                                  const uint32_t quant,
269                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
270                                  const unsigned int bound)                                  const unsigned int bound,
271                                    const int reduced_resolution)
272  {  {
273    
274          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 227  Line 281 
281          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
282          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
283    
284            if (reduced_resolution) {
285                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
286                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
287                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
288            }else{
289          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
290          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
291          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
292            }
293    
294          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
295    
# Line 268  Line 328 
328                  start_timer();                  start_timer();
329                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
330                  {                  {
331                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
332                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
333    
334                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
335                  }                  }
336                  stop_coding_timer();                  stop_coding_timer();
337    
# Line 288  Line 350 
350                  start_timer();                  start_timer();
351                  idct(&data[i * 64]);                  idct(&data[i * 64]);
352                  stop_idct_timer();                  stop_idct_timer();
353    
354          }          }
355    
356          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 296  Line 359 
359          }          }
360    
361          start_timer();          start_timer();
362    
363            if (reduced_resolution)
364            {
365                    next_block*=2;
366                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
367                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
368                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
369                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
370                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
371                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
372            }else{
373          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
374          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
375          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
376          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
377          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
378          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
379            }
380          stop_transfer_timer();          stop_transfer_timer();
381  }  }
382    
# Line 311  Line 386 
386    
387  #define SIGN(X) (((X)>0)?1:-1)  #define SIGN(X) (((X)>0)?1:-1)
388  #define ABS(X) (((X)>0)?(X):-(X))  #define ABS(X) (((X)>0)?(X):-(X))
 static const uint32_t roundtab[16] =  
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
389    
390  // decode an inter macroblock  // decode an inter macroblock
391    
392    static void
393    rrv_mv_scaleup(VECTOR * mv)
394    {
395            if (mv->x > 0) {
396                    mv->x = 2*mv->x - 1;
397            } else if (mv->x < 0) {
398                    mv->x = 2*mv->x + 1;
399            }
400    
401            if (mv->y > 0) {
402                    mv->y = 2*mv->y - 1;
403            } else if (mv->y < 0) {
404                    mv->y = 2*mv->y + 1;
405            }
406    }
407    
408    
409    
410  void  void
411  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
412                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
# Line 326  Line 416 
416                                  const uint32_t cbp,                                  const uint32_t cbp,
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
419                                  const uint32_t rounding)                                  const uint32_t rounding,
420                                    const int reduced_resolution)
421  {  {
422    
423          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 334  Line 425 
425    
426          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
427          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
428          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
429          uint32_t i;          uint32_t i;
430          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
431          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
432    
433          int uv_dx, uv_dy;          int uv_dx, uv_dy;
434            VECTOR mv[4];
435    
436            for (i = 0; i < 4; i++)
437            {
438                    mv[i] = pMB->mvs[i];
439                    //DPRINTF(DPRINTF_MB, "mv[%i]   orig=%i,%i   local=%i", i, pMB->mvs[i].x, pMB->mvs[i].y,                                                mv[i].x, mv[i].y);
440            }
441    
442            if (reduced_resolution) {
443                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
444                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
445                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
446                    DPRINTF(DPRINTF_MB,"[%i,%i] %i,%i  %i,%i  %i,%i  %i,%i",
447                            x_pos, y_pos,
448                            mv[0].x, mv[0].y,
449                            mv[1].x, mv[1].y,
450                            mv[2].x, mv[2].y,
451                            mv[3].x, mv[3].y);
452    
453                    rrv_mv_scaleup(&mv[0]);
454                    rrv_mv_scaleup(&mv[1]);
455                    rrv_mv_scaleup(&mv[2]);
456                    rrv_mv_scaleup(&mv[3]);
457    
458                    DPRINTF(DPRINTF_MB,"        %i,%i  %i,%i  %i,%i  %i,%i",
459                            mv[0].x, mv[0].y,
460                            mv[1].x, mv[1].y,
461                            mv[2].x, mv[2].y,
462                            mv[3].x, mv[3].y);
463            }else{
464          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
465          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
466          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
467            }
468    
469          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
470                  uv_dx = pMB->mvs[0].x;                  uv_dx = mv[0].x;
471                  uv_dy = pMB->mvs[0].y;                  uv_dy = mv[0].y;
472    
473                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
474                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
475          } else {                          uv_dx /= 2;
476                  int sum;                          uv_dy /= 2;
477                    }
478    
479                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
480                  uv_dx =                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
481    
482                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
483                  uv_dy =                  if (reduced_resolution)
484                          (sum ==                  {
485                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
486                                                                    (ABS(sum) / 16) * 2));                                                                    mv[0].x, mv[0].y, stride,  rounding);
487                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
488                                                                      uv_dx, uv_dy, stride2, rounding);
489                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
490                                                                      uv_dx, uv_dy, stride2, rounding);
491    
492                    }
493                    else
494                    {
495                            if(dec->quarterpel) {
496                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,
497                                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos,
498                                                                                            mv[0].x, mv[0].y, stride,  rounding);
499                            }
500                            else {
501                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
502                                                                              mv[0].x, mv[0].y, stride,  rounding);
503                            }
504    
505                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
506                                                                      uv_dx, uv_dy, stride2, rounding);
507                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
508                                                                      uv_dx, uv_dy, stride2, rounding);
509          }          }
510                    stop_comp_timer();
511    
512            } else {        /* MODE_INTER4V */
513                    int sum;
514    
515                    if(dec->quarterpel)
516                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
517                    else
518                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
519    
520                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
521    
522                    if(dec->quarterpel)
523                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
524                    else
525                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
526    
527                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
528    
529          start_timer();          start_timer();
530                    if (reduced_resolution)
531                    {
532                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
533                                                                      mv[0].x, mv[0].y, stride,  rounding);
534                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos,
535                                                                      mv[1].x, mv[1].y, stride,  rounding);
536                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16,
537                                                                      mv[2].x, mv[2].y, stride,  rounding);
538                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16,
539                                                                      mv[3].x, mv[3].y, stride,  rounding);
540                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
541                                                                      uv_dx, uv_dy, stride2, rounding);
542                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
543                                                                      uv_dx, uv_dy, stride2, rounding);
544    
545                            // set_block(pY_Cur, stride, 32, 32, 127);
546                    }
547                    else
548                    {
549                            if(dec->quarterpel) {
550                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,
551                                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos,
552                                                                                      mv[0].x, mv[0].y, stride,  rounding);
553                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,
554                                                                                      dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,
555                                                                                      mv[1].x, mv[1].y, stride,  rounding);
556                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,
557                                                                                      dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,
558                                                                                      mv[2].x, mv[2].y, stride,  rounding);
559                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,
560                                                                                      dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,
561                                                                                      mv[3].x, mv[3].y, stride,  rounding);
562                            }
563                            else {
564          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,
565                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
566          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
567                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                            mv[1].x, mv[1].y, stride,  rounding);
568                                                    rounding);                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
569          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                            mv[2].x, mv[2].y, stride,  rounding);
570                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
571                                                    rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
572          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          }
573                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  
                                                   rounding);  
574          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
575                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
576          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
577                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
578                    }
579          stop_comp_timer();          stop_comp_timer();
580            }
581    
582          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
583                    int direction = dec->alternate_vertical_scan ? 2 : 0;
584    
585                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
586                  {                  {
587                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
588    
589                          start_timer();                          start_timer();
590                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
591                          stop_coding_timer();                          stop_coding_timer();
592    
593                          start_timer();                          start_timer();
# Line 413  Line 610 
610          }          }
611    
612          start_timer();          start_timer();
613            if (reduced_resolution)
614            {
615                    if (cbp & 32)
616                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
617                    if (cbp & 16)
618                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
619                    if (cbp & 8)
620                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
621                    if (cbp & 4)
622                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
623                    if (cbp & 2)
624                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
625                    if (cbp & 1)
626                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
627            }
628            else
629            {
630          if (cbp & 32)          if (cbp & 32)
631                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
632          if (cbp & 16)          if (cbp & 16)
# Line 425  Line 639 
639                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
640          if (cbp & 1)          if (cbp & 1)
641                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
642            }
643          stop_transfer_timer();          stop_transfer_timer();
644  }  }
645    
# Line 432  Line 647 
647  void  void
648  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
649                             Bitstream * bs,                             Bitstream * bs,
650                               int reduced_resolution,
651                             int quant,                             int quant,
652                             int intra_dc_threshold)                             int intra_dc_threshold)
653  {  {
654          uint32_t bound;          uint32_t bound;
655          uint32_t x, y;          uint32_t x, y;
656            int mb_width = dec->mb_width;
657            int mb_height = dec->mb_height;
658    
659            if (reduced_resolution)
660            {
661                    mb_width /= 2;
662                    mb_height /= 2;
663            }
664    
665          bound = 0;          bound = 0;
666    
667          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
668                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
669                          MACROBLOCK *mb;                          MACROBLOCK *mb;
670                          uint32_t mcbpc;                          uint32_t mcbpc;
671                          uint32_t cbpc;                          uint32_t cbpc;
# Line 454  Line 678 
678    
679                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
680                          {                          {
681                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
682                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
683                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
684                                    y = bound / mb_width;
685                          }                          }
686                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
687    
# Line 491  Line 716 
716                          }                          }
717    
718                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
719                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
720    
721                  }                  }
722                  if(dec->out_frm)                  if(dec->out_frm)
723                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
   
724          }          }
725    
726  }  }
# Line 507  Line 732 
732                                    int x,                                    int x,
733                                    int y,                                    int y,
734                                    int k,                                    int k,
735                                    VECTOR * mv,                                    VECTOR * ret_mv,
736                                    int fcode,                                    int fcode,
737                                    const int bound)                                    const int bound)
738  {  {
# Line 518  Line 743 
743          int range = (64 * scale_fac);          int range = (64 * scale_fac);
744    
745          VECTOR pmv;          VECTOR pmv;
746          int mv_x, mv_y;          VECTOR mv;
747    
748          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
749    
750          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
751          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
752    
753          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y);          DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv.x, mv.y, pmv.x, pmv.y);
754    
755          mv_x += pmv.x;          mv.x += pmv.x;
756          mv_y += pmv.y;          mv.y += pmv.y;
757    
758          if (mv_x < low) {          if (mv.x < low) {
759                  mv_x += range;                  mv.x += range;
760          } else if (mv_x > high) {          } else if (mv.x > high) {
761                  mv_x -= range;                  mv.x -= range;
762          }          }
763    
764          if (mv_y < low) {          if (mv.y < low) {
765                  mv_y += range;                  mv.y += range;
766          } else if (mv_y > high) {          } else if (mv.y > high) {
767                  mv_y -= range;                  mv.y -= range;
768          }          }
769    
770          mv->x = mv_x;          ret_mv->x = mv.x;
771          mv->y = mv_y;          ret_mv->y = mv.y;
772    }
773    
774    
775    
776    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
777    {
778            int length = 1 << (fcode+4);
779    
780            if (quarterpel) value *= 2;
781    
782            if (value < -length)
783                    return -length;
784            else if (value >= length)
785                    return length-1;
786            else return value;
787  }  }
788    
789    
790    /* for P_VOP set gmc_mv to NULL */
791  void  void
792  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
793                             Bitstream * bs,                             Bitstream * bs,
794                             int rounding,                             int rounding,
795                               int reduced_resolution,
796                             int quant,                             int quant,
797                             int fcode,                             int fcode,
798                             int intra_dc_threshold)                             int intra_dc_threshold,
799                               VECTOR * gmc_mv)
800  {  {
801    
802          uint32_t x, y;          uint32_t x, y;
803          uint32_t bound;          uint32_t bound;
804          int cp_mb, st_mb;          int cp_mb, st_mb;
805            int mb_width = dec->mb_width;
806            int mb_height = dec->mb_height;
807    
808            if (reduced_resolution)
809            {
810                    mb_width /= 2;
811                    mb_height /= 2;
812            }
813    
814          start_timer();          start_timer();
815          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
816                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
817          stop_edges_timer();          stop_edges_timer();
818    
819          bound = 0;          bound = 0;
820    
821          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
822                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
823                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
824                          MACROBLOCK *mb;                          MACROBLOCK *mb;
825    
826                          // skip stuffing                          // skip stuffing
# Line 579  Line 829 
829    
830                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
831                          {                          {
832                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
833                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
834                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
835                                    y = bound / mb_width;
836                          }                          }
837                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
838    
# Line 596  Line 847 
847                                  uint32_t cbpy;                                  uint32_t cbpy;
848                                  uint32_t cbp;                                  uint32_t cbp;
849                                  uint32_t intra;                                  uint32_t intra;
850                                    int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC
851    
852                                  cp_mb++;                                  cp_mb++;
853                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 612  Line 864 
864                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
865                                  }                                  }
866    
867                                    if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
868                                    {
869                                            mcsel = BitstreamGetBit(bs);
870                                    }
871    
872                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
873                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
874    
# Line 631  Line 888 
888                                  mb->quant = quant;                                  mb->quant = quant;
889    
890                                  if (dec->interlacing) {                                  if (dec->interlacing) {
891                                            if (cbp || intra) {
892                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
893                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                          DEBUG1("decp: field_dct: ", mb->field_dct);
894                                            }
895    
896                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
897                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
# Line 648  Line 907 
907                                  }                                  }
908    
909                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
910                                          if (dec->interlacing && mb->field_pred) {  
911                                            if (mcsel)
912                                            {
913                                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);
914                                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);
915    
916                                            } else if (dec->interlacing && mb->field_pred) {
917                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
918                                                                                    fcode, bound);                                                                                    fcode, bound);
919                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 661  Line 926 
926                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
927                                                          mb->mvs[0].y;                                                          mb->mvs[0].y;
928                                          }                                          }
929                                  } else if (mb->mode ==                                  } else if (mb->mode == MODE_INTER4V ) {
930                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {  
931                                          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);
932                                          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);
933                                          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);
# Line 674  Line 939 
939                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
940                                                  0;                                                  0;
941                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
942                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
943                                          continue;                                          continue;
944                                  }                                  }
945    
946                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
947                                                                  rounding);                                                                  rounding, reduced_resolution);
948                          } else                          // not coded  
949                            }
950                            else if (gmc_mv)        /* not coded S_VOP macroblock */
951                          {                          {
                                 //DEBUG2("P-frame MB at (X,Y)=",x,y);  
952                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
953                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = gmc_sanitize(gmc_mv[0].x, dec->quarterpel, fcode);
954                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = gmc_sanitize(gmc_mv[0].y, dec->quarterpel, fcode);
955                                    decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);
956                            }
957                            else    /* not coded P_VOP macroblock */
958                            {
959                                    mb->mode = MODE_NOT_CODED;
960    
961                                  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;
962                                  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;
   
963                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
964    
965                                  start_timer();                                  start_timer();
966    
967                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  if (reduced_resolution)
968                                                                   (16 * x),                                  {
969                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
970                                                                   (16 * x), dec->edged_width);                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
971                                                                             dec->edged_width);
972                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
973                                                                   (16 * x + 8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
974                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
975                                                                   (16 * x + 8), dec->edged_width);                                                                          dec->edged_width/2);
976    
977                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
978                                                                   (16 * x),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
979                                                                   dec->refn[0].y + (16 * y +                                                                           dec->edged_width/2);
980                                                                                                     8) * dec->edged_width +                                  }
981                                                                   (16 * x), dec->edged_width);                                  else
982                                    {
983                                  transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +                                          transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
984                                                                   (16 * x + 8),                                                                           dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
985                                                                   dec->refn[0].y + (16 * y +                                                                           dec->edged_width);
986                                                                                                     8) * dec->edged_width +  
987                                                                   (16 * x + 8), dec->edged_width);                                          transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
988                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
                                 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),  
989                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
990    
991                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +                                          transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
992                                                                   (8 * x),                                                                           dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
993                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
994                                    }
995    
996                                  stop_transfer_timer();                                  stop_transfer_timer();
997    
998                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
999                                    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);
1000                                    cp_mb = 0;                                    cp_mb = 0;
# Line 816  Line 1086 
1086                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1087                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1088    
1089                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1090                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1091                            uv_dx /= 2;
1092                            uv_dy /= 2;
1093                    }
1094    
1095                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1096                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1097          } else {          } else {
1098                  int sum;                  int sum;
1099    
1100                    if(dec->quarterpel)
1101                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1102                    else
1103                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1104    
1105                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1106    
1107                    if(dec->quarterpel)
1108                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1109                    else
1110                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1111                  uv_dy =  
1112                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1113          }          }
1114    
1115          start_timer();          start_timer();
1116            if(dec->quarterpel) {
1117                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->refh.y, dec->refh.y + 64,
1118                                                                        dec->refh.y + 128, 16*x_pos, 16*y_pos,
1119                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1120            }
1121            else {
1122          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos,
1123                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1124          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1125                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1126          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos,                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1127                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1128                                                    0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1129          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1130                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
1131                                                    0);  
1132          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1133                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
1134          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
# Line 852  Line 1136 
1136          stop_comp_timer();          stop_comp_timer();
1137    
1138          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1139                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1140    
1141                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1142                  {                  {
1143                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1144    
1145                          start_timer();                          start_timer();
1146                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1147                          stop_coding_timer();                          stop_coding_timer();
1148    
1149                          start_timer();                          start_timer();
# Line 895  Line 1181 
1181          stop_transfer_timer();          stop_transfer_timer();
1182  }  }
1183    
   
1184  // add by MinChen <chenm001@163.com>  // add by MinChen <chenm001@163.com>
1185  // decode an B-frame direct &  inter macroblock  // decode an B-frame direct &  inter macroblock
1186  void  void
# Line 930  Line 1215 
1215                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1216                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1217    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1218                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1219                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1220    
1221                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1222                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1223                            uv_dx /= 2;
1224                            uv_dy /= 2;
1225    
1226                            b_uv_dx /= 2;
1227                            b_uv_dy /= 2;
1228                    }
1229    
1230                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1231                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1232    
1233                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1234                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1235          } else {          } else {
1236                  int sum;                  int sum;
1237    
1238                    if(dec->quarterpel)
1239                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1240                    else
1241                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
                 uv_dx =  
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1242    
1243                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1244    
1245                    if(dec->quarterpel)
1246                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1247                    else
1248                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1249                  uv_dy =  
1250                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1251                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1252                                                                    (ABS(sum) / 16) * 2));  
1253                    if(dec->quarterpel)
1254                  sum =                          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);
1255                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +                  else
1256                          pMB->b_mvs[3].x;                          sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1257                  b_uv_dx =  
1258                          (sum ==                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1259                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1260                                                                    (ABS(sum) / 16) * 2));                  if(dec->quarterpel)
1261                            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);
1262                  sum =                  else
1263                          pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y +                          sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1264                          pMB->b_mvs[3].y;  
1265                  b_uv_dy =                  b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1266          }          }
1267    
1268    
1269          start_timer();          start_timer();
1270            if(dec->quarterpel) {
1271                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1272                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,
1273                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos,
1274                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1275                    else {
1276                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,
1277                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos,
1278                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1279                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,
1280                                                                                dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,
1281                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1282                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,
1283                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,
1284                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1285                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->refh.y, dec->refh.y + 64,
1286                                                                                dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1287                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1288                    }
1289            }
1290            else {
1291          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,
1292                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1293          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 981  Line 1297 
1297          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1298                                                    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,
1299                                                    0);                                                    0);
1300            }
1301    
1302          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,
1303                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1304          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,          interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1305                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1306    
1307    
1308            if(dec->quarterpel) {
1309                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1310                            interpolate16x16_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,
1311                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos,
1312                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1313                    else {
1314                            interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,
1315                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos,
1316                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1317                            interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,
1318                                                                                dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,
1319                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1320                            interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,
1321                                                                                dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,
1322                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1323                            interpolate8x8_quarterpel(dec->refn[2].y, backward.y, dec->refh.y, dec->refh.y + 64,
1324                                                                                dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1325                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1326                    }
1327            }
1328            else {
1329          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,
1330                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1331          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
# Line 998  Line 1337 
1337          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,
1338                                                    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,
1339                                                    stride, 0);                                                    stride, 0);
1340            }
1341    
1342          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,
1343                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1344          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,
1345                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1346    
1347          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1348                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1349          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,
1350                                           stride);                                                  stride, 1, 8);
1351          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1352                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1353          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1354                                           16 * y_pos + 8, stride);                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,
1355          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1356                                           stride2);  
1357          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1358                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1359                                                    dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1360                                                    stride, 1, 8);
1361    
1362            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1363                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1364                                                    dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1365                                                    stride, 1, 8);
1366    
1367            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1368                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1369                                                    dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,
1370                                                    stride2, 1, 8);
1371    
1372            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1373                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1374                                                    dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,
1375                                                    stride2, 1, 8);
1376    
1377          stop_comp_timer();          stop_comp_timer();
1378    
1379          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1380                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1381    
1382                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1383                  {                  {
1384                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1385    
1386                          start_timer();                          start_timer();
1387                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1388                          stop_coding_timer();                          stop_coding_timer();
1389    
1390                          start_timer();                          start_timer();
# Line 1118  Line 1479 
1479    
1480          start_timer();          start_timer();
1481          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1482                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1483          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1484                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1485          stop_edges_timer();          stop_edges_timer();
1486    
1487  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
# Line 1188  Line 1549 
1549  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1550          BFRAME_DEBUG          BFRAME_DEBUG
1551  #endif  #endif
1552    
1553                          switch (mb->mb_type) {                          switch (mb->mb_type) {
1554                          case MODE_DIRECT:                          case MODE_DIRECT:
1555                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1556    
1557                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1558                                  {                               // Because this file is a C file not C++ so I use '{' to define var                                  {
1559                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1560                                          int i;                                          int i;
1561    
# Line 1253  Line 1615 
1615                                  break;                                  break;
1616    
1617                          default:                          default:
1618                                  //DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);
                                 ;  
1619                          }                          }
1620    
1621                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1281  Line 1642 
1642    
1643  int  int
1644  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1645                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1646  {  {
1647    
1648          Bitstream bs;          Bitstream bs;
1649          uint32_t rounding;          uint32_t rounding;
1650            uint32_t reduced_resolution;
1651          uint32_t quant;          uint32_t quant;
1652          uint32_t fcode_forward;          uint32_t fcode_forward;
1653          uint32_t fcode_backward;          uint32_t fcode_backward;
1654          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1655            VECTOR gmc_mv[5];
1656          uint32_t vop_type;          uint32_t vop_type;
1657            int success = 0;
1658    
1659          start_global_timer();          start_global_timer();
1660    
# Line 1298  Line 1662 
1662    
1663          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1664    
1665            // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1666            if(frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1667            {
1668                    if (stats)
1669                            stats->notify = XVID_DEC_VOP;
1670                    frame->length = 1;
1671                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1672                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1673                    emms();
1674                    return XVID_ERR_OK;
1675            }
1676    
1677    start:
1678          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
1679          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
1680          dec->frames++;          dec->frames++;
1681    
1682    xxx:
1683          vop_type =          vop_type =
1684                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                  BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1685                                                           &fcode_backward, &intra_dc_threshold);                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);
1686    
1687            //DPRINTF(DPRINTF_HEADER, "vop_type=%i", vop_type);
1688    
1689            if (vop_type == -1 && success)
1690                    goto done;
1691    
1692            if (vop_type == -2 || vop_type == -3)
1693            {
1694                    if (vop_type == -3)
1695                            decoder_resize(dec);
1696    
1697                    if (stats)
1698                    {
1699                            stats->notify = XVID_DEC_VOL;
1700                            stats->data.vol.general = 0;
1701                            if (dec->interlacing)
1702                                    stats->data.vol.general |= XVID_INTERLACING;
1703                            stats->data.vol.width = dec->width;
1704                            stats->data.vol.height = dec->height;
1705                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1706                            stats->data.vol.par_width = dec->par_width;
1707                            stats->data.vol.par_height = dec->par_height;
1708                            frame->length = BitstreamPos(&bs) / 8;
1709                            return XVID_ERR_OK;
1710                    }
1711                    goto xxx;
1712            }
1713    
1714          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
1715    
1716          switch (vop_type) {          switch (vop_type) {
1717          case P_VOP:          case P_VOP:
1718                  decoder_pframe(dec, &bs, rounding, quant, fcode_forward,                  decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1719                                             intra_dc_threshold);                                                  fcode_forward, intra_dc_threshold, NULL);
1720  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1721                  DEBUG1("P_VOP  Time=", dec->time);                  DEBUG1("P_VOP  Time=", dec->time);
1722  #endif  #endif
1723                  break;                  break;
1724    
1725          case I_VOP:          case I_VOP:
1726                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                  decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1727  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1728                  DEBUG1("I_VOP  Time=", dec->time);                  DEBUG1("I_VOP  Time=", dec->time);
1729  #endif  #endif
# Line 1336  Line 1742 
1742  #endif  #endif
1743                  break;                  break;
1744    
1745            case S_VOP :
1746                    decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1747                                                    fcode_forward, intra_dc_threshold, gmc_mv);
1748                    break;
1749    
1750          case N_VOP:                             // vop not coded          case N_VOP:                             // vop not coded
1751                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                  // when low_delay==0, N_VOP's should interpolate between the past and future frames
1752                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1753    #ifdef BFRAMES_DEC
1754                    DEBUG1("N_VOP  Time=", dec->time);
1755    #endif
1756                  break;                  break;
1757    
1758          default:          default:
1759                    if (stats)
1760                            stats->notify = 0;
1761    
1762                    emms();
1763                  return XVID_ERR_FAIL;                  return XVID_ERR_FAIL;
1764          }          }
1765    
1766  #ifdef BFRAMES_DEC_DEBUG  
1767          if (frame->length != BitstreamPos(&bs) / 8){          /* reduced resolution deblocking filter */
1768                  DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);  
1769            if (reduced_resolution)
1770            {
1771                    const int rmb_height = dec->mb_height / 2;
1772                    const int rmb_width = dec->mb_width / 2;
1773                    const int edged_width2 = dec->edged_width /2;
1774                    int i,j;
1775    
1776                    /* horizontal deblocking */
1777    
1778                    for (j = 1; j < rmb_height*2; j++)      // luma: j,i in block units
1779                    for (i = 0; i < rmb_width*2; i++)
1780                    {
1781                            if (dec->mbs[(j-1)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED ||
1782                                    dec->mbs[(j+0)/2*dec->mb_width + (i/2)].mode != MODE_NOT_CODED)
1783                            {
1784                                    xvid_HFilter_31_C(dec->cur.y + (j*16 - 1)*dec->edged_width + i*16,
1785                                                                  dec->cur.y + (j*16 + 0)*dec->edged_width + i*16, 2);
1786                            }
1787          }          }
 #endif  
         frame->length = BitstreamPos(&bs) / 8;  
1788    
1789                    for (j = 1; j < rmb_height; j++)        // chroma
1790                    for (i = 0; i < rmb_width; i++)
1791                    {
1792                            if (dec->mbs[(j-1)*dec->mb_width + i].mode != MODE_NOT_CODED ||
1793                                    dec->mbs[(j+0)*dec->mb_width + i].mode != MODE_NOT_CODED)
1794                            {
1795                                    hfilter_31(dec->cur.u + (j*16 - 1)*edged_width2 + i*16,
1796                                                                      dec->cur.u + (j*16 + 0)*edged_width2 + i*16, 2);
1797                                    hfilter_31(dec->cur.v + (j*16 - 1)*edged_width2 + i*16,
1798                                                                      dec->cur.v + (j*16 + 0)*edged_width2 + i*16, 2);
1799                            }
1800                    }
1801    
1802                    /* vertical deblocking */
1803    
1804                    for (j = 0; j < rmb_height*2; j++)              // luma: i,j in block units
1805                    for (i = 1; i < rmb_width*2; i++)
1806                    {
1807                            if (dec->mbs[(j/2)*dec->mb_width + (i-1)/2].mode != MODE_NOT_CODED ||
1808                                    dec->mbs[(j/2)*dec->mb_width + (i+0)/2].mode != MODE_NOT_CODED)
1809                            {
1810                                    vfilter_31(dec->cur.y + (j*16)*dec->edged_width + i*16 - 1,
1811                                                                  dec->cur.y + (j*16)*dec->edged_width + i*16 + 0,
1812                                                                      dec->edged_width, 2);
1813                            }
1814                    }
1815    
1816                    for (j = 0; j < rmb_height; j++)        // chroma
1817                    for (i = 1; i < rmb_width; i++)
1818                    {
1819                            if (dec->mbs[j*dec->mb_width + i - 1].mode != MODE_NOT_CODED ||
1820                                    dec->mbs[j*dec->mb_width + i + 0].mode != MODE_NOT_CODED)
1821                            {
1822                                    vfilter_31(dec->cur.u + (j*16)*edged_width2 + i*16 - 1,
1823                                                                      dec->cur.u + (j*16)*edged_width2 + i*16 + 0,
1824                                                                      edged_width2, 2);
1825                                    vfilter_31(dec->cur.v + (j*16)*edged_width2 + i*16 - 1,
1826                                                                      dec->cur.v + (j*16)*edged_width2 + i*16 + 0,
1827                                                                      edged_width2, 2);
1828                            }
1829                    }
1830            }
1831    
1832            BitstreamByteAlign(&bs);
1833    
1834  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1835          // test if no B_VOP          // test if no B_VOP
1836          if (dec->low_delay) {          if (dec->low_delay || dec->frames == 0 || ((dec->packed_mode) && !(frame->length > BitstreamPos(&bs) / 8))) {
1837  #endif  #endif
1838          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
1839                                           frame->image, frame->stride, frame->colorspace);                                           frame->image, frame->stride, frame->colorspace, dec->interlacing);
1840    
1841  #ifdef BFRAMES_DEC  #ifdef BFRAMES_DEC
1842          } else {          } else {
1843                  if (dec->frames >= 0) {                  if (dec->frames >= 1 && !(dec->packed_mode)) {
1844                          start_timer();                          start_timer();
1845                          if ((vop_type == I_VOP || vop_type == P_VOP)) {                          if ((vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP)) {
1846                                  image_output(&dec->refn[0], dec->width, dec->height,                                  image_output(&dec->refn[0], dec->width, dec->height,
1847                                                           dec->edged_width, frame->image, frame->stride,                                                           dec->edged_width, frame->image, frame->stride,
1848                                                           frame->colorspace);                                                           frame->colorspace, dec->interlacing);
1849                          } else if (vop_type == B_VOP) {                          } else if (vop_type == B_VOP) {
1850                                  image_output(&dec->cur, dec->width, dec->height,                                  image_output(&dec->cur, dec->width, dec->height,
1851                                                           dec->edged_width, frame->image, frame->stride,                                                           dec->edged_width, frame->image, frame->stride,
1852                                                           frame->colorspace);                                                           frame->colorspace, dec->interlacing);
1853                          }                          }
1854                          stop_conv_timer();                          stop_conv_timer();
1855                  }                  }
1856          }          }
1857  #endif  #endif
1858    
1859          if (vop_type == I_VOP || vop_type == P_VOP) {          if (vop_type == I_VOP || vop_type == P_VOP || vop_type == S_VOP) {
1860                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1861                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
1862    
# Line 1394  Line 1872 
1872                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1873          }          }
1874    
1875    
1876            if (success == 0 && dec->packed_mode)
1877            {
1878                    success = 1;
1879            //      if (frame->length > BitstreamPos(&bs) / 8)      // multiple vops packed together
1880                    goto start;
1881            }
1882    
1883    done :
1884    
1885            frame->length = BitstreamPos(&bs) / 8;
1886    
1887            if (stats)
1888            {
1889                    stats->notify = XVID_DEC_VOP;
1890                    stats->data.vop.time_base = (int)dec->time_base;
1891                    stats->data.vop.time_increment = 0;     //XXX: todo
1892            }
1893    
1894          emms();          emms();
1895    
1896          stop_global_timer();          stop_global_timer();

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.37.2.16

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