[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.24, Wed Jul 3 12:32:50 2002 UTC revision 1.37.2.27, Sat Jan 11 14:59:23 2003 UTC
# Line 32  Line 32 
32   *   *
33   *  History:   *  History:
34   *   *
35     *  15.07.2002  fix a bug in B-frame decode at DIRECT mode
36     *              MinChen <chenm001@163.com>
37     *  10.07.2002  added BFRAMES_DEC_DEBUG support
38     *              Fix a little bug for low_delay flage
39     *              MinChen <chenm001@163.com>
40   *  28.06.2002  added basic resync support to iframe/pframe_decode()   *  28.06.2002  added basic resync support to iframe/pframe_decode()
41   *      22.06.2002      added primative N_VOP support   *      22.06.2002      added primative N_VOP support
42   *                              #define BFRAMES_DEC now enables Minchenm's bframe decoder   *                              #define BFRAMES_DEC now enables Minchen's bframe decoder
43   *  08.05.2002  add low_delay support for B_VOP decode   *  08.05.2002  add low_delay support for B_VOP decode
44   *              MinChen <chenm001@163.com>   *              MinChen <chenm001@163.com>
45   *  05.05.2002  fix some B-frame decode problem   *  05.05.2002  fix some B-frame decode problem
# Line 54  Line 59 
59   *   *
60   *************************************************************************/   *************************************************************************/
61    
62    #include <stdio.h>
63  #include <stdlib.h>  #include <stdlib.h>
64  #include <string.h>  #include <string.h>
65    
66    #ifdef BFRAMES_DEC_DEBUG
67            #define BFRAMES_DEC
68    #endif
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 70  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"
89  #include "utils/timer.h"  #include "utils/timer.h"
90  #include "utils/emms.h"  #include "utils/emms.h"
91    #include "motion/motion.h"
92    
93  #include "image/image.h"  #include "image/image.h"
94  #include "image/colorspace.h"  #include "image/colorspace.h"
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 111  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 119  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);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
144                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
145                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
# Line 127  Line 147 
147                  return XVID_ERR_MEMORY;                  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);
152                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
153                    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);
156                    return XVID_ERR_MEMORY;
157            }
158    
159            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);
162                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
163                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
164                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
165                    xvid_free(dec);
166                    return XVID_ERR_MEMORY;
167            }
168    
169          dec->mbs =          dec->mbs =
170                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
171                                          CACHE_LINE);                                          CACHE_LINE);
# Line 134  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->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 151  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->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 174  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->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 205  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 218  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 259  Line 350 
350                  start_timer();                  start_timer();
351                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
352                  {                  {
353                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
354                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
355    
356                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
357                  }                  }
358                  stop_coding_timer();                  stop_coding_timer();
359    
# Line 279  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 287  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))  
 static const uint32_t roundtab[16] =  
         { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };  
   
   
408  // decode an inter macroblock  // decode an inter macroblock
409    
410  void  void
# Line 317  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 325  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                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
471                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         } else {  
                 int sum;  
472    
473                  sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
474                  uv_dx =                  uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
                         (sum ==  
                          0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +  
                                                                   (ABS(sum) / 16) * 2));  
475    
476                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
477                  uv_dy =                  if (reduced_resolution)
478                          (sum ==                  {
479                           0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] +                          interpolate32x32_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
480                                                                    (ABS(sum) / 16) * 2));                                                                    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) {
490                                    interpolate16x16_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
491                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
492                                                                                            mv[0].x, mv[0].y, stride, rounding);
493                            }
494                            else {
495                                    interpolate16x16_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
496                                                                              mv[0].x, mv[0].y, stride, rounding);
497          }          }
498    
499                            interpolate8x8_switch(dec->cur.u, pU_Ref , 8 * x_pos, 8 * y_pos,
500                                                                      uv_dx, uv_dy, stride2, rounding);
501                            interpolate8x8_switch(dec->cur.v, pV_Ref , 8 * x_pos, 8 * y_pos,
502                                                                      uv_dx, uv_dy, stride2, rounding);
503                    }
504                    stop_comp_timer();
505    
506            } else {        /* MODE_INTER4V */
507                    int sum;
508    
509                    if(dec->quarterpel)
510                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
511                    else
512                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
513    
514                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
515    
516                    if(dec->quarterpel)
517                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
518                    else
519                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
520    
521                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
522    
523          start_timer();          start_timer();
524          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos,                  if (reduced_resolution)
525                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding);                  {
526          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos,
527                                                    16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride,                                                                    mv[0].x, mv[0].y, stride,  rounding);
528                                                    rounding);                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos,
529          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos,                                                                    mv[1].x, mv[1].y, stride,  rounding);
530                                                    16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos, 32*y_pos + 16,
531                                                    rounding);                                                                    mv[2].x, mv[2].y, stride,  rounding);
532          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8,                          interpolate16x16_switch(dec->cur.y, pY_Ref , 32*x_pos + 16, 32*y_pos + 16,
533                                                    16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,                                                                    mv[3].x, mv[3].y, stride,  rounding);
534                                                    rounding);                          interpolate16x16_switch(dec->cur.u, pU_Ref , 16 * x_pos, 16 * y_pos,
535          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,                                                                    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) {
544                                    interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
545                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
546                                                                                      mv[0].x, mv[0].y, stride,  rounding);
547                                    interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
548                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
549                                                                                      mv[1].x, mv[1].y, stride,  rounding);
550                                    interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
551                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
552                                                                                      mv[2].x, mv[2].y, stride,  rounding);
553                                    interpolate8x8_quarterpel(dec->cur.y, pY_Ref , dec->qtmp.y, dec->qtmp.y + 64,
554                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
555                                                                                      mv[3].x, mv[3].y, stride,  rounding);
556                            }
557                            else {
558                                    interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos,
559                                                                              mv[0].x, mv[0].y, stride,  rounding);
560                                    interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos,
561                                                                              mv[1].x, mv[1].y, stride,  rounding);
562                                    interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos, 16*y_pos + 8,
563                                                                              mv[2].x, mv[2].y, stride,  rounding);
564                                    interpolate8x8_switch(dec->cur.y, pY_Ref , 16*x_pos + 8, 16*y_pos + 8,
565                                                                              mv[3].x, mv[3].y, stride,  rounding);
566                            }
567    
568                            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    
576          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
577                    int direction = dec->alternate_vertical_scan ? 2 : 0;
578    
579                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
580                  {                  {
581                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
582    
583                          start_timer();                          start_timer();
584                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
585                          stop_coding_timer();                          stop_coding_timer();
586    
587                          start_timer();                          start_timer();
# Line 404  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 416  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 423  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 445  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 471  Line 699 
699                                  }                                  }
700                          }                          }
701                          mb->quant = quant;                          mb->quant = quant;
702                            mb->mvs[0].x = mb->mvs[0].y =
703                            mb->mvs[1].x = mb->mvs[1].y =
704                            mb->mvs[2].x = mb->mvs[2].y =
705                            mb->mvs[3].x = mb->mvs[3].y =0;
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)
717                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
718          }          }
719    
720  }  }
# Line 491  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 502  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;
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->interlacing);                                     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                  for (x = 0; x < dec->mb_width; x++) {                  cp_mb = st_mb = 0;
855                    for (x = 0; x < mb_width; x++) {
856                          MACROBLOCK *mb;                          MACROBLOCK *mb;
857    
858                          // skip stuffing                          // skip stuffing
# Line 561  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 578  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++;
885                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
886                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
887                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
# Line 593  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 612  Line 920 
920                                  mb->quant = quant;                                  mb->quant = quant;
921    
922                                  if (dec->interlacing) {                                  if (dec->interlacing) {
923                                            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 637  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 ==                                  } else if (mb->mode == MODE_INTER4V ) {
959                                                     MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) {  
960                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
961                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
962                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);                                          get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
# Line 655  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                          {                          {
                                 //DEBUG2("P-frame MB at (X,Y)=",x,y);  
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),
1028                                                                   dec->refn[0].v +                                                                           dec->edged_width/2);
1029                                                                   (8 * y) * dec->edged_width / 2 + (8 * x),                                  }
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);                                                                   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) {
1048                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1049                                      cp_mb = 0;
1050                          }                          }
1051                                    st_mb = x+1;
1052                  }                  }
1053          }          }
1054                    if(dec->out_frm && cp_mb > 0)
1055                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1056            }
1057  }  }
1058    
1059    
# Line 786  Line 1130 
1130          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1131          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1132    
1133    
1134          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
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 826  Line 1185 
1185          stop_comp_timer();          stop_comp_timer();
1186    
1187          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1188                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1189    
1190                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1191                  {                  {
1192                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1193    
1194                          start_timer();                          start_timer();
1195                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1196                          stop_coding_timer();                          stop_coding_timer();
1197    
1198                          start_timer();                          start_timer();
# Line 869  Line 1230 
1230          stop_transfer_timer();          stop_transfer_timer();
1231  }  }
1232    
   
1233  // add by MinChen <chenm001@163.com>  // add by MinChen <chenm001@163.com>
1234  // decode an B-frame direct &  inter macroblock  // decode an B-frame direct &  inter macroblock
1235  void  void
# Line 879  Line 1239 
1239                                                             const MACROBLOCK * pMB,                                                             const MACROBLOCK * pMB,
1240                                                             const uint32_t x_pos,                                                             const uint32_t x_pos,
1241                                                             const uint32_t y_pos,                                                             const uint32_t y_pos,
                                                            const uint32_t cbp,  
1242                                                             Bitstream * bs)                                                             Bitstream * bs)
1243  {  {
1244    
# Line 894  Line 1253 
1253          int b_uv_dx, b_uv_dy;          int b_uv_dx, b_uv_dy;
1254          uint32_t i;          uint32_t i;
1255          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1256        const uint32_t cbp = pMB->cbp;
1257    
1258          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1259          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1260          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1261    
1262    
1263          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {          if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
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 954  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);                                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1362          interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8,                  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);
1366                            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_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos,          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1397                                           stride);                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1398          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos,                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1399                                           stride);                                                  stride, 1, 8);
1400          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8,  
1401                                           stride);          interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1402          interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8,                                                  dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1403                                           16 * y_pos + 8, stride);                                                  dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1404          interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos,                                                  stride, 1, 8);
1405                                           stride2);  
1406          interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1407                                           stride2);                                                  dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1408                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1409                                                    stride, 1, 8);
1410    
1411            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,
1413                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1414                                                    stride, 1, 8);
1415    
1416            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1417                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1418                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1419                                                    stride2, 1, 8);
1420    
1421            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1422                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1423                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1424                                                    stride2, 1, 8);
1425    
1426          stop_comp_timer();          stop_comp_timer();
1427    
1428          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
1429                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1430    
1431                  if (cbp & (1 << (5 - i)))       // coded                  if (cbp & (1 << (5 - i)))       // coded
1432                  {                  {
1433                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        // clear
1434    
1435                          start_timer();                          start_timer();
1436                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
1437                          stop_coding_timer();                          stop_coding_timer();
1438    
1439                          start_timer();                          start_timer();
# Line 1079  Line 1515 
1515                             int fcode_forward,                             int fcode_forward,
1516                             int fcode_backward)                             int fcode_backward)
1517  {  {
   
1518          uint32_t x, y;          uint32_t x, y;
1519          VECTOR mv, zeromv;          VECTOR mv;
1520            const VECTOR zeromv = {0,0};
1521    #ifdef BFRAMES_DEC_DEBUG
1522            FILE *fp;
1523            static char first=0;
1524    #define BFRAME_DEBUG    if (!first && fp){ \
1525                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1526            }
1527    #endif
1528    
1529          start_timer();          start_timer();
1530          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1531                                     dec->width, dec->height, dec->interlacing);                                     dec->width, dec->height);
1532          //image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing);          image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1533                                       dec->width, dec->height);
1534          stop_edges_timer();          stop_edges_timer();
1535    
1536    #ifdef BFRAMES_DEC_DEBUG
1537            if (!first){
1538                    fp=fopen("C:\\XVIDDBG.TXT","w");
1539            }
1540    #endif
1541    
1542          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < dec->mb_height; y++) {
1543                  // Initialize Pred Motion Vector                  // Initialize Pred Motion Vector
1544                  dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0;                  dec->p_fmv = dec->p_bmv = zeromv;
1545                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < dec->mb_width; x++) {
1546                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];                          MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1547                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];                          MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1548    
1549                          mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y =                          mv =
1550                                  0;                          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;
1552    
1553                            // skip if the co-located P_VOP macroblock is not coded
1554                            // note: gmc+not_coded isn't skipped
1555    
                         // the last P_VOP is skip macroblock ?  
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);
                                 mb->mb_type = MODE_FORWARD;  
1558                                  mb->cbp = 0;                                  mb->cbp = 0;
1559                                  mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;  #ifdef BFRAMES_DEC_DEBUG
1560                                  mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                  mb->mb_type = MODE_NOT_CODED;
1561                                  mb->quant = 8;          BFRAME_DEBUG
1562    #endif
1563                                    mb->mb_type = MODE_FORWARD;
1564                                    mb->quant = last_mb->quant;
1565                                    //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1566                                    //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1567    
1568                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1569                                  continue;                                  continue;
1570                          }                          }
                         //t=BitstreamShowBits(bs,32);  
1571    
1572                          if (!BitstreamGetBit(bs)) {     // modb=='0'                          if (!BitstreamGetBit(bs)) {     // modb=='0'
1573                                  const uint8_t modb2 = BitstreamGetBit(bs);                                  const uint8_t modb2 = BitstreamGetBit(bs);
# Line 1132  Line 1587 
1587                                          } else if (quant < 1) {                                          } else if (quant < 1) {
1588                                                  quant = 1;                                                  quant = 1;
1589                                          }                                          }
                                 } else {  
                                         quant = 8;  
1590                                  }                                  }
                                 mb->quant = quant;  
1591                          } else {                          } else {
1592                                  mb->mb_type = MODE_DIRECT_NONE_MV;                                  mb->mb_type = MODE_DIRECT_NONE_MV;
1593                                  mb->cbp = 0;                                  mb->cbp = 0;
1594                          }                          }
1595    
1596                          mb->mode = MODE_INTER;                          mb->quant = quant;
1597                            mb->mode = MODE_INTER4V;
1598                          //DEBUG1("Switch bm_type=",mb->mb_type);                          //DEBUG1("Switch bm_type=",mb->mb_type);
1599    
1600    #ifdef BFRAMES_DEC_DEBUG
1601            BFRAME_DEBUG
1602    #endif
1603    
1604                          switch (mb->mb_type) {                          switch (mb->mb_type) {
1605                          case MODE_DIRECT:                          case MODE_DIRECT:
1606                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv);                                  get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1607    
1608                          case MODE_DIRECT_NONE_MV:                          case MODE_DIRECT_NONE_MV:
1609                                  {                               // Because this file is a C file not C++ so I use '{' to define var                                  {
1610                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD =                                          const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
                                                 dec->time_pp;  
1611                                          int i;                                          int i;
1612    
1613                                          for (i = 0; i < 4; i++) {                                          for (i = 0; i < 4; i++) {
1614                                                  mb->mvs[i].x =                                                  mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1615                                                          (int32_t) ((TRB * last_mb->mvs[i].x) / TRD +                                                                        / TRD + mv.x);
1616                                                                             mb->mvs[0].x);                                                  mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1617                                                  mb->b_mvs[i].x =                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].x)
1618                                                          (int32_t) ((mb->mvs[0].x ==                                                                                    / TRD
1619                                                                                  0) ? ((TRB -                                                                                  : mb->mvs[i].x - last_mb->mvs[i].x);
1620                                                                                             TRD) * last_mb->mvs[i].x) /                                                  mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1621                                                                             TRD : mb->mvs[i].x - last_mb->mvs[i].x);                                                                        / TRD + mv.y);
1622                                                  mb->mvs[i].y =                                                  mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1623                                                          (int32_t) ((TRB * last_mb->mvs[i].y) / TRD +                                                                                  ? ((TRB - TRD) * last_mb->mvs[i].y)
1624                                                                             mb->mvs[0].y);                                                                                    / TRD
1625                                                  mb->b_mvs[i].y =                                                                              : mb->mvs[i].y - last_mb->mvs[i].y);
                                                         (int32_t) ((mb->mvs[0].y ==  
                                                                                 0) ? ((TRB -  
                                                                                            TRD) * last_mb->mvs[i].y) /  
                                                                            TRD : mb->mvs[i].y - last_mb->mvs[i].y);  
1626                                          }                                          }
1627                                          //DEBUG("B-frame Direct!\n");                                          //DEBUG("B-frame Direct!\n");
1628                                  }                                  }
                                 mb->mode = MODE_INTER4V;  
1629                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1630                                                                                             mb, x, y, mb->cbp, bs);                                                                                             mb, x, y, bs);
1631                                  break;                                  break;
1632    
1633                          case MODE_INTERPOLATE:                          case MODE_INTERPOLATE:
1634                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1635                                                                          dec->p_fmv);                                                                          dec->p_fmv);
1636                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1637    
1638                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],                                  get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1639                                                                          fcode_backward, dec->p_bmv);                                                                          fcode_backward, dec->p_bmv);
1640                                  dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x =                                  dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1641                                          mb->b_mvs[3].x = mb->b_mvs[0].x;                                          mb->b_mvs[3] = mb->b_mvs[0];
                                 dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y =  
                                         mb->b_mvs[3].y = mb->b_mvs[0].y;  
1642    
1643                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],                                  decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1644                                                                                             mb, x, y, mb->cbp, bs);                                                                                             mb, x, y, bs);
1645                                  //DEBUG("B-frame Bidir!\n");                                  //DEBUG("B-frame Bidir!\n");
1646                                  break;                                  break;
1647    
1648                          case MODE_BACKWARD:                          case MODE_BACKWARD:
1649                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1650                                                                          dec->p_bmv);                                                                          dec->p_bmv);
1651                                  dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1652    
1653                                    mb->mode = MODE_INTER;
1654                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1655                                  //DEBUG("B-frame Backward!\n");                                  //DEBUG("B-frame Backward!\n");
1656                                  break;                                  break;
# Line 1214  Line 1658 
1658                          case MODE_FORWARD:                          case MODE_FORWARD:
1659                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,                                  get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1660                                                                          dec->p_fmv);                                                                          dec->p_fmv);
1661                                  dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x =                                  dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
                                         mb->mvs[0].x;  
                                 dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y =  
                                         mb->mvs[0].y;  
1662    
1663                                    mb->mode = MODE_INTER;
1664                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);                                  decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1665                                  //DEBUG("B-frame Forward!\n");                                  //DEBUG("B-frame Forward!\n");
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
1673          }          }
1674    #ifdef BFRAMES_DEC_DEBUG
1675            if (!first){
1676                    first=1;
1677                    if (fp)
1678                            fclose(fp);
1679            }
1680    #endif
1681  }  }
1682    
1683  // swap two MACROBLOCK array  // swap two MACROBLOCK array
# Line 1242  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;
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          // add by chenm001 <chenm001@163.com>          // XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's
1769          // for support B-frame to reference last 2 frame          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1770          dec->frames++;          {
1771          vop_type =                  if (stats)
1772                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                          stats->notify = XVID_DEC_VOP;
1773                                                           &fcode_backward, &intra_dc_threshold);                  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;
1778            }
1779    
1780    repeat:
1781    
1782            vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1783                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1784    
1785            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);
1848                  break;                  break;
   
         default:  
                 return XVID_ERR_FAIL;  
1849          }          }
1850    
1851          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution)
1852                    {
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                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1859          // test if no B_VOP                  if (!(dec->low_delay_default && dec->packed_mode))
1860          if (dec->low_delay) {                  {
1861  #endif                          if (dec->low_delay)
1862                  image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                          {
1863                                           frame->image, frame->stride, frame->colorspace);                                  decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1864  #ifdef BFRAMES_DEC                                  output = 1;
         } else {  
                 if (dec->frames >= 1) {  
                         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);  
1865                          }                          }
1866                          stop_conv_timer();                          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  
1873    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1874                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1875                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
                 // swap MACROBLOCK  
                 if (dec->low_delay && vop_type == P_VOP)  
1876                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1877                    dec->last_reduced_resolution = reduced_resolution;
1878    
1879                    dec->frames++;
1880                    seen_something = 1;
1881    
1882            }else{  /* B_VOP */
1883    
1884                    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{
1901                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1902                    }
1903    
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            }
1917    
1918    done :
1919    
1920            /* low_delay_default mode: if we've gotten here without outputting anything,
1921               then output the recently decoded frame, or print an error message  */
1922            if (dec->low_delay_default && output == 0)
1923            {
1924                    if (dec->packed_mode && seen_something)
1925                    {
1926                            /* output the recently decoded frame */
1927                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1928                            output = 1;
1929                    }
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.24  
changed lines
  Added in v.1.37.2.27

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