[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.37.2.6, Wed Oct 30 18:01:48 2002 UTC revision 1.37.2.27, Sat Jan 11 14:59:23 2003 UTC
# Line 59  Line 59 
59   *   *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  #include <string.h>
65    
# Line 68  Line 69 
69    
70  #include "xvid.h"  #include "xvid.h"
71  #include "portab.h"  #include "portab.h"
72    #include "global.h"
73    
74  #include "decoder.h"  #include "decoder.h"
75  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 79  Line 81 
81  #include "dct/fdct.h"  #include "dct/fdct.h"
82  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
83  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
84    #include "image/reduced.h"
85    #include "image/font.h"
86    
87  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
88  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
# Line 91  Line 95 
95  #include "utils/mem_align.h"  #include "utils/mem_align.h"
96    
97  int  int
98  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
99  {  {
100          DECODER *dec;          /* free existing */
101    
102          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
103          if (dec == NULL) {          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
104                  return XVID_ERR_MEMORY;          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
105          }          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
106          param->handle = dec;          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
107    
108          dec->width = param->width;          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
109          dec->height = param->height;  
110            if (dec->last_mbs)
111                    xvid_free(dec->last_mbs);
112            if (dec->mbs)
113                    xvid_free(dec->mbs);
114    
115            /* realloc */
116    
117          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
118          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
119    
120          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
121          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
122    
123          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
124                  xvid_free(dec);                  xvid_free(dec);
# Line 121  Line 130 
130                  xvid_free(dec);                  xvid_free(dec);
131                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
132          }          }
133    
134          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
135          // for support B-frame to reference last 2 frame          // for support B-frame to reference last 2 frame
136          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 129  Line 139 
139                  xvid_free(dec);                  xvid_free(dec);
140                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
141          }          }
142          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
143                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
146                    xvid_free(dec);
147                    return XVID_ERR_MEMORY;
148            }
149    
150            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
151                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
154                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
155                  xvid_free(dec);                  xvid_free(dec);
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          }          }
158    
159          if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
160                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
161                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
162                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
163                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
164                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
165                  xvid_free(dec);                  xvid_free(dec);
166                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
167          }          }
# Line 153  Line 173 
173                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
174                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
175                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
176                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
177                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
178                  xvid_free(dec);                  xvid_free(dec);
179                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
180          }          }
   
181          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
182    
183          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
# Line 171  Line 190 
190                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
191                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
192                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
193                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
194                  image_destroy(&dec->refh, dec->edged_width, dec->edged_height);                  image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
195                  xvid_free(dec);                  xvid_free(dec);
196                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
197          }          }
198    
199          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);
200    
201            return XVID_ERR_OK;
202    }
203    
204    
205    int
206    decoder_create(XVID_DEC_PARAM * param)
207    {
208            DECODER *dec;
209    
210            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
211            if (dec == NULL) {
212                    return XVID_ERR_MEMORY;
213            }
214            memset(dec, 0, sizeof(DECODER));
215    
216            param->handle = dec;
217    
218            dec->width = param->width;
219            dec->height = param->height;
220    
221            image_null(&dec->cur);
222            image_null(&dec->refn[0]);
223            image_null(&dec->refn[1]);
224            image_null(&dec->tmp);
225            image_null(&dec->qtmp);
226    
227    /* image based GMC */
228            image_null(&dec->gmc);
229    
230    
231            dec->mbs = NULL;
232            dec->last_mbs = NULL;
233    
234          init_timer();          init_timer();
235    
236          // add by chenm001 <chenm001@163.com>          // add by chenm001 <chenm001@163.com>
237          // for support B-frame to save reference frame's time          // for support B-frame to save reference frame's time
238          dec->frames = -1;          dec->frames = 0;
239          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
240            dec->low_delay = 0;
241            dec->packed_mode = 0;
242    
243            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
244    
245            if (dec->fixed_dimensions)
246                    return decoder_resize(dec);
247            else
248          return XVID_ERR_OK;          return XVID_ERR_OK;
249  }  }
250    
# Line 195  Line 254 
254  {  {
255          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
256          xvid_free(dec->mbs);          xvid_free(dec->mbs);
257    
258            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);          /* image based GMC */
259    
260          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
261          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
262          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
263          image_destroy(&dec->refh, dec->edged_width, dec->edged_height);          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
264          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
265          xvid_free(dec);          xvid_free(dec);
266    
# Line 227  Line 289 
289                                  Bitstream * bs,                                  Bitstream * bs,
290                                  const uint32_t quant,                                  const uint32_t quant,
291                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
292                                  const unsigned int bound)                                  const unsigned int bound,
293                                    const int reduced_resolution)
294  {  {
295    
296          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 240  Line 303 
303          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
304          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
305    
306            if (reduced_resolution) {
307                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
308                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
309                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
310            }else{
311          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
312          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
313          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
314            }
315    
316          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear          memset(block, 0, 6 * 64 * sizeof(int16_t));     // clear
317    
# Line 303  Line 372 
372                  start_timer();                  start_timer();
373                  idct(&data[i * 64]);                  idct(&data[i * 64]);
374                  stop_idct_timer();                  stop_idct_timer();
375    
376          }          }
377    
378          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 311  Line 381 
381          }          }
382    
383          start_timer();          start_timer();
384    
385            if (reduced_resolution)
386            {
387                    next_block*=2;
388                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
389                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
390                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
391                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
392                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
393                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
394            }else{
395          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
396          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
397          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
398          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
399          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
400          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
401            }
402          stop_transfer_timer();          stop_transfer_timer();
403  }  }
404    
405    
406    
407    
   
 #define SIGN(X) (((X)>0)?1:-1)  
 #define ABS(X) (((X)>0)?(X):-(X))  
   
408  // decode an inter macroblock  // decode an inter macroblock
409    
410  void  void
# Line 338  Line 416 
416                                  const uint32_t cbp,                                  const uint32_t cbp,
417                                  Bitstream * bs,                                  Bitstream * bs,
418                                  const uint32_t quant,                                  const uint32_t quant,
419                                  const uint32_t rounding)                                  const uint32_t rounding,
420                                    const int reduced_resolution,
421                                    const int mcsel)
422  {  {
423    
424          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 346  Line 426 
426    
427          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
428          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
429          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
430          uint32_t i;          uint32_t i;
431          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
432          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
433            uint8_t *pY_Ref, *pU_Ref, *pV_Ref;              /* ref for GMC is _not_ pRef itself */
434    
435          int uv_dx, uv_dy;          int uv_dx, uv_dy;
436            VECTOR mv[4];   /* local copy of mvs */
437    
438            if (reduced_resolution) {
439                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
440                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
441                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
442                    for (i = 0; i < 4; i++) {
443                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
444                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
445                    }
446            } else {
447          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
448          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
449          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
450                    for (i = 0; i < 4; i++)
451                            mv[i] = pMB->mvs[i];
452            }
453    
454            if (mcsel) {
455                    mv[0].x = mv[0].y = mv[1].x = mv[1].y = mv[2].x = mv[2].y = mv[3].x = mv[3].y = 0;
456                            /* position in ref is same as the block, set vector to (0,0) */
457                    pY_Ref = dec->gmc.y;
458                    pU_Ref = dec->gmc.u;
459                    pV_Ref = dec->gmc.v;
460                            /* but reference itself isn't. It's warped... */
461                            /* Btw., this is too slow! For GMC it should simply be transfer_16to8add() */
462            } else {
463                    pY_Ref = dec->refn[0].y;
464                    pU_Ref = dec->refn[0].u;
465                    pV_Ref = dec->refn[0].v;
466            }
467    
468          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {          if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) {
                 uv_dx = pMB->mvs[0].x;  
                 uv_dy = pMB->mvs[0].y;  
469    
470                  if (dec->quarterpel)                  uv_dx = mv[0].x / (1 + dec->quarterpel);
471                  {                  uv_dy = mv[0].y / (1 + dec->quarterpel);
472                          uv_dx = (uv_dx >> 2) + roundtab_78[uv_dx & 0x7];  
                         uv_dy = (uv_dy >> 2) + roundtab_78[uv_dy & 0x7];  
                 }  
                 else {  
473                          uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];                          uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
474                          uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];                          uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
                 }  
475    
476                  start_timer();                  start_timer();
477                    if (reduced_resolution)
478                    {
479                            interpolate32x32_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
480                                                                      mv[0].x, mv[0].y, stride,  rounding);
481                            interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,
482                                                                      uv_dx, uv_dy, stride2, rounding);
483                            interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,
484                                                                      uv_dx, uv_dy, stride2, rounding);
485    
486                    }
487                    else
488                    {
489                  if(dec->quarterpel) {                  if(dec->quarterpel) {
490                          interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate16x16_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                              dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                          dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                                          mv[0].x, mv[0].y, stride, rounding);
493                  }                  }
494                  else {                  else {
495                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate16x16_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
496                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride, rounding);
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,  
                                                               pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,  
                                                                   pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);  
                         interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,  
                                                                   pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);  
497                  }                  }
498    
499                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,
500                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
501                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,
502                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
503                    }
504                  stop_comp_timer();                  stop_comp_timer();
505    
506          } else {          } else {        /* MODE_INTER4V */
507                  int sum;                  int sum;
508    
509                  if(dec->quarterpel)                  if(dec->quarterpel)
510                          sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);                          sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
511                  else                  else
512                          sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                          sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
513    
514                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
515    
516                  if(dec->quarterpel)                  if(dec->quarterpel)
517                          sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);                          sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
518                  else                  else
519                          sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                          sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
520    
521                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
522    
523                  start_timer();                  start_timer();
524                    if (reduced_resolution)
525                    {
526                            interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
527                                                                      mv[0].x, mv[0].y, stride,  rounding);
528                            interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos,
529                                                                      mv[1].x, mv[1].y, stride,  rounding);
530                            interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos + 16,
531                                                                      mv[2].x, mv[2].y, stride,  rounding);
532                            interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos + 16,
533                                                                      mv[3].x, mv[3].y, stride,  rounding);
534                            interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,
535                                                                      uv_dx, uv_dy, stride2, rounding);
536                            interpolate16x16_switch(dec->cur.v, pV_Ref , 16 * x_pos, 16 * y_pos,
537                                                                      uv_dx, uv_dy, stride2, rounding);
538    
539                            // set_block(pY_Cur, stride, 32, 32, 127);
540                    }
541                    else
542                    {
543                  if(dec->quarterpel) {                  if(dec->quarterpel) {
544                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
545                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
546                                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                                    mv[0].x, mv[0].y, stride,  rounding);
547                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
548                                                                            dec->refh.y + 128, 16*x_pos + 8, 16*y_pos,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
549                                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                                    mv[1].x, mv[1].y, stride,  rounding);
550                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
551                                                                            dec->refh.y + 128, 16*x_pos, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
552                                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                                    mv[2].x, mv[2].y, stride,  rounding);
553                          interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64,                                  interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
554                                                                            dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8,                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
555                                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                                    mv[3].x, mv[3].y, stride,  rounding);
556                  }                  }
557                  else {                  else {
558                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
559                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
560                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos,
561                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
562                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos + 8,
563                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
564                          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,                                  interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos + 8,
565                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
566                  }                  }
567    
568                  interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,
569                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
570                  interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,                          interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,
571                                                            uv_dx, uv_dy, stride2, rounding);                                                            uv_dx, uv_dy, stride2, rounding);
572                    }
573                  stop_comp_timer();                  stop_comp_timer();
574          }          }
575    
# Line 474  Line 604 
604          }          }
605    
606          start_timer();          start_timer();
607            if (reduced_resolution)
608            {
609                    if (cbp & 32)
610                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
611                    if (cbp & 16)
612                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
613                    if (cbp & 8)
614                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
615                    if (cbp & 4)
616                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
617                    if (cbp & 2)
618                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
619                    if (cbp & 1)
620                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
621            }
622            else
623            {
624          if (cbp & 32)          if (cbp & 32)
625                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
626          if (cbp & 16)          if (cbp & 16)
# Line 486  Line 633 
633                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);                  transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
634          if (cbp & 1)          if (cbp & 1)
635                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);                  transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
636            }
637          stop_transfer_timer();          stop_transfer_timer();
638  }  }
639    
# Line 493  Line 641 
641  void  void
642  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
643                             Bitstream * bs,                             Bitstream * bs,
644                               int reduced_resolution,
645                             int quant,                             int quant,
646                             int intra_dc_threshold)                             int intra_dc_threshold)
647  {  {
648          uint32_t bound;          uint32_t bound;
649          uint32_t x, y;          uint32_t x, y;
650            uint32_t mb_width = dec->mb_width;
651            uint32_t mb_height = dec->mb_height;
652    
653            if (reduced_resolution)
654            {
655                    mb_width = (dec->width + 31) / 32;
656                    mb_height = (dec->height + 31) / 32;
657            }
658    
659          bound = 0;          bound = 0;
660    
661          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
662                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
663                          MACROBLOCK *mb;                          MACROBLOCK *mb;
664                          uint32_t mcbpc;                          uint32_t mcbpc;
665                          uint32_t cbpc;                          uint32_t cbpc;
# Line 515  Line 672 
672    
673                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
674                          {                          {
675                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
676                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
677                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
678                                    y = bound / mb_width;
679                          }                          }
680                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
681    
# Line 548  Line 706 
706    
707                          if (dec->interlacing) {                          if (dec->interlacing) {
708                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
709                                  DEBUG1("deci: field_dct: ", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
710                          }                          }
711    
712                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
713                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
714    
715                  }                  }
716                  if(dec->out_frm)                  if(dec->out_frm)
717                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
   
718          }          }
719    
720  }  }
# Line 568  Line 726 
726                                    int x,                                    int x,
727                                    int y,                                    int y,
728                                    int k,                                    int k,
729                                    VECTOR * mv,                                    VECTOR * ret_mv,
730                                    int fcode,                                    int fcode,
731                                    const int bound)                                    const int bound)
732  {  {
# Line 579  Line 737 
737          int range = (64 * scale_fac);          int range = (64 * scale_fac);
738    
739          VECTOR pmv;          VECTOR pmv;
740          int mv_x, mv_y;          VECTOR mv;
741    
742          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
743    
744          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
745          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
746    
747          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) result (%i,%i)", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
748    
749          mv_x += pmv.x;          mv.x += pmv.x;
750          mv_y += pmv.y;          mv.y += pmv.y;
751    
752          if (mv_x < low) {          if (mv.x < low) {
753                  mv_x += range;                  mv.x += range;
754          } else if (mv_x > high) {          } else if (mv.x > high) {
755                  mv_x -= range;                  mv.x -= range;
756          }          }
757    
758          if (mv_y < low) {          if (mv.y < low) {
759                  mv_y += range;                  mv.y += range;
760          } else if (mv_y > high) {          } else if (mv.y > high) {
761                  mv_y -= range;                  mv.y -= range;
762          }          }
763    
764          mv->x = mv_x;          ret_mv->x = mv.x;
765          mv->y = mv_y;          ret_mv->y = mv.y;
766    }
767    
768    
769    
770    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
771    {
772            int length = 1 << (fcode+4);
773    
774            if (quarterpel) value *= 2;
775    
776            if (value < -length)
777                    return -length;
778            else if (value >= length)
779                    return length-1;
780            else return value;
781  }  }
782    
783    
784    /* for P_VOP set gmc_warp to NULL */
785  void  void
786  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
787                             Bitstream * bs,                             Bitstream * bs,
788                             int rounding,                             int rounding,
789                               int reduced_resolution,
790                             int quant,                             int quant,
791                             int fcode,                             int fcode,
792                             int intra_dc_threshold)                             int intra_dc_threshold,
793                               const WARPPOINTS *const gmc_warp)
794  {  {
795    
796          uint32_t x, y;          uint32_t x, y;
797          uint32_t bound;          uint32_t bound;
798          int cp_mb, st_mb;          int cp_mb, st_mb;
799            uint32_t mb_width = dec->mb_width;
800            uint32_t mb_height = dec->mb_height;
801    
802            static int framecount=0;
803            if (reduced_resolution)
804            {
805                    mb_width = (dec->width + 31) / 32;
806                    mb_height = (dec->height + 31) / 32;
807            }
808    
809          start_timer();          start_timer();
810          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
811                                     dec->width, dec->height);                                     dec->width, dec->height);
812          stop_edges_timer();          stop_edges_timer();
813    
814            if (gmc_warp)
815            {
816                    char filename[80];
817                    sprintf(filename,"dGMC%05d.pgm",framecount);
818                    // accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16
819                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
820                    {
821                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
822                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
823                                    dec->sprite_warping_points);
824                    }
825    
826                    generate_GMCparameters( dec->sprite_warping_points,
827                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
828                                    dec->width, dec->height, &dec->gmc_data);
829    
830                    generate_GMCimage(&dec->gmc_data, &dec->refn[0],
831                                            mb_width, mb_height,
832                                            dec->edged_width, dec->edged_width/2,
833                                            fcode, 0, 0,
834                                            rounding, dec->mbs, &dec->gmc);
835    
836    /*
837                    sprintf(filename,"dGMC%05d.pgm",framecount);
838                    image_dump_yuvpgm(&dec->gmc,
839                                            dec->edged_width, dec->width, dec->height, filename);
840    
841                    sprintf(filename,"dREF%05d.pgm",framecount);
842                    image_dump_yuvpgm(&dec->refn[0],
843                                            dec->edged_width, dec->width, dec->height, filename);
844                    sprintf(filename,"dCUR%05d.pgm",framecount);
845                    image_dump_yuvpgm(&dec->cur,
846                                            dec->edged_width, dec->width, dec->height, filename);
847                    framecount++;
848    */
849            }
850    
851          bound = 0;          bound = 0;
852    
853          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
854                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
855                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
856                          MACROBLOCK *mb;                          MACROBLOCK *mb;
857    
858                          // skip stuffing                          // skip stuffing
# Line 640  Line 861 
861    
862                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
863                          {                          {
864                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
865                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
866                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
867                                    y = bound / mb_width;
868                          }                          }
869                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
870    
871                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
872    
873                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded                          //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs)))         // not_coded
874                          if (!(BitstreamGetBit(bs)))     // not_coded                          if (!(BitstreamGetBit(bs)))     // block _is_ coded
875                          {                          {
876                                  uint32_t mcbpc;                                  uint32_t mcbpc;
877                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 657  Line 879 
879                                  uint32_t cbpy;                                  uint32_t cbpy;
880                                  uint32_t cbp;                                  uint32_t cbp;
881                                  uint32_t intra;                                  uint32_t intra;
882                                    int mcsel = 0;          // mcsel: '0'=local motion, '1'=GMC
883    
884                                  cp_mb++;                                  cp_mb++;
885                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 673  Line 896 
896                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
897                                  }                                  }
898    
899                                    if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
900                                    {
901                                            mcsel = BitstreamGetBit(bs);
902                                    }
903    
904                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
905                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
906    
907                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
908    
# Line 694  Line 922 
922                                  if (dec->interlacing) {                                  if (dec->interlacing) {
923                                          if (cbp || intra) {                                          if (cbp || intra) {
924                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
925                                                  DEBUG1("decp: field_dct: ", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
926                                          }                                          }
927    
928                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
929                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
930                                                  DEBUG1("decp: field_pred: ", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
931    
932                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
933                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
934                                                          DEBUG1("decp: field_for_top: ", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
935                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
936                                                          DEBUG1("decp: field_for_bot: ", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
937                                                  }                                                  }
938                                          }                                          }
939                                  }                                  }
940    
941                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
942                                          if (dec->interlacing && mb->field_pred) {  
943                                            if (mcsel)
944                                            {
945                                                    mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;
946                                               /* already clipped to fcode */
947    
948                                            } else if (dec->interlacing && mb->field_pred) {
949                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
950                                                                                    fcode, bound);                                                                                    fcode, bound);
951                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1],
# Line 719  Line 953 
953                                          } else {                                          } else {
954                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
955                                                                                    fcode, bound);                                                                                    fcode, bound);
956                                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                                  mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
                                                         mb->mvs[0].x;  
                                                 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                                         mb->mvs[0].y;  
957                                          }                                          }
958                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
959    
# Line 737  Line 968 
968                                          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 =
969                                                  0;                                                  0;
970                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
971                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
972                                          continue;                                          continue;
973                                  }                                  }
974    
975                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,
976                                                                  rounding);                                                                  rounding, reduced_resolution, mcsel);
977                          } else                          // not coded  
978                            }
979                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
980                            {
981                                    mb->mode = MODE_NOT_CODED_GMC;
982                                    mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->amv;
983    
984                                    start_timer();
985    
986                                    transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
987                                                                     dec->gmc.y + (16*y)*dec->edged_width + (16*x),
988                                                                     dec->edged_width);
989    
990                                    transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
991                                                                    dec->gmc.u + (8*y)*dec->edged_width/2 + (8*x),
992                                                                    dec->edged_width/2);
993    
994                                    transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
995                                                                     dec->gmc.v + (8*y)*dec->edged_width/2 + (8*x),
996                                                                     dec->edged_width/2);
997    
998                                    stop_transfer_timer();
999    
1000                                    if(dec->out_frm && cp_mb > 0) {
1001                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1002                                      cp_mb = 0;
1003                                    }
1004                                    st_mb = x+1;
1005                            }
1006                            else    /* not coded P_VOP macroblock */
1007                          {                          {
1008                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
1009    
1010                                  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;
1011                                  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;
   
1012                                  // copy macroblock directly from ref to cur                                  // copy macroblock directly from ref to cur
1013    
1014                                  start_timer();                                  start_timer();
1015    
1016                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  if (reduced_resolution)
1017                                                                   (16 * x),                                  {
1018                                                                   dec->refn[0].y + (16 * y) * dec->edged_width +                                          transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1019                                                                   (16 * x), dec->edged_width);                                                                           dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1020                                                                             dec->edged_width);
1021                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
1022                                                                   (16 * x + 8),                                          transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1023                                                                   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),  
1024                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
1025    
1026                                  transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +                                          transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1027                                                                   (8 * x),                                                                           dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
1028                                                                   dec->edged_width / 2);                                                                   dec->edged_width / 2);
1029                                    }
1030                                    else
1031                                    {
1032                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1033                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1034                                                                             dec->edged_width);
1035    
1036                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1037                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1038                                                                            dec->edged_width/2);
1039    
1040                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1041                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1042                                                                             dec->edged_width/2);
1043                                    }
1044    
1045                                  stop_transfer_timer();                                  stop_transfer_timer();
1046    
1047                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1048                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1049                                    cp_mb = 0;                                    cp_mb = 0;
# Line 878  Line 1135 
1135                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1136                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1137    
1138                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1139                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1140                            uv_dx /= 2;
1141                            uv_dy /= 2;
1142                    }
1143    
1144                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1145                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1146          } else {          } else {
1147                  int sum;                  int sum;
1148    
1149                    if(dec->quarterpel)
1150                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1151                    else
1152                  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));  
1153    
1154                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1155    
1156                    if(dec->quarterpel)
1157                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1158                    else
1159                  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;
1160                  uv_dy =  
1161                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
1162          }          }
1163    
1164          start_timer();          start_timer();
1165            if(dec->quarterpel) {
1166                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1167                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1168                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1169            }
1170            else {
1171          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,
1172                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1173          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,
1174                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);                                                        pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1175          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,
1176                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1177                                                    0);                  interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1178          interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8,                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1179                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,          }
1180                                                    0);  
1181          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,
1182                                                    uv_dx, uv_dy, stride2, 0);                                                    uv_dx, uv_dy, stride2, 0);
1183          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 993  Line 1264 
1264                  uv_dx = pMB->mvs[0].x;                  uv_dx = pMB->mvs[0].x;
1265                  uv_dy = pMB->mvs[0].y;                  uv_dy = pMB->mvs[0].y;
1266    
                 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;  
                 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;  
   
1267                  b_uv_dx = pMB->b_mvs[0].x;                  b_uv_dx = pMB->b_mvs[0].x;
1268                  b_uv_dy = pMB->b_mvs[0].y;                  b_uv_dy = pMB->b_mvs[0].y;
1269    
1270                  b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  if (dec->quarterpel)
1271                  b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  {
1272                            uv_dx /= 2;
1273                            uv_dy /= 2;
1274    
1275                            b_uv_dx /= 2;
1276                            b_uv_dy /= 2;
1277                    }
1278    
1279                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1280                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1281    
1282                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1283                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1284          } else {          } else {
1285                  int sum;                  int sum;
1286    
1287                    if(dec->quarterpel)
1288                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1289                    else
1290                  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));  
1291    
1292                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1293    
1294                    if(dec->quarterpel)
1295                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1296                    else
1297                  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;
1298                  uv_dy =  
1299                          (sum ==                  uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1300                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1301                                                                    (ABS(sum) / 16) * 2));  
1302                    if(dec->quarterpel)
1303                  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);
1304                          pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x +                  else
1305                          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;
1306                  b_uv_dx =  
1307                          (sum ==                  b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1308                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
1309                                                                    (ABS(sum) / 16) * 2));                  if(dec->quarterpel)
1310                            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);
1311                  sum =                  else
1312                          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;
1313                          pMB->b_mvs[3].y;  
1314                  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));  
1315          }          }
1316    
1317    
1318          start_timer();          start_timer();
1319            if(dec->quarterpel) {
1320                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1321                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1322                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1323                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1324                    else {
1325                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1326                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1327                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1328                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1329                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1330                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1331                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1332                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1333                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1334                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1335                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1336                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1337                    }
1338            }
1339            else {
1340          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,
1341                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1342          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 1044  Line 1346 
1346          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,          interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1347                                                    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,
1348                                                    0);                                                    0);
1349            }
1350    
1351          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,
1352                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1353          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,
1354                                                    uv_dy, stride2, 0);                                                    uv_dy, stride2, 0);
1355    
1356    
1357          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos,          if(dec->quarterpel) {
1358                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1359                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1360                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1361                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1362                    else {
1363                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1364                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1365                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1366          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,
1367                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1368                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1369                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1370                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1371                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1372                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1373                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1374                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1375                    }
1376            }
1377            else {
1378                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1379                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1380                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1381                                                    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,
1382                                                    0);                                                    0);
1383          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1384                                                    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,
1385                                                    stride, 0);                                                    stride, 0);
1386          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1387                                                    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,
1388                                                    stride, 0);                                                    stride, 0);
1389          interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos,          }
1390    
1391            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1392                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1393          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,
1394                                                    b_uv_dx, b_uv_dy, stride2, 0);                                                    b_uv_dx, b_uv_dy, stride2, 0);
1395    
1396          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1397                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1398                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1399                                                  stride, 0);                                                  stride, 1, 8);
1400    
1401          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1402                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1403                                                  dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1404                                                  stride, 0);                                                  stride, 1, 8);
1405    
1406          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1407                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1408                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1409                                                  stride, 0);                                                  stride, 1, 8);
1410    
1411          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1412                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1413                                                  dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,                                                  dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1414                                                  stride, 0);                                                  stride, 1, 8);
1415    
1416          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1417                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1418                                                  dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1419                                                  stride2, 0);                                                  stride2, 1, 8);
1420    
1421          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,          interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1422                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1423                                                  dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos,                                                  dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1424                                                  stride2, 0);                                                  stride2, 1, 8);
1425    
1426          stop_comp_timer();          stop_comp_timer();
1427    
# Line 1223  Line 1550 
1550                          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] =
1551                          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;
1552    
1553                          // the last P_VOP is skip macroblock ?                          // skip if the co-located P_VOP macroblock is not coded
1554                            // note: gmc+not_coded isn't skipped
1555    
1556                          if (last_mb->mode == MODE_NOT_CODED) {                          if (last_mb->mode == MODE_NOT_CODED) {
1557                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);                                  //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y);
1558                                  mb->cbp = 0;                                  mb->cbp = 0;
# Line 1337  Line 1666 
1666                                  break;                                  break;
1667    
1668                          default:                          default:
1669                                  DEBUG1("Not support B-frame mb_type =", mb->mb_type);                                  DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1670                          }                          }
1671    
1672                  }                                               // end of FOR                  }                                               // end of FOR
# Line 1362  Line 1691 
1691          *mb2 = temp;          *mb2 = temp;
1692  }  }
1693    
1694    
1695    /* perform post processing if necessary, and output the image */
1696    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1697                                            const XVID_DEC_FRAME * frame, int pp_disable)
1698    {
1699    
1700            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1701            {
1702                    /* note: image is stored to tmp */
1703                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1704                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1705                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1706                                                    8, frame->general);
1707                    img = &dec->tmp;
1708            }
1709    
1710            image_output(img, dec->width, dec->height,
1711                                     dec->edged_width, frame->image, frame->stride,
1712                                     frame->colorspace, dec->interlacing);
1713    }
1714    
1715    
1716  int  int
1717  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1718                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1719  {  {
1720    
1721          Bitstream bs;          Bitstream bs;
1722          uint32_t rounding;          uint32_t rounding;
1723            uint32_t reduced_resolution;
1724          uint32_t quant;          uint32_t quant;
1725          uint32_t fcode_forward;          uint32_t fcode_forward;
1726          uint32_t fcode_backward;          uint32_t fcode_backward;
1727          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1728            WARPPOINTS gmc_warp;
1729          uint32_t vop_type;          uint32_t vop_type;
1730            int success = 0;
1731            int output = 0;
1732            int seen_something = 0;
1733    
1734          start_global_timer();          start_global_timer();
1735    
1736            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1737          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1738    
1739            if ((frame->general & XVID_DEC_DISCONTINUITY))
1740                    dec->frames = 0;
1741    
1742            if (frame->length < 0)  /* decoder flush */
1743            {
1744                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1745                        we have a reference frame, then outout the reference frame */
1746                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1747                    {
1748                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1749                            output = 1;
1750                    }
1751    
1752                    frame->length = 0;
1753                    if (stats)
1754                    {
1755                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1756                            stats->data.vop.time_base = (int)dec->time_base;
1757                            stats->data.vop.time_increment = 0;     //XXX: todo
1758                    }
1759    
1760                    emms();
1761    
1762                    stop_global_timer();
1763                    return XVID_ERR_OK;
1764            }
1765    
1766          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1767    
1768          if(BitstreamShowBits(&bs, 8) == 0x7f)          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1769            if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1770            {
1771                    if (stats)
1772                            stats->notify = XVID_DEC_VOP;
1773                    frame->length = 1;
1774                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1775                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1776                    emms();
1777                  return XVID_ERR_OK;                  return XVID_ERR_OK;
1778            }
1779    
1780          // add by chenm001 <chenm001@163.com>  repeat:
1781          // for support B-frame to reference last 2 frame  
1782          dec->frames++;          vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1783          vop_type =                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1784                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,  
1785                                                           &fcode_backward, &intra_dc_threshold);          DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1786                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1787    
1788            if (vop_type == - 1)
1789            {
1790                    if (success) goto done;
1791                    emms();
1792                    return XVID_ERR_FAIL;
1793            }
1794    
1795            if (vop_type == -2 || vop_type == -3)
1796            {
1797                    if (vop_type == -3)
1798                            decoder_resize(dec);
1799    
1800                    if (stats)
1801                    {
1802                            stats->notify = XVID_DEC_VOL;
1803                            stats->data.vol.general = 0;
1804                            if (dec->interlacing)
1805                                    stats->data.vol.general |= XVID_INTERLACING;
1806                            stats->data.vol.width = dec->width;
1807                            stats->data.vol.height = dec->height;
1808                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1809                            stats->data.vol.par_width = dec->par_width;
1810                            stats->data.vol.par_height = dec->par_height;
1811                            frame->length = BitstreamPos(&bs) / 8;
1812                            emms();
1813                            return XVID_ERR_OK;
1814                    }
1815                    goto repeat;
1816            }
1817    
1818          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
1819    
         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;  
1820    
1821            /* packed_mode: special-N_VOP treament */
1822            if (dec->packed_mode && vop_type == N_VOP)
1823            {
1824                    if (dec->low_delay_default && dec->frames > 0)
1825                    {
1826                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1827                            output = 1;
1828                    }
1829                    /* ignore otherwise */
1830            }
1831            else if (vop_type != B_VOP)
1832            {
1833                    switch(vop_type)
1834                    {
1835          case I_VOP:          case I_VOP:
1836                  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  
1837                  break;                  break;
1838                    case P_VOP :
1839          case B_VOP:                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1840  #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  
1841                  break;                  break;
1842                    case S_VOP :
1843          case N_VOP:                             // vop not coded                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1844                  // when low_delay==0, N_VOP's should interpolate between the past and future frames                                                  fcode_forward, intra_dc_threshold, &gmc_warp);
1845                            break;
1846                    case N_VOP :
1847                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
 #ifdef BFRAMES_DEC  
                 DEBUG1("N_VOP  Time=", dec->time);  
 #endif  
1848                  break;                  break;
1849                    }
1850    
1851          default:                  if (reduced_resolution)
1852                  return XVID_ERR_FAIL;                  {
1853                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1854                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1855                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1856          }          }
1857    
1858  #ifdef BFRAMES_DEC_DEBUG                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1859          if (frame->length != BitstreamPos(&bs) / 8){                  if (!(dec->low_delay_default && dec->packed_mode))
1860                  DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8);                  {
1861                            if (dec->low_delay)
1862                            {
1863                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1864                                    output = 1;
1865                            }
1866                            else if (dec->frames > 0)       /* is the reference frame valid? */
1867                            {
1868                                    /* output the reference frame */
1869                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1870                                    output = 1;
1871                            }
1872          }          }
 #endif  
         frame->length = BitstreamPos(&bs) / 8;  
1873    
1874                    image_swap(&dec->refn[0], &dec->refn[1]);
1875                    image_swap(&dec->cur, &dec->refn[0]);
1876                    mb_swap(&dec->mbs, &dec->last_mbs);
1877                    dec->last_reduced_resolution = reduced_resolution;
1878    
1879  #ifdef BFRAMES_DEC                  dec->frames++;
1880          // test if no B_VOP                  seen_something = 1;
1881          if (dec->low_delay || dec->frames == 0) {  
1882  #endif          }else{  /* B_VOP */
         image_output(&dec->cur, dec->width, dec->height, dec->edged_width,  
                                          frame->image, frame->stride, frame->colorspace);  
1883    
1884  #ifdef BFRAMES_DEC                  if (dec->low_delay)
1885                    {
1886                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1887                            dec->low_delay = 1;
1888                    }
1889    
1890                    if (dec->frames < 2)
1891                    {
1892                            /* attemping to decode a bvop without atleast 2 reference frames */
1893                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1894                                                    "broken b-frame, mising ref frames");
1895                    }else if (dec->time_pp <= dec->time_bp) {
1896                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1897                            decoded in vfw. */
1898                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1899                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1900          } else {          } else {
1901                  if (dec->frames >= 1) {                          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);  
1902                          }                          }
1903                          stop_conv_timer();  
1904                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1905                    output = 1;
1906                    dec->frames++;
1907                  }                  }
1908    
1909            BitstreamByteAlign(&bs);
1910    
1911            /* low_delay_default mode: repeat in packed_mode */
1912            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1913            {
1914                    success = 1;
1915                    goto repeat;
1916          }          }
 #endif  
1917    
1918          if (vop_type == I_VOP || vop_type == P_VOP) {  done :
                 image_swap(&dec->refn[0], &dec->refn[1]);  
                 image_swap(&dec->cur, &dec->refn[0]);  
1919    
1920                  // swap MACROBLOCK          /* low_delay_default mode: if we've gotten here without outputting anything,
1921                  // the Divx will not set the low_delay flage some times             then output the recently decoded frame, or print an error message  */
1922                  // so follow code will wrong to not swap at that time          if (dec->low_delay_default && output == 0)
1923                  // this will broken bitstream! so I'm change it,          {
1924                  // But that is not the best way! can anyone tell me how                  if (dec->packed_mode && seen_something)
1925                  // to do another way?                  {
1926                  // 18-07-2002   MinChen<chenm001@163.com>                          /* output the recently decoded frame */
1927                  //if (!dec->low_delay && vop_type == P_VOP)                          decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1928                  if (vop_type == P_VOP)                          output = 1;
1929                          mb_swap(&dec->mbs, &dec->last_mbs);                  }
1930                    else
1931                    {
1932                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1933                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1934                                    "warning: nothing to output");
1935                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1936                                    "bframe decoder lag");
1937    
1938                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1939                    }
1940            }
1941    
1942            frame->length = BitstreamPos(&bs) / 8;
1943    
1944            if (stats)
1945            {
1946                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1947                    stats->data.vop.time_base = (int)dec->time_base;
1948                    stats->data.vop.time_increment = 0;     //XXX: todo
1949          }          }
1950    
1951          emms();          emms();

Legend:
Removed from v.1.37.2.6  
changed lines
  Added in v.1.37.2.27

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