[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.30, Mon Jul 15 23:50:31 2002 UTC revision 1.37.2.26, Sat Jan 4 06:14:32 2003 UTC
# Line 68  Line 68 
68    
69  #include "xvid.h"  #include "xvid.h"
70  #include "portab.h"  #include "portab.h"
71    #include "global.h"
72    
73  #include "decoder.h"  #include "decoder.h"
74  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 79  Line 80 
80  #include "dct/fdct.h"  #include "dct/fdct.h"
81  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
82  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
83    #include "image/reduced.h"
84    #include "image/font.h"
85    
86  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
87  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
88  #include "utils/timer.h"  #include "utils/timer.h"
89  #include "utils/emms.h"  #include "utils/emms.h"
90    #include "motion/motion.h"
91    
92  #include "image/image.h"  #include "image/image.h"
93  #include "image/colorspace.h"  #include "image/colorspace.h"
94  #include "utils/mem_align.h"  #include "utils/mem_align.h"
95    
96  int  int
97  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
98  {  {
99          DECODER *dec;          /* free existing */
100    
101          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
102          if (dec == NULL) {          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
103                  return XVID_ERR_MEMORY;          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
104          }          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
105          param->handle = dec;          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
106    
107          dec->width = param->width;          if (dec->last_mbs)
108          dec->height = param->height;                  xvid_free(dec->last_mbs);
109            if (dec->mbs)
110                    xvid_free(dec->mbs);
111    
112            /* realloc */
113    
114          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
115          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
116    
117          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
118          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
119    
120          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
121                  xvid_free(dec);                  xvid_free(dec);
# Line 120  Line 127 
127                  xvid_free(dec);                  xvid_free(dec);
128                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
129          }          }
130    
131          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
132          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
133          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 128  Line 136 
136                  xvid_free(dec);                  xvid_free(dec);
137                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
138          }          }
139          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
140                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
141                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
142                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
143                    xvid_free(dec);
144                    return XVID_ERR_MEMORY;
145            }
146    
147            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
148                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
149                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
150                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
151                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
152                  xvid_free(dec);                  xvid_free(dec);
153                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
154          }          }
# Line 143  Line 160 
160                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
162                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
163                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
164                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
165                  xvid_free(dec);                  xvid_free(dec);
166                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
167          }          }
   
168          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
169    
170          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
# Line 160  Line 177 
177                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
178                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
179                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
180                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
181                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
182                  xvid_free(dec);                  xvid_free(dec);
183                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
184          }          }
185    
186          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);
187    
188            return XVID_ERR_OK;
189    }
190    
191    
192    int
193    decoder_create(XVID_DEC_PARAM * param)
194    {
195            DECODER *dec;
196    
197            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
198            if (dec == NULL) {
199                    return XVID_ERR_MEMORY;
200            }
201            memset(dec, 0, sizeof(DECODER));
202    
203            param->handle = dec;
204    
205            dec->width = param->width;
206            dec->height = param->height;
207    
208            image_null(&dec->cur);
209            image_null(&dec->refn[0]);
210            image_null(&dec->refn[1]);
211            image_null(&dec->tmp);
212            image_null(&dec->qtmp);
213    
214            dec->mbs = NULL;
215            dec->last_mbs = NULL;
216    
217          init_timer();          init_timer();
218    
219          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
220          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
221          dec->frames = -1;          dec->frames = 0;
222          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
223            dec->low_delay = 0;
224            dec->packed_mode = 0;
225    
226            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
227    
228            if (dec->fixed_dimensions)
229                    return decoder_resize(dec);
230            else
231          return XVID_ERR_OK;          return XVID_ERR_OK;
232  }  }
233    
# Line 185  Line 239 
239          xvid_free(dec->mbs);          xvid_free(dec->mbs);
240          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
241          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
242          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
243            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
244          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
245          xvid_free(dec);          xvid_free(dec);
246    
# Line 214  Line 269 
269                                  Bitstream * bs,                                  Bitstream * bs,
270                                  const uint32_t quant,                                  const uint32_t quant,
271                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
272                                  const unsigned int bound)                                  const unsigned int bound,
273                                    const int reduced_resolution)
274  {  {
275    
276          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 227  Line 283 
283          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
284          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
285    
286            if (reduced_resolution) {
287                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
288                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
289                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
290            }else{
291          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
292          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
293          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
294            }
295    
296          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
297    
# Line 268  Line 330 
330                  start_timer();                  start_timer();
331                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
332                  {                  {
333                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
334                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
335    
336                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
337                  }                  }
338                  stop_coding_timer();                  stop_coding_timer();
339    
# Line 288  Line 352 
352                  start_timer();                  start_timer();
353                  idct(&data[i * 64]);                  idct(&data[i * 64]);
354                  stop_idct_timer();                  stop_idct_timer();
355    
356          }          }
357    
358          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 296  Line 361 
361          }          }
362    
363          start_timer();          start_timer();
364    
365            if (reduced_resolution)
366            {
367                    next_block*=2;
368                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
369                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
370                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
371                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
372                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
373                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
374            }else{
375          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
376          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
377          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
378          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
379          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
380          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
381            }
382          stop_transfer_timer();          stop_transfer_timer();
383  }  }
384    
385    
386    
387    
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #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 };  
   
   
388  // decode an inter macroblock  // decode an inter macroblock
389    
390  void  void
# Line 326  Line 396 
396                                  const uint32_t cbp,                                  const uint32_t cbp,
397                                  Bitstream * bs,                                  Bitstream * bs,
398                                  const uint32_t quant,                                  const uint32_t quant,
399                                  const uint32_t rounding)                                  const uint32_t rounding,
400                                    const int reduced_resolution)
401  {  {
402    
403          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 334  Line 405 
405    
406          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
407          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
408          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
409          uint32_t i;          uint32_t i;
410          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
411          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
412    
413          int uv_dx, uv_dy;          int uv_dx, uv_dy;
414            VECTOR mv[4];   /* local copy of mvs */
415    
416            if (reduced_resolution) {
417                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
418                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
419                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
420                    for (i = 0; i < 4; i++) {
421                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
422                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
423                    }
424            }else{
425          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
426          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
427          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
428                    for (i = 0; i < 4; i++)
429                            mv[i] = pMB->mvs[i];
430            }
431    
432          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
433                  uv_dx = pMB->mvs[0].x;                  uv_dx = mv[0].x;
434                  uv_dy = pMB->mvs[0].y;                  uv_dy = mv[0].y;
435    
436                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
437                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
438          } else {                          uv_dx /= 2;
439                  int sum;                          uv_dy /= 2;
440                    }
441    
442                  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];
443                  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));  
444    
445                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
446                  uv_dy =                  if (reduced_resolution)
447                          (sum ==                  {
448                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
449                                                                    (ABS(sum) / 16) * 2));                                                                    mv[0].x, mv[0].y, stride,  rounding);
450                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
451                                                                      uv_dx, uv_dy, stride2, rounding);
452                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
453                                                                      uv_dx, uv_dy, stride2, rounding);
454    
455                    }
456                    else
457                    {
458                            if(dec->quarterpel) {
459                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
460                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
461                                                                                            mv[0].x, mv[0].y, stride,  rounding);
462                            }
463                            else {
464                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
465                                                                              mv[0].x, mv[0].y, stride,  rounding);
466                            }
467    
468                            interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
469                                                                      uv_dx, uv_dy, stride2, rounding);
470                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
471                                                                      uv_dx, uv_dy, stride2, rounding);
472          }          }
473                    stop_comp_timer();
474    
475            } else {        /* MODE_INTER4V */
476                    int sum;
477    
478                    if(dec->quarterpel)
479                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
480                    else
481                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
482    
483                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
484    
485                    if(dec->quarterpel)
486                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
487                    else
488                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
489    
490                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
491    
492          start_timer();          start_timer();
493                    if (reduced_resolution)
494                    {
495                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
496                                                                      mv[0].x, mv[0].y, stride,  rounding);
497                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos,
498                                                                      mv[1].x, mv[1].y, stride,  rounding);
499                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos + 16,
500                                                                      mv[2].x, mv[2].y, stride,  rounding);
501                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos + 16, 32*y_pos + 16,
502                                                                      mv[3].x, mv[3].y, stride,  rounding);
503                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
504                                                                      uv_dx, uv_dy, stride2, rounding);
505                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
506                                                                      uv_dx, uv_dy, stride2, rounding);
507    
508                            // set_block(pY_Cur, stride, 32, 32, 127);
509                    }
510                    else
511                    {
512                            if(dec->quarterpel) {
513                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
514                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
515                                                                                      mv[0].x, mv[0].y, stride,  rounding);
516                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
517                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
518                                                                                      mv[1].x, mv[1].y, stride,  rounding);
519                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
520                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
521                                                                                      mv[2].x, mv[2].y, stride,  rounding);
522                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
523                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
524                                                                                      mv[3].x, mv[3].y, stride,  rounding);
525                            }
526                            else {
527          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,
528                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
529          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,
530                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                            mv[1].x, mv[1].y, stride,  rounding);
531                                                    rounding);                                  interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
532          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                            mv[2].x, mv[2].y, stride,  rounding);
533                                                    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,
534                                                    rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
535          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          }
536                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,  
                                                   rounding);  
537          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,
538                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
539          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,
540                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
541                    }
542          stop_comp_timer();          stop_comp_timer();
543            }
544    
545          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
546                    int direction = dec->alternate_vertical_scan ? 2 : 0;
547    
548                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
549                  {                  {
550                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
551    
552                          start_timer();                          start_timer();
553                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
554                          stop_coding_timer();                          stop_coding_timer();
555    
556                          start_timer();                          start_timer();
# Line 413  Line 573 
573          }          }
574    
575          start_timer();          start_timer();
576            if (reduced_resolution)
577            {
578                    if (cbp & 32)
579                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
580                    if (cbp & 16)
581                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
582                    if (cbp & 8)
583                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
584                    if (cbp & 4)
585                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
586                    if (cbp & 2)
587                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
588                    if (cbp & 1)
589                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
590            }
591            else
592            {
593          if (cbp & 32)          if (cbp & 32)
594                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
595          if (cbp & 16)          if (cbp & 16)
# Line 425  Line 602 
602                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
603          if (cbp & 1)          if (cbp & 1)
604                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
605            }
606          stop_transfer_timer();          stop_transfer_timer();
607  }  }
608    
# Line 432  Line 610 
610  void  void
611  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
612                             Bitstream * bs,                             Bitstream * bs,
613                               int reduced_resolution,
614                             int quant,                             int quant,
615                             int intra_dc_threshold)                             int intra_dc_threshold)
616  {  {
617          uint32_t bound;          uint32_t bound;
618          uint32_t x, y;          uint32_t x, y;
619            uint32_t mb_width = dec->mb_width;
620            uint32_t mb_height = dec->mb_height;
621    
622            if (reduced_resolution)
623            {
624                    mb_width = (dec->width + 31) / 32;
625                    mb_height = (dec->height + 31) / 32;
626            }
627    
628          bound = 0;          bound = 0;
629    
630          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
631                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
632                          MACROBLOCK *mb;                          MACROBLOCK *mb;
633                          uint32_t mcbpc;                          uint32_t mcbpc;
634                          uint32_t cbpc;                          uint32_t cbpc;
# Line 454  Line 641 
641    
642                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
643                          {                          {
644                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
645                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
646                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
647                                    y = bound / mb_width;
648                          }                          }
649                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
650    
# Line 487  Line 675 
675    
676                          if (dec->interlacing) {                          if (dec->interlacing) {
677                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
678                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
679                          }                          }
680    
681                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
682                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
683    
684                  }                  }
685                    if(dec->out_frm)
686                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
687          }          }
688    
689  }  }
# Line 504  Line 695 
695                                    int x,                                    int x,
696                                    int y,                                    int y,
697                                    int k,                                    int k,
698                                    VECTOR * mv,                                    VECTOR * ret_mv,
699                                    int fcode,                                    int fcode,
700                                    const int bound)                                    const int bound)
701  {  {
# Line 515  Line 706 
706          int range = (64 * scale_fac);          int range = (64 * scale_fac);
707    
708          VECTOR pmv;          VECTOR pmv;
709          int mv_x, mv_y;          VECTOR mv;
710    
711          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
712    
713          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
714          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
715    
716          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);
717    
718          mv_x += pmv.x;          mv.x += pmv.x;
719          mv_y += pmv.y;          mv.y += pmv.y;
720    
721          if (mv_x < low) {          if (mv.x < low) {
722                  mv_x += range;                  mv.x += range;
723          } else if (mv_x > high) {          } else if (mv.x > high) {
724                  mv_x -= range;                  mv.x -= range;
725          }          }
726    
727          if (mv_y < low) {          if (mv.y < low) {
728                  mv_y += range;                  mv.y += range;
729          } else if (mv_y > high) {          } else if (mv.y > high) {
730                  mv_y -= range;                  mv.y -= range;
731          }          }
732    
733          mv->x = mv_x;          ret_mv->x = mv.x;
734          mv->y = mv_y;          ret_mv->y = mv.y;
735    }
736    
737    
738    
739    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
740    {
741            int length = 1 << (fcode+4);
742    
743            if (quarterpel) value *= 2;
744    
745            if (value < -length)
746                    return -length;
747            else if (value >= length)
748                    return length-1;
749            else return value;
750  }  }
751    
752    
753    /* for P_VOP set gmc_mv to NULL */
754  void  void
755  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
756                             Bitstream * bs,                             Bitstream * bs,
757                             int rounding,                             int rounding,
758                               int reduced_resolution,
759                             int quant,                             int quant,
760                             int fcode,                             int fcode,
761                             int intra_dc_threshold)                             int intra_dc_threshold,
762                               VECTOR * gmc_mv)
763  {  {
764    
765          uint32_t x, y;          uint32_t x, y;
766          uint32_t bound;          uint32_t bound;
767            int cp_mb, st_mb;
768            uint32_t mb_width = dec->mb_width;
769            uint32_t mb_height = dec->mb_height;
770    
771            if (reduced_resolution)
772            {
773                    mb_width = (dec->width + 31) / 32;
774                    mb_height = (dec->height + 31) / 32;
775            }
776    
777          start_timer();          start_timer();
778          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
779                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
780          stop_edges_timer();          stop_edges_timer();
781    
782          bound = 0;          bound = 0;
783    
784          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
785                  for (x = 0; x < dec->mb_width; x++) {                  cp_mb = st_mb = 0;
786                    for (x = 0; x < mb_width; x++) {
787                          MACROBLOCK *mb;                          MACROBLOCK *mb;
788    
789                          // skip stuffing                          // skip stuffing
# Line 574  Line 792 
792    
793                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
794                          {                          {
795                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
796                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
797                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
798                                    y = bound / mb_width;
799                          }                          }
800                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
801    
# Line 591  Line 810 
810                                  uint32_t cbpy;                                  uint32_t cbpy;
811                                  uint32_t cbp;                                  uint32_t cbp;
812                                  uint32_t intra;                                  uint32_t intra;
813                                    int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC
814    
815                                    cp_mb++;
816                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
817                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
818                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
# Line 606  Line 827 
827                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
828                                  }                                  }
829    
830                                    if (gmc_mv && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
831                                    {
832                                            mcsel = BitstreamGetBit(bs);
833                                    }
834    
835                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
836                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);
837    
# Line 625  Line 851 
851                                  mb->quant = quant;                                  mb->quant = quant;
852    
853                                  if (dec->interlacing) {                                  if (dec->interlacing) {
854                                            if (cbp || intra) {
855                                          mb->field_dct = BitstreamGetBit(bs);                                          mb->field_dct = BitstreamGetBit(bs);
856                                          DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
857                                            }
858    
859                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
860                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
861                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
862    
863                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
864                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
865                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
866                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
867                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
868                                                  }                                                  }
869                                          }                                          }
870                                  }                                  }
871    
872                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
873                                          if (dec->interlacing && mb->field_pred) {  
874                                            if (mcsel)
875                                            {
876                                                    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);
877                                                    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);
878    
879                                            } else if (dec->interlacing && mb->field_pred) {
880                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
881                                                                                    fcode, bound);                                                                                    fcode, bound);
882                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 655  Line 889 
889                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =                                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =
890                                                          mb->mvs[0].y;                                                          mb->mvs[0].y;
891                                          }                                          }
892                                  } else if (mb->mode ==                                  } else if (mb->mode == MODE_INTER4V ) {
893                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {  
894                                          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);
895                                          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);
896                                          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 668  Line 902 
902                                          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 =
903                                                  0;                                                  0;
904                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
905                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
906                                          continue;                                          continue;
907                                  }                                  }
908    
909                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
910                                                                  rounding);                                                                  rounding, reduced_resolution);
911                          } else                          // not coded  
912                            }
913                            else if (gmc_mv)        /* not coded S_VOP macroblock */
914                            {
915                                    mb->mode = MODE_NOT_CODED_GMC;
916                                    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);
917                                    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);
918                                    decoder_mbinter(dec, mb, x, y, 0, 0, bs, quant, rounding, reduced_resolution);
919                            }
920                            else    /* not coded P_VOP macroblock */
921                          {                          {
                                 //DEBUG2("P-frame MB at (X,Y)=",x,y);  
922                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
923    
924                                  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;
925                                  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;
   
926                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
927    
928                                  start_timer();                                  start_timer();
929    
930                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  if (reduced_resolution)
931                                                                   (16 * x),                                  {
932                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
933                                                                   (16 * x), dec->edged_width);                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
934                                                                             dec->edged_width);
935                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
936                                                                   (16 * x + 8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
937                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                                                          dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
                                                                  (16 * x + 8), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +  
                                                                  (16 * x),  
                                                                  dec->refn[0].y + (16 * y +  
                                                                                                    8) * dec->edged_width +  
                                                                  (16 * x), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width +  
                                                                  (16 * x + 8),  
                                                                  dec->refn[0].y + (16 * y +  
                                                                                                    8) * dec->edged_width +  
                                                                  (16 * x + 8), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 +  
                                                                  (8 * x),  
                                                                  dec->refn[0].u +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
938                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
939    
940                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
941                                                                   (8 * x),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
942                                                                   dec->refn[0].v +                                                                           dec->edged_width/2);
943                                                                   (8 * y) * dec->edged_width / 2 + (8 * x),                                  }
944                                    else
945                                    {
946                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
947                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
948                                                                             dec->edged_width);
949    
950                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
951                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
952                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
953    
954                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
955                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
956                                                                             dec->edged_width/2);
957                                    }
958    
959                                  stop_transfer_timer();                                  stop_transfer_timer();
960    
961                                    if(dec->out_frm && cp_mb > 0) {
962                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
963                                      cp_mb = 0;
964                                    }
965                                    st_mb = x+1;
966                          }                          }
967                  }                  }
968                    if(dec->out_frm && cp_mb > 0)
969                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
970          }          }
971  }  }
972    
# Line 804  Line 1049 
1049                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1050                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1051    
1052                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1053                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1054                            uv_dx /= 2;
1055                            uv_dy /= 2;
1056                    }
1057    
1058                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1059                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1060          } else {          } else {
1061                  int sum;                  int sum;
1062    
1063                    if(dec->quarterpel)
1064                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1065                    else
1066                  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));  
1067    
1068                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1069    
1070                    if(dec->quarterpel)
1071                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1072                    else
1073                  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;
1074                  uv_dy =  
1075                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1076          }          }
1077    
1078          start_timer();          start_timer();
1079            if(dec->quarterpel) {
1080                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1081                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1082                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1083            }
1084            else {
1085          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,
1086                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1087          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,
1088                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1089          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,
1090                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1091                                                    0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1092          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1093                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
1094                                                    0);  
1095          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,
1096                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
1097          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 840  Line 1099 
1099          stop_comp_timer();          stop_comp_timer();
1100    
1101          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1102                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1103    
1104                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1105                  {                  {
1106                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1107    
1108                          start_timer();                          start_timer();
1109                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1110                          stop_coding_timer();                          stop_coding_timer();
1111    
1112                          start_timer();                          start_timer();
# Line 883  Line 1144 
1144          stop_transfer_timer();          stop_transfer_timer();
1145  }  }
1146    
   
1147  // add by MinChen <chenm001@163.com>  // add by MinChen <chenm001@163.com>
1148  // decode an B-frame direct &  inter macroblock  // decode an B-frame direct &  inter macroblock
1149  void  void
# Line 918  Line 1178 
1178                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1179                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1180    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1181                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1182                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1183    
1184                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1185                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1186                            uv_dx /= 2;
1187                            uv_dy /= 2;
1188    
1189                            b_uv_dx /= 2;
1190                            b_uv_dy /= 2;
1191                    }
1192    
1193                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1194                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1195    
1196                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1197                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1198          } else {          } else {
1199                  int sum;                  int sum;
1200    
1201                    if(dec->quarterpel)
1202                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1203                    else
1204                  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));  
1205    
1206                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1207    
1208                    if(dec->quarterpel)
1209                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1210                    else
1211                  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;
1212                  uv_dy =  
1213                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1214                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1215                                                                    (ABS(sum) / 16) * 2));  
1216                    if(dec->quarterpel)
1217                  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);
1218                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +                  else
1219                          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;
1220                  b_uv_dx =  
1221                          (sum ==                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1222                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1223                                                                    (ABS(sum) / 16) * 2));                  if(dec->quarterpel)
1224                            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);
1225                  sum =                  else
1226                          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;
1227                          pMB->b_mvs[3].y;  
1228                  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));  
1229          }          }
1230    
1231    
1232          start_timer();          start_timer();
1233            if(dec->quarterpel) {
1234                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1235                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1236                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1237                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1238                    else {
1239                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1240                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1241                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1242                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1243                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1244                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1245                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1246                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1247                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1248                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1249                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1250                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1251                    }
1252            }
1253            else {
1254          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,
1255                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1256          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 969  Line 1260 
1260          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1261                                                    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,
1262                                                    0);                                                    0);
1263            }
1264    
1265          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,
1266                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1267          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,
1268                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1269    
1270    
1271          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1272                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1273                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1274                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1275                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1276                    else {
1277                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1278                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1279                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1280          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                          interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1281                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1282                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1283                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1284                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1285                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1286                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1287                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1288                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1289                    }
1290            }
1291            else {
1292                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1293                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1294                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1295                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,                                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1296                                                    0);                                                    0);
1297          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1298                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,                                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1299                                                    stride, 0);                                                    stride, 0);
1300          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1301                                                    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,
1302                                                    stride, 0);                                                    stride, 0);
1303          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          }
1304    
1305            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1306                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1307          interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1308                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1309    
1310          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,
1311                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1312          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1313                                           stride);                                                  stride, 1, 8);
1314          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1315                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1316          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1317                                           16 * y_pos + 8, stride);                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1318          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1319                                           stride2);  
1320          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,
1321                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1322                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1323                                                    stride, 1, 8);
1324    
1325            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1326                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1327                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1328                                                    stride, 1, 8);
1329    
1330            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1331                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1332                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1333                                                    stride2, 1, 8);
1334    
1335            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1336                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1337                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1338                                                    stride2, 1, 8);
1339    
1340          stop_comp_timer();          stop_comp_timer();
1341    
1342          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1343                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1344    
1345                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1346                  {                  {
1347                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1348    
1349                          start_timer();                          start_timer();
1350                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1351                          stop_coding_timer();                          stop_coding_timer();
1352    
1353                          start_timer();                          start_timer();
# Line 1106  Line 1442 
1442    
1443          start_timer();          start_timer();
1444          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1445                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1446          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1447                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1448          stop_edges_timer();          stop_edges_timer();
1449    
1450  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
# Line 1128  Line 1464 
1464                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =                          mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1465                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;                          mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1466    
1467                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1468                            // note: gmc+not_coded isn't skipped
1469    
1470                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1471                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1472                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1176  Line 1514 
1514  #ifdef BFRAMES_DEC_DEBUG  #ifdef BFRAMES_DEC_DEBUG
1515          BFRAME_DEBUG          BFRAME_DEBUG
1516  #endif  #endif
1517    
1518                          switch (mb->mb_type) {                          switch (mb->mb_type) {
1519                          case MODE_DIRECT:                          case MODE_DIRECT:
1520                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1521    
1522                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1523                                  {                               // Because this file is a C file not C++ so I use '{' to define var                                  {
1524                                          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;
1525                                          int i;                                          int i;
1526    
# Line 1241  Line 1580 
1580                                  break;                                  break;
1581    
1582                          default:                          default:
1583                                  //DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
                                 ;  
1584                          }                          }
1585    
1586                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1267  Line 1605 
1605          *mb2 = temp;          *mb2 = temp;
1606  }  }
1607    
1608    
1609    /* perform post processing if necessary, and output the image */
1610    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1611                                            const XVID_DEC_FRAME * frame, int pp_disable)
1612    {
1613    
1614            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1615            {
1616                    /* note: image is stored to tmp */
1617                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1618                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1619                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1620                                                    8, frame->general);
1621                    img = &dec->tmp;
1622            }
1623    
1624            image_output(img, dec->width, dec->height,
1625                                     dec->edged_width, frame->image, frame->stride,
1626                                     frame->colorspace, dec->interlacing);
1627    }
1628    
1629    
1630  int  int
1631  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1632                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1633  {  {
1634    
1635          Bitstream bs;          Bitstream bs;
1636          uint32_t rounding;          uint32_t rounding;
1637            uint32_t reduced_resolution;
1638          uint32_t quant;          uint32_t quant;
1639          uint32_t fcode_forward;          uint32_t fcode_forward;
1640          uint32_t fcode_backward;          uint32_t fcode_backward;
1641          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1642            VECTOR gmc_mv[5];
1643          uint32_t vop_type;          uint32_t vop_type;
1644            int success = 0;
1645            int output = 0;
1646            int seen_something = 0;
1647    
1648          start_global_timer();          start_global_timer();
1649    
1650            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1651            dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1652    
1653            if ((frame->general & XVID_DEC_DISCONTINUITY))
1654                    dec->frames = 0;
1655    
1656            if (frame->length < 0)  /* decoder flush */
1657            {
1658                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1659                        we have a reference frame, then outout the reference frame */
1660                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1661                    {
1662                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1663                            output = 1;
1664                    }
1665    
1666                    frame->length = 0;
1667                    if (stats)
1668                    {
1669                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1670                            stats->data.vop.time_base = (int)dec->time_base;
1671                            stats->data.vop.time_increment = 0;     //XXX: todo
1672                    }
1673    
1674                    emms();
1675    
1676                    stop_global_timer();
1677                    return XVID_ERR_OK;
1678            }
1679    
1680          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1681    
1682          // add by chenm001 <chenm001@163.com>          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1683          // for support B-frame to reference last 2 frame          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1684          dec->frames++;          {
1685          vop_type =                  if (stats)
1686                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                          stats->notify = XVID_DEC_VOP;
1687                                                           &fcode_backward, &intra_dc_threshold);                  frame->length = 1;
1688                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1689                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1690                    emms();
1691                    return XVID_ERR_OK;
1692            }
1693    
1694    repeat:
1695    
1696            vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1697                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, gmc_mv);
1698    
1699            DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%i,  time_pp=%i,  time_bp=%i",
1700                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1701    
1702            if (vop_type == - 1)
1703            {
1704                    if (success) goto done;
1705                    emms();
1706                    return XVID_ERR_FAIL;
1707            }
1708    
1709            if (vop_type == -2 || vop_type == -3)
1710            {
1711                    if (vop_type == -3)
1712                            decoder_resize(dec);
1713    
1714                    if (stats)
1715                    {
1716                            stats->notify = XVID_DEC_VOL;
1717                            stats->data.vol.general = 0;
1718                            if (dec->interlacing)
1719                                    stats->data.vol.general |= XVID_INTERLACING;
1720                            stats->data.vol.width = dec->width;
1721                            stats->data.vol.height = dec->height;
1722                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1723                            stats->data.vol.par_width = dec->par_width;
1724                            stats->data.vol.par_height = dec->par_height;
1725                            frame->length = BitstreamPos(&bs) / 8;
1726                            emms();
1727                            return XVID_ERR_OK;
1728                    }
1729                    goto repeat;
1730            }
1731    
1732          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
1733    
         switch (vop_type) {  
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, quant, fcode_forward,  
                                            intra_dc_threshold);  
 #ifdef BFRAMES_DEC  
                 DEBUG1("P_VOP  Time=", dec->time);  
 #endif  
                 break;  
1734    
1735            /* packed_mode: special-N_VOP treament */
1736            if (dec->packed_mode && vop_type == N_VOP)
1737            {
1738                    if (dec->low_delay_default && dec->frames > 0)
1739                    {
1740                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1741                            output = 1;
1742                    }
1743                    /* ignore otherwise */
1744            }
1745            else if (vop_type != B_VOP)
1746            {
1747                    switch(vop_type)
1748                    {
1749          case I_VOP:          case I_VOP:
1750                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
 #ifdef BFRAMES_DEC  
                 DEBUG1("I_VOP  Time=", dec->time);  
 #endif  
1751                  break;                  break;
1752                    case P_VOP :
1753          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1754  #ifdef BFRAMES_DEC                                                  fcode_forward, intra_dc_threshold, NULL);
                 if (dec->time_pp > dec->time_bp) {  
                         DEBUG1("B_VOP  Time=", dec->time);  
                         decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);  
                 } else {  
                         DEBUG("broken B-frame!");  
                 }  
 #else  
                 image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);  
 #endif  
1755                  break;                  break;
1756                    case S_VOP :
1757          case N_VOP:                             // vop not coded                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1758                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                                                  fcode_forward, intra_dc_threshold, gmc_mv);
1759                            break;
1760                    case N_VOP :
1761                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1762                  break;                  break;
1763                    }
1764    
1765          default:                  if (reduced_resolution)
1766                  return XVID_ERR_FAIL;                  {
1767                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1768                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1769                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1770          }          }
1771    
1772  #ifdef BFRAMES_DEC_DEBUG                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1773          if (frame->length != BitstreamPos(&bs) / 8){                  if (!(dec->low_delay_default && dec->packed_mode))
1774                  DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);                  {
1775                            if (dec->low_delay)
1776                            {
1777                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1778                                    output = 1;
1779                            }
1780                            else if (dec->frames > 0)       /* is the reference frame valid? */
1781                            {
1782                                    /* output the reference frame */
1783                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1784                                    output = 1;
1785                            }
1786          }          }
 #endif  
         frame->length = BitstreamPos(&bs) / 8;  
1787    
1788                    image_swap(&dec->refn[0], &dec->refn[1]);
1789                    image_swap(&dec->cur, &dec->refn[0]);
1790                    mb_swap(&dec->mbs, &dec->last_mbs);
1791                    dec->last_reduced_resolution = reduced_resolution;
1792    
1793  #ifdef BFRAMES_DEC                  dec->frames++;
1794          // test if no B_VOP                  seen_something = 1;
1795          if (dec->low_delay) {  
1796  #endif          }else{  /* B_VOP */
1797          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
1798                                           frame->image, frame->stride, frame->colorspace);                  if (dec->low_delay)
1799                    {
1800                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1801                            dec->low_delay = 1;
1802                    }
1803    
1804  #ifdef BFRAMES_DEC                  if (dec->frames < 2)
1805                    {
1806                            /* attemping to decode a bvop without atleast 2 reference frames */
1807                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1808                                                    "broken b-frame, mising ref frames");
1809                    }else if (dec->time_pp <= dec->time_bp) {
1810                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1811                            decoded in vfw. */
1812                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1813                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1814          } else {          } else {
1815                  if (dec->frames >= 0) {                          decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
                         start_timer();  
                         if ((vop_type == I_VOP || vop_type == P_VOP)) {  
                                 image_output(&dec->refn[0], dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
                         } else if (vop_type == B_VOP) {  
                                 image_output(&dec->cur, dec->width, dec->height,  
                                                          dec->edged_width, frame->image, frame->stride,  
                                                          frame->colorspace);  
1816                          }                          }
1817                          stop_conv_timer();  
1818                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1819                    output = 1;
1820                    dec->frames++;
1821                  }                  }
1822    
1823            BitstreamByteAlign(&bs);
1824    
1825            /* low_delay_default mode: repeat in packed_mode */
1826            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1827            {
1828                    success = 1;
1829                    goto repeat;
1830          }          }
 #endif  
1831    
1832          if (vop_type == I_VOP || vop_type == P_VOP) {  done :
1833                  image_swap(&dec->refn[0], &dec->refn[1]);  
1834                  image_swap(&dec->cur, &dec->refn[0]);          /* low_delay_default mode: if we've gotten here without outputting anything,
1835                  // swap MACROBLOCK             then output the recently decoded frame, or print an error message  */
1836                  if (!dec->low_delay && vop_type == P_VOP)          if (dec->low_delay_default && output == 0)
1837                          mb_swap(&dec->mbs, &dec->last_mbs);          {
1838                    if (dec->packed_mode && seen_something)
1839                    {
1840                            /* output the recently decoded frame */
1841                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1842                            output = 1;
1843                    }
1844                    else
1845                    {
1846                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1847                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1848                                    "warning: nothing to output");
1849                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1850                                    "bframe decoder lag");
1851    
1852                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1853                    }
1854            }
1855    
1856            frame->length = BitstreamPos(&bs) / 8;
1857    
1858            if (stats)
1859            {
1860                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1861                    stats->data.vop.time_base = (int)dec->time_base;
1862                    stats->data.vop.time_increment = 0;     //XXX: todo
1863          }          }
1864    
1865          emms();          emms();

Legend:
Removed from v.1.30  
changed lines
  Added in v.1.37.2.26

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