[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.46, Tue Feb 11 21:56:31 2003 UTC revision 1.48, Mon Feb 17 23:33:12 2003 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  -  Decoder main module  -   *  - Decoder Module -
  *  
  *  Copyright(C) 2002 MinChen <chenm001@163.com>  
  *               2002 Peter Ross <pross@xvid.org>  
5   *   *
6   *  This file is part of XviD, a free MPEG-4 video encoder/decoder   *  This file is part of XviD, a free MPEG-4 video encoder/decoder
7   *   *
8   *  XviD is free software; you can redistribute it and/or modify it   *  This program is free software; you can redistribute it and/or modify
9   *  under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
10   *  the Free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation; either version 2 of the License, or
11   *  (at your option) any later version.   *  (at your option) any later version.
12   *   *
# Line 22  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
  *  Under section 8 of the GNU General Public License, the copyright  
  *  holders of XVID explicitly forbid distribution in the following  
  *  countries:  
  *  
  *    - Japan  
  *    - United States of America  
  *  
  *  Linking XviD statically or dynamically with other modules is making a  
  *  combined work based on XviD.  Thus, the terms and conditions of the  
  *  GNU General Public License cover the whole combination.  
  *  
  *  As a special exception, the copyright holders of XviD give you  
  *  permission to link XviD with independent modules that communicate with  
  *  XviD solely through the VFW1.1 and DShow interfaces, regardless of the  
  *  license terms of these independent modules, and to copy and distribute  
  *  the resulting combined work under terms of your choice, provided that  
  *  every copy of the combined work is accompanied by a complete copy of  
  *  the source code of XviD (the version of XviD used to produce the  
  *  combined work), being distributed under the terms of the GNU General  
  *  Public License plus this exception.  An independent module is a module  
  *  which is not derived from or based on XviD.  
  *  
  *  Note that people who make modified versions of XviD are not obligated  
  *  to grant this special exception for their modified versions; it is  
  *  their choice whether to do so.  The GNU General Public License gives  
  *  permission to release a modified version without this exception; this  
  *  exception also makes it possible to release a modified version which  
  *  carries forward this exception.  
  *  
22   * $Id$   * $Id$
23   *   *
24   *************************************************************************/   ****************************************************************************/
25    
26    #include <stdio.h>
27  #include <stdlib.h>  #include <stdlib.h>
28  #include <string.h>  #include <string.h>
29    
# Line 64  Line 33 
33    
34  #include "xvid.h"  #include "xvid.h"
35  #include "portab.h"  #include "portab.h"
36    #include "global.h"
37    
38  #include "decoder.h"  #include "decoder.h"
39  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
# Line 75  Line 45 
45  #include "dct/fdct.h"  #include "dct/fdct.h"
46  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
47  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
48    #include "image/reduced.h"
49    #include "image/font.h"
50    
51  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
52  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
53  #include "utils/timer.h"  #include "utils/timer.h"
54  #include "utils/emms.h"  #include "utils/emms.h"
55    #include "motion/motion.h"
56    
57  #include "image/image.h"  #include "image/image.h"
58  #include "image/colorspace.h"  #include "image/colorspace.h"
59  #include "utils/mem_align.h"  #include "utils/mem_align.h"
60    
61  int  int
62  decoder_create(XVID_DEC_PARAM * param)  decoder_resize(DECODER * dec)
63  {  {
64          DECODER *dec;          /* free existing */
65            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
66            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
67            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
68            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
69            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
70    
71          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);          image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
         if (dec == NULL) {  
                 return XVID_ERR_MEMORY;  
         }  
         param->handle = dec;  
72    
73          dec->width = param->width;          if (dec->last_mbs)
74          dec->height = param->height;                  xvid_free(dec->last_mbs);
75            if (dec->mbs)
76                    xvid_free(dec->mbs);
77    
78            /* realloc */
79          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
80          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
81    
82          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
83          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
         dec->low_delay = 0;  
84    
85          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
86                  xvid_free(dec);                  xvid_free(dec);
# Line 116  Line 92 
92                  xvid_free(dec);                  xvid_free(dec);
93                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
94          }          }
95          /* add by chenm001 <chenm001@163.com> */  
96          /* for support B-frame to reference last 2 frame */          /* Support B-frame to reference last 2 frame */
97          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) {
98                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
99                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
100                  xvid_free(dec);                  xvid_free(dec);
101                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
102          }          }
103          if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) {          if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
104                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
105                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
106                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
107                    xvid_free(dec);
108                    return XVID_ERR_MEMORY;
109            }
110    
111            if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
112                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
113                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
114                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
115                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
116                    xvid_free(dec);
117                    return XVID_ERR_MEMORY;
118            }
119    
120            if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
121                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
122                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
123                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
124                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
125                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
126                  xvid_free(dec);                  xvid_free(dec);
127                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
128          }          }
# Line 139  Line 134 
134                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
135                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
136                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
137                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
138                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
139                  xvid_free(dec);                  xvid_free(dec);
140                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
141          }          }
   
142          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);          memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
143    
144          /* add by chenm001 <chenm001@163.com> */          /* For skip MB flag */
         /* for skip MB flag */  
145          dec->last_mbs =          dec->last_mbs =
146                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,                  xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
147                                          CACHE_LINE);                                          CACHE_LINE);
# Line 156  Line 150 
150                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
151                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
152                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);                  image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
153                  image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);                  image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
154                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
155                  xvid_free(dec);                  xvid_free(dec);
156                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
157          }          }
158    
159          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);
160    
161            return XVID_ERR_OK;
162    }
163    
164    
165    int
166    decoder_create(XVID_DEC_PARAM * param)
167    {
168            DECODER *dec;
169    
170            dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
171            if (dec == NULL) {
172                    return XVID_ERR_MEMORY;
173            }
174            memset(dec, 0, sizeof(DECODER));
175    
176            param->handle = dec;
177    
178            dec->width = param->width;
179            dec->height = param->height;
180    
181            image_null(&dec->cur);
182            image_null(&dec->refn[0]);
183            image_null(&dec->refn[1]);
184            image_null(&dec->tmp);
185            image_null(&dec->qtmp);
186    
187            /* image based GMC */
188            image_null(&dec->gmc);
189    
190    
191            dec->mbs = NULL;
192            dec->last_mbs = NULL;
193    
194          init_timer();          init_timer();
195    
196          /* add by chenm001 <chenm001@163.com> */          /* For B-frame support (used to save reference frame's time */
197          /* for support B-frame to save reference frame's time */          dec->frames = 0;
         dec->frames = -1;  
198          dec->time = dec->time_base = dec->last_time_base = 0;          dec->time = dec->time_base = dec->last_time_base = 0;
199            dec->low_delay = 0;
200            dec->packed_mode = 0;
201    
202            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
203    
204            if (dec->fixed_dimensions)
205                    return decoder_resize(dec);
206            else
207          return XVID_ERR_OK;          return XVID_ERR_OK;
208  }  }
209    
# Line 179  Line 213 
213  {  {
214          xvid_free(dec->last_mbs);          xvid_free(dec->last_mbs);
215          xvid_free(dec->mbs);          xvid_free(dec->mbs);
216    
217            /* image based GMC */
218            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
219    
220          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
221          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);          image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
222          image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height);          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
223            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
224          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
225          xvid_free(dec);          xvid_free(dec);
226    
# Line 199  Line 238 
238    
239    
240  /* decode an intra macroblock */  /* decode an intra macroblock */
   
241  void  void
242  decoder_mbintra(DECODER * dec,  decoder_mbintra(DECODER * dec,
243                                  MACROBLOCK * pMB,                                  MACROBLOCK * pMB,
# Line 210  Line 248 
248                                  Bitstream * bs,                                  Bitstream * bs,
249                                  const uint32_t quant,                                  const uint32_t quant,
250                                  const uint32_t intra_dc_threshold,                                  const uint32_t intra_dc_threshold,
251                                  const unsigned int bound)                                  const unsigned int bound,
252                                    const int reduced_resolution)
253  {  {
254    
255          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 223  Line 262 
262          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
263          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
264    
265            if (reduced_resolution) {
266                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
267                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
268                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
269            }else{
270          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
271          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
272          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
273            }
274    
275          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
276    
# Line 264  Line 309 
309                  start_timer();                  start_timer();
310                  if (cbp & (1 << (5 - i)))       /* coded */                  if (cbp & (1 << (5 - i)))       /* coded */
311                  {                  {
312                          get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i],                          int direction = dec->alternate_vertical_scan ?
313                                                          start_coeff);                                  2 : pMB->acpred_directions[i];
314    
315                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
316                  }                  }
317                  stop_coding_timer();                  stop_coding_timer();
318    
# Line 284  Line 331 
331                  start_timer();                  start_timer();
332                  idct(&data[i * 64]);                  idct(&data[i * 64]);
333                  stop_idct_timer();                  stop_idct_timer();
334    
335          }          }
336    
337          if (dec->interlacing && pMB->field_dct) {          if (dec->interlacing && pMB->field_dct) {
# Line 292  Line 340 
340          }          }
341    
342          start_timer();          start_timer();
343    
344            if (reduced_resolution)
345            {
346                    next_block*=2;
347                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
348                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
349                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
350                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
351                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
352                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
353            }else{
354          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);          transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
355          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);          transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
356          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);          transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
357          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);          transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
358          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);          transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
359          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);          transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
360            }
361          stop_transfer_timer();          stop_transfer_timer();
362  }  }
363    
364    
365    
366    
   
 #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 };  
   
   
367  /* decode an inter macroblock */  /* decode an inter macroblock */
   
368  void  void
369  decoder_mbinter(DECODER * dec,  decoder_mbinter(DECODER * dec,
370                                  const MACROBLOCK * pMB,                                  const MACROBLOCK * pMB,
371                                  const uint32_t x_pos,                                  const uint32_t x_pos,
372                                  const uint32_t y_pos,                                  const uint32_t y_pos,
373                                  const uint32_t acpred_flag,                                  const uint32_t fcode,
374                                  const uint32_t cbp,                                  const uint32_t cbp,
375                                  Bitstream * bs,                                  Bitstream * bs,
376                                  const uint32_t quant,                                  const uint32_t quant,
377                                  const uint32_t rounding)                                  const uint32_t rounding,
378                                    const int reduced_resolution)
379  {  {
380    
381          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);          DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
# Line 330  Line 383 
383    
384          uint32_t stride = dec->edged_width;          uint32_t stride = dec->edged_width;
385          uint32_t stride2 = stride / 2;          uint32_t stride2 = stride / 2;
386          uint32_t next_block = stride * 8;          uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
387          uint32_t i;          uint32_t i;
388          uint32_t iQuant = pMB->quant;          uint32_t iQuant = pMB->quant;
389          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
390    
391          int uv_dx, uv_dy;          int uv_dx, uv_dy;
392            VECTOR mv[4];   /* local copy of mvs */
393    
394            if (reduced_resolution) {
395                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
396                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
397                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
398                    for (i = 0; i < 4; i++) {
399                            mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
400                            mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
401                    }
402            } else {
403          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);          pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
404          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);          pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
405          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);          pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
406                    for (i = 0; i < 4; i++)
407                            mv[i] = pMB->mvs[i];
408            }
409    
410          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;  
411    
412                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                  uv_dx = mv[0].x / (1 + dec->quarterpel);
413                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                  uv_dy = mv[0].y / (1 + dec->quarterpel);
         } else {  
                 int sum;  
                 sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;  
414    
415                  uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                  uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
416                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
417    
418                  sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;                  start_timer();
419                    if (reduced_resolution)
420                    {
421                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
422                                                                      mv[0].x, mv[0].y, stride,  rounding);
423                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
424                                                                      uv_dx, uv_dy, stride2, rounding);
425                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
426                                                                      uv_dx, uv_dy, stride2, rounding);
427    
428                    }
429                    else
430                    {
431                            if(dec->quarterpel) {
432                                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->qtmp.y, dec->qtmp.y + 64,
433                                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
434                                                                                            mv[0].x, mv[0].y, stride, rounding);
435                            }
436                            else {
437                                    interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
438                                                                              mv[0].x, mv[0].y, stride, rounding);
439                            }
440    
441                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2));                          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
442                                                                      uv_dx, uv_dy, stride2, rounding);
443                            interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
444                                                                      uv_dx, uv_dy, stride2, rounding);
445          }          }
446                    stop_comp_timer();
447    
448            } else {        /* MODE_INTER4V */
449                    int sum;
450    
451                    if(dec->quarterpel)
452                            sum = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
453                    else
454                            sum = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
455    
456                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
457    
458                    if(dec->quarterpel)
459                            sum = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
460                    else
461                            sum = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
462    
463                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
464    
465          start_timer();          start_timer();
466                    if (reduced_resolution)
467                    {
468                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
469                                                                      mv[0].x, mv[0].y, stride,  rounding);
470                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
471                                                                      mv[1].x, mv[1].y, stride,  rounding);
472                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
473                                                                      mv[2].x, mv[2].y, stride,  rounding);
474                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
475                                                                      mv[3].x, mv[3].y, stride,  rounding);
476                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
477                                                                      uv_dx, uv_dy, stride2, rounding);
478                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
479                                                                      uv_dx, uv_dy, stride2, rounding);
480    
481                            /* set_block(pY_Cur, stride, 32, 32, 127); */
482                    }
483                    else
484                    {
485                            if(dec->quarterpel) {
486                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
487                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
488                                                                                      mv[0].x, mv[0].y, stride,  rounding);
489                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
490                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
491                                                                                      mv[1].x, mv[1].y, stride,  rounding);
492                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
493                                                                                      dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
494                                                                                      mv[2].x, mv[2].y, stride,  rounding);
495                                    interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
496                                                                                      dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
497                                                                                      mv[3].x, mv[3].y, stride,  rounding);
498                            }
499                            else {
500          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos,
501                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride,  rounding);                                                                            mv[0].x, mv[0].y, stride,  rounding);
502          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos,
503                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride,  rounding);                                                                            mv[1].x, mv[1].y, stride,  rounding);
504          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8,
505                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride,  rounding);                                                                            mv[2].x, mv[2].y, stride,  rounding);
506          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,          interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8,
507                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride,  rounding);                                                                            mv[3].x, mv[3].y, stride,  rounding);
508                            }
509    
510          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos,
511                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
512          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,          interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos,
513                                                    uv_dx, uv_dy, stride2, rounding);                                                    uv_dx, uv_dy, stride2, rounding);
514                    }
515          stop_comp_timer();          stop_comp_timer();
516            }
517    
518          for (i = 0; i < 6; i++) {          for (i = 0; i < 6; i++) {
519                    int direction = dec->alternate_vertical_scan ? 2 : 0;
520    
521                  if (cbp & (1 << (5 - i)))       /* coded */                  if (cbp & (1 << (5 - i)))       /* coded */
522                  {                  {
523                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */                          memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
524    
525                          start_timer();                          start_timer();
526                          get_inter_block(bs, &block[i * 64]);                          get_inter_block(bs, &block[i * 64], direction);
527                          stop_coding_timer();                          stop_coding_timer();
528    
529                          start_timer();                          start_timer();
# Line 401  Line 546 
546          }          }
547    
548          start_timer();          start_timer();
549            if (reduced_resolution)
550            {
551                    if (cbp & 32)
552                            add_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
553                    if (cbp & 16)
554                            add_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
555                    if (cbp & 8)
556                            add_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
557                    if (cbp & 4)
558                            add_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
559                    if (cbp & 2)
560                            add_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
561                    if (cbp & 1)
562                            add_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
563            }
564            else
565            {
566                    if (cbp & 32)
567                            transfer_16to8add(pY_Cur, &data[0 * 64], stride);
568                    if (cbp & 16)
569                            transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
570                    if (cbp & 8)
571                            transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
572                    if (cbp & 4)
573                            transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
574                    if (cbp & 2)
575                            transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
576                    if (cbp & 1)
577                            transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
578            }
579            stop_transfer_timer();
580    }
581    
582    static __inline int gmc_sanitize(int value, int quarterpel, int fcode)
583    {
584            int length = 1 << (fcode+4);
585    
586    /*      if (quarterpel) value *= 2; */
587    
588            if (value < -length)
589                    return -length;
590            else if (value >= length)
591                    return length-1;
592            else return value;
593    }
594    
595    
596    static void
597    decoder_mbgmc(DECODER * dec,
598                                    MACROBLOCK * const pMB,
599                                    const uint32_t x_pos,
600                                    const uint32_t y_pos,
601                                    const uint32_t fcode,
602                                    const uint32_t cbp,
603                                    Bitstream * bs,
604                                    const uint32_t quant,
605                                    const uint32_t rounding,
606                                    const int reduced_resolution)   /* no reduced res support */
607    {
608    
609            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
610            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
611    
612            const uint32_t stride = dec->edged_width;
613            const uint32_t stride2 = stride / 2;
614            const uint32_t next_block = stride * (reduced_resolution ? 16 : 8);
615            uint32_t i;
616            const uint32_t iQuant = pMB->quant;
617            uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
618            uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
619            uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
620    
621            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
622    
623            start_timer();
624    
625    /* this is where the calculations are done */
626    
627            {
628                    pMB->amv = generate_GMCimageMB(&dec->gmc_data, &dec->refn[0], x_pos, y_pos,
629                                            stride, stride2, dec->quarterpel, rounding, &dec->cur);
630    
631                    pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
632                    pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
633            }
634            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
635    
636    /*
637            transfer16x16_copy(pY_Cur, dec->gmc.y + (y_pos << 4)*stride + (x_pos  << 4), stride);
638            transfer8x8_copy(pU_Cur, dec->gmc.u + (y_pos << 3)*stride2 + (x_pos  << 3), stride2);
639            transfer8x8_copy(pV_Cur, dec->gmc.v + (y_pos << 3)*stride2 + (x_pos << 3), stride2);
640    */
641    
642    
643            stop_transfer_timer();
644    
645            if (!cbp) return;
646    
647            for (i = 0; i < 6; i++) {
648                    int direction = dec->alternate_vertical_scan ? 2 : 0;
649    
650                    if (cbp & (1 << (5 - i)))       /* coded */
651                    {
652                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
653    
654                            start_timer();
655                            get_inter_block(bs, &block[i * 64], direction);
656                            stop_coding_timer();
657    
658                            start_timer();
659                            if (dec->quant_type == 0) {
660                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
661                            } else {
662                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
663                            }
664                            stop_iquant_timer();
665    
666                            start_timer();
667                            idct(&data[i * 64]);
668                            stop_idct_timer();
669                    }
670            }
671    
672    /* interlace + GMC is this possible ??? */
673    /*
674      if (dec->interlacing && pMB->field_dct) {
675              next_block = stride;
676              stride *= 2;
677      }
678    */
679            start_timer();
680          if (cbp & 32)          if (cbp & 32)
681                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);                  transfer_16to8add(pY_Cur, &data[0 * 64], stride);
682          if (cbp & 16)          if (cbp & 16)
# Line 420  Line 696 
696  void  void
697  decoder_iframe(DECODER * dec,  decoder_iframe(DECODER * dec,
698                             Bitstream * bs,                             Bitstream * bs,
699                               int reduced_resolution,
700                             int quant,                             int quant,
701                             int intra_dc_threshold)                             int intra_dc_threshold)
702  {  {
703          uint32_t bound;          uint32_t bound;
704          uint32_t x, y;          uint32_t x, y;
705            uint32_t mb_width = dec->mb_width;
706            uint32_t mb_height = dec->mb_height;
707    
708            if (reduced_resolution)
709            {
710                    mb_width = (dec->width + 31) / 32;
711                    mb_height = (dec->height + 31) / 32;
712            }
713    
714          bound = 0;          bound = 0;
715    
716          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
717                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
718                          MACROBLOCK *mb;                          MACROBLOCK *mb;
719                          uint32_t mcbpc;                          uint32_t mcbpc;
720                          uint32_t cbpc;                          uint32_t cbpc;
# Line 442  Line 727 
727    
728                          if (check_resync_marker(bs, 0))                          if (check_resync_marker(bs, 0))
729                          {                          {
730                                  bound = read_video_packet_header(bs, 0, &quant);                                  bound = read_video_packet_header(bs, dec, 0,
731                                  x = bound % dec->mb_width;                                                          &quant, NULL, NULL, &intra_dc_threshold);
732                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
733                                    y = bound / mb_width;
734                          }                          }
735                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
736    
# Line 475  Line 761 
761    
762                          if (dec->interlacing) {                          if (dec->interlacing) {
763                                  mb->field_dct = BitstreamGetBit(bs);                                  mb->field_dct = BitstreamGetBit(bs);
764                                  DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct);                                  DPRINTF(DPRINTF_MB,"deci: field_dct: %i", mb->field_dct);
765                          }                          }
766    
767                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
768                                                          intra_dc_threshold, bound);                                                          intra_dc_threshold, bound, reduced_resolution);
769    
770                  }                  }
771                  if(dec->out_frm)                  if(dec->out_frm)
772                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width);                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
   
773          }          }
774    
775  }  }
# Line 495  Line 781 
781                                    int x,                                    int x,
782                                    int y,                                    int y,
783                                    int k,                                    int k,
784                                    VECTOR * mv,                                    VECTOR * ret_mv,
785                                    int fcode,                                    int fcode,
786                                    const int bound)                                    const int bound)
787  {  {
# Line 506  Line 792 
792          int range = (64 * scale_fac);          int range = (64 * scale_fac);
793    
794          VECTOR pmv;          VECTOR pmv;
795          int mv_x, mv_y;          VECTOR mv;
796    
797          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);          pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
798    
799          mv_x = get_mv(bs, fcode);          mv.x = get_mv(bs, fcode);
800          mv_y = get_mv(bs, fcode);          mv.y = get_mv(bs, fcode);
801    
802          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);
803    
804          mv_x += pmv.x;          mv.x += pmv.x;
805          mv_y += pmv.y;          mv.y += pmv.y;
806    
807          if (mv_x < low) {          if (mv.x < low) {
808                  mv_x += range;                  mv.x += range;
809          } else if (mv_x > high) {          } else if (mv.x > high) {
810                  mv_x -= range;                  mv.x -= range;
811          }          }
812    
813          if (mv_y < low) {          if (mv.y < low) {
814                  mv_y += range;                  mv.y += range;
815          } else if (mv_y > high) {          } else if (mv.y > high) {
816                  mv_y -= range;                  mv.y -= range;
817          }          }
818    
819          mv->x = mv_x;          ret_mv->x = mv.x;
820          mv->y = mv_y;          ret_mv->y = mv.y;
   
821  }  }
822    
823    
824    
825    
826    
827    /* for P_VOP set gmc_warp to NULL */
828  void  void
829  decoder_pframe(DECODER * dec,  decoder_pframe(DECODER * dec,
830                             Bitstream * bs,                             Bitstream * bs,
831                             int rounding,                             int rounding,
832                               int reduced_resolution,
833                             int quant,                             int quant,
834                             int fcode,                             int fcode,
835                             int intra_dc_threshold)                             int intra_dc_threshold,
836                               const WARPPOINTS *const gmc_warp)
837  {  {
838    
839          uint32_t x, y;          uint32_t x, y;
840          uint32_t bound;          uint32_t bound;
841          int cp_mb, st_mb;          int cp_mb, st_mb;
842            uint32_t mb_width = dec->mb_width;
843            uint32_t mb_height = dec->mb_height;
844    
845            if (reduced_resolution)
846            {
847                    mb_width = (dec->width + 31) / 32;
848                    mb_height = (dec->height + 31) / 32;
849            }
850    
851          start_timer();          start_timer();
852          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,          image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
853                                     dec->width, dec->height);                                     dec->width, dec->height);
854          stop_edges_timer();          stop_edges_timer();
855    
856            if (gmc_warp)
857            {
858    
859                    /* accuracy:  0==1/2, 1=1/4, 2=1/8, 3=1/16 */
860                    if ( (dec->sprite_warping_accuracy != 3) || (dec->sprite_warping_points != 2) )
861                    {
862                            fprintf(stderr,"Wrong GMC parameters acc=%d(-> 1/%d), %d!!!\n",
863                                    dec->sprite_warping_accuracy,(2<<dec->sprite_warping_accuracy),
864                                    dec->sprite_warping_points);
865                    }
866    
867                    generate_GMCparameters( dec->sprite_warping_points,
868                                    (2 << dec->sprite_warping_accuracy), gmc_warp,
869                                    dec->width, dec->height, &dec->gmc_data);
870    
871    /* image warping is done block-based  in decoder_mbgmc(), now */
872    /*
873            generate_GMCimage(&dec->gmc_data, &dec->refn[0],
874                                            mb_width, mb_height,
875                                            dec->edged_width, dec->edged_width/2,
876                                            fcode, dec->quarterpel, 0,
877                                            rounding, dec->mbs, &dec->gmc);
878    */
879            }
880    
881          bound = 0;          bound = 0;
882    
883          for (y = 0; y < dec->mb_height; y++) {          for (y = 0; y < mb_height; y++) {
884                  cp_mb = st_mb = 0;                  cp_mb = st_mb = 0;
885                  for (x = 0; x < dec->mb_width; x++) {                  for (x = 0; x < mb_width; x++) {
886                          MACROBLOCK *mb;                          MACROBLOCK *mb;
887    
888                          /* skip stuffing */                          /* skip stuffing */
# Line 567  Line 891 
891    
892                          if (check_resync_marker(bs, fcode - 1))                          if (check_resync_marker(bs, fcode - 1))
893                          {                          {
894                                  bound = read_video_packet_header(bs, fcode - 1, &quant);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
895                                  x = bound % dec->mb_width;                                          &quant, &fcode, NULL, &intra_dc_threshold);
896                                  y = bound / dec->mb_width;                                  x = bound % mb_width;
897                                    y = bound / mb_width;
898                          }                          }
899                          mb = &dec->mbs[y * dec->mb_width + x];                          mb = &dec->mbs[y * dec->mb_width + x];
900    
901                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));                          DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32));
902    
903                          /*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 */
904                          if (!(BitstreamGetBit(bs)))     /* not_coded */                          if (!(BitstreamGetBit(bs)))     /* block _is_ coded */
905                          {                          {
906                                  uint32_t mcbpc;                                  uint32_t mcbpc;
907                                  uint32_t cbpc;                                  uint32_t cbpc;
# Line 584  Line 909 
909                                  uint32_t cbpy;                                  uint32_t cbpy;
910                                  uint32_t cbp;                                  uint32_t cbp;
911                                  uint32_t intra;                                  uint32_t intra;
912                                    int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
913    
914                                  cp_mb++;                                  cp_mb++;
915                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
# Line 600  Line 926 
926                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
927                                  }                                  }
928    
929                                    if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
930                                    {
931                                            mcsel = BitstreamGetBit(bs);
932                                    }
933    
934                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
935                                  DPRINTF(DPRINTF_MB, "cbpy %i", cbpy);                                  DPRINTF(DPRINTF_MB, "cbpy %i  mcsel %i ", cbpy,mcsel);
936    
937                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
938    
# Line 621  Line 952 
952                                  if (dec->interlacing) {                                  if (dec->interlacing) {
953                                          if (cbp || intra) {                                          if (cbp || intra) {
954                                                  mb->field_dct = BitstreamGetBit(bs);                                                  mb->field_dct = BitstreamGetBit(bs);
955                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct);                                                  DPRINTF(DPRINTF_MB,"decp: field_dct: %i", mb->field_dct);
956                                          }                                          }
957    
958                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
959                                                  mb->field_pred = BitstreamGetBit(bs);                                                  mb->field_pred = BitstreamGetBit(bs);
960                                                  DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred);                                                  DPRINTF(DPRINTF_MB, "decp: field_pred: %i", mb->field_pred);
961    
962                                                  if (mb->field_pred) {                                                  if (mb->field_pred) {
963                                                          mb->field_for_top = BitstreamGetBit(bs);                                                          mb->field_for_top = BitstreamGetBit(bs);
964                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_top: %i", mb->field_for_top);
965                                                          mb->field_for_bot = BitstreamGetBit(bs);                                                          mb->field_for_bot = BitstreamGetBit(bs);
966                                                          DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot);                                                          DPRINTF(DPRINTF_MB,"decp: field_for_bot: %i", mb->field_for_bot);
967                                                  }                                                  }
968                                          }                                          }
969                                  }                                  }
970    
971                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {                                  if (mcsel) {
972                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, quant,
973                                                                    rounding, reduced_resolution);
974                                            continue;
975    
976                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
977    
978                                          if (dec->interlacing && mb->field_pred) {                                          if (dec->interlacing && mb->field_pred) {
979                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
980                                                                                    fcode, bound);                                                                                    fcode, bound);
# Line 646  Line 983 
983                                          } else {                                          } else {
984                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],                                                  get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0],
985                                                                                    fcode, bound);                                                                                    fcode, bound);
986                                                  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;  
987                                          }                                          }
988                                  } else if (mb->mode == MODE_INTER4V ) {                                  } else if (mb->mode == MODE_INTER4V ) {
989    
# Line 664  Line 998 
998                                          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 =
999                                                  0;                                                  0;
1000                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
1001                                                                          intra_dc_threshold, bound);                                                                          intra_dc_threshold, bound, reduced_resolution);
1002                                          continue;                                          continue;
1003                                  }                                  }
1004    
1005                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant,                                  decoder_mbinter(dec, mb, x, y, fcode, cbp, bs, quant,
1006                                                                  rounding);                                                                  rounding, reduced_resolution);
                         } else                          /* not coded */  
                         {  
                                 DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y);  
   
                                 mb->mode = MODE_NOT_CODED;  
                                 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;  
                                 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;  
1007    
1008                                  /* copy macroblock directly from ref to cur */                          }
1009                            else if (gmc_warp)      /* a not coded S(GMC)-VOP macroblock */
1010                            {
1011                                    mb->mode = MODE_NOT_CODED_GMC;
1012    
1013                                  start_timer();                                  start_timer();
1014    
1015                                  transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +                                  decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, quant,
1016                                                                   (16 * x),                                                                  rounding, reduced_resolution);
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (16 * x), dec->edged_width);  
   
                                 transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width +  
                                                                  (16 * x + 8),  
                                                                  dec->refn[0].y + (16 * y) * dec->edged_width +  
                                                                  (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),  
                                                                  dec->edged_width / 2);  
1017    
                                 transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 +  
                                                                  (8 * x),  
                                                                  dec->refn[0].v +  
                                                                  (8 * y) * dec->edged_width / 2 + (8 * x),  
                                                                  dec->edged_width / 2);  
1018                                  stop_transfer_timer();                                  stop_transfer_timer();
1019    
1020                                    if(dec->out_frm && cp_mb > 0) {
1021                                      output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1022                                      cp_mb = 0;
1023                                    }
1024                                    st_mb = x+1;
1025                            }
1026                            else    /* not coded P_VOP macroblock */
1027                            {
1028                                    mb->mode = MODE_NOT_CODED;
1029    
1030                                    mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
1031                                    mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
1032                                    /* copy macroblock directly from ref to cur */
1033    
1034                                    start_timer();
1035    
1036                                    if (reduced_resolution)
1037                                    {
1038                                            transfer32x32_copy(dec->cur.y + (32*y)*dec->edged_width + (32*x),
1039                                                                             dec->refn[0].y + (32*y)*dec->edged_width + (32*x),
1040                                                                             dec->edged_width);
1041    
1042                                            transfer16x16_copy(dec->cur.u + (16*y)*dec->edged_width/2 + (16*x),
1043                                                                            dec->refn[0].u + (16*y)*dec->edged_width/2 + (16*x),
1044                                                                            dec->edged_width/2);
1045    
1046                                            transfer16x16_copy(dec->cur.v + (16*y)*dec->edged_width/2 + (16*x),
1047                                                                             dec->refn[0].v + (16*y)*dec->edged_width/2 + (16*x),
1048                                                                             dec->edged_width/2);
1049                                    }
1050                                    else
1051                                    {
1052                                            transfer16x16_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
1053                                                                             dec->refn[0].y + (16*y)*dec->edged_width + (16*x),
1054                                                                             dec->edged_width);
1055    
1056                                            transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
1057                                                                            dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x),
1058                                                                            dec->edged_width/2);
1059    
1060                                            transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
1061                                                                             dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x),
1062                                                                             dec->edged_width/2);
1063                                    }
1064    
1065                                    stop_transfer_timer();
1066    
1067                                  if(dec->out_frm && cp_mb > 0) {                                  if(dec->out_frm && cp_mb > 0) {
1068                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);                                    output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
1069                                    cp_mb = 0;                                    cp_mb = 0;
# Line 728  Line 1076 
1076          }          }
1077  }  }
1078    
1079    
1080    /* decode B-frame motion vector */
1081    void
1082    get_b_motion_vector(DECODER * dec,
1083                                            Bitstream * bs,
1084                                            int x,
1085                                            int y,
1086                                            VECTOR * mv,
1087                                            int fcode,
1088                                            const VECTOR pmv)
1089    {
1090            int scale_fac = 1 << (fcode - 1);
1091            int high = (32 * scale_fac) - 1;
1092            int low = ((-32) * scale_fac);
1093            int range = (64 * scale_fac);
1094    
1095            int mv_x, mv_y;
1096            int pmv_x, pmv_y;
1097    
1098            pmv_x = pmv.x;
1099            pmv_y = pmv.y;
1100    
1101            mv_x = get_mv(bs, fcode);
1102            mv_y = get_mv(bs, fcode);
1103    
1104            mv_x += pmv_x;
1105            mv_y += pmv_y;
1106    
1107            if (mv_x < low) {
1108                    mv_x += range;
1109            } else if (mv_x > high) {
1110                    mv_x -= range;
1111            }
1112    
1113            if (mv_y < low) {
1114                    mv_y += range;
1115            } else if (mv_y > high) {
1116                    mv_y -= range;
1117            }
1118    
1119            mv->x = mv_x;
1120            mv->y = mv_y;
1121    }
1122    
1123    
1124    /* decode an B-frame forward & backward inter macroblock */
1125    void
1126    decoder_bf_mbinter(DECODER * dec,
1127                                       const MACROBLOCK * pMB,
1128                                       const uint32_t x_pos,
1129                                       const uint32_t y_pos,
1130                                       const uint32_t cbp,
1131                                       Bitstream * bs,
1132                                       const uint32_t quant,
1133                                       const uint8_t ref)
1134    {
1135    
1136            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1137            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1138    
1139            uint32_t stride = dec->edged_width;
1140            uint32_t stride2 = stride / 2;
1141            uint32_t next_block = stride * 8;
1142            uint32_t i;
1143            uint32_t iQuant = pMB->quant;
1144            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1145            int uv_dx, uv_dy;
1146    
1147            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1148            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1149            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1150    
1151    
1152            if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1153                    uv_dx = pMB->mvs[0].x;
1154                    uv_dy = pMB->mvs[0].y;
1155    
1156                    if (dec->quarterpel)
1157                    {
1158                            uv_dx /= 2;
1159                            uv_dy /= 2;
1160                    }
1161    
1162                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1163                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1164            } else {
1165                    int sum;
1166    
1167                    if(dec->quarterpel)
1168                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1169                    else
1170                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1171    
1172                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1173    
1174                    if(dec->quarterpel)
1175                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1176                    else
1177                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1178    
1179                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1180            }
1181    
1182            start_timer();
1183            if(dec->quarterpel) {
1184                    interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
1185                                                                        dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1186                                                                        pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1187            }
1188            else {
1189                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
1190                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1191                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos,
1192                                                          pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1193                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8,
1194                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1195                    interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8,
1196                                                              pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1197            }
1198    
1199            interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
1200                                                      uv_dx, uv_dy, stride2, 0);
1201            interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
1202                                                      uv_dx, uv_dy, stride2, 0);
1203            stop_comp_timer();
1204    
1205            for (i = 0; i < 6; i++) {
1206                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1207    
1208                    if (cbp & (1 << (5 - i)))       /* coded */
1209                    {
1210                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1211    
1212                            start_timer();
1213                            get_inter_block(bs, &block[i * 64], direction);
1214                            stop_coding_timer();
1215    
1216                            start_timer();
1217                            if (dec->quant_type == 0) {
1218                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1219                            } else {
1220                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1221                            }
1222                            stop_iquant_timer();
1223    
1224                            start_timer();
1225                            idct(&data[i * 64]);
1226                            stop_idct_timer();
1227                    }
1228            }
1229    
1230            if (dec->interlacing && pMB->field_dct) {
1231                    next_block = stride;
1232                    stride *= 2;
1233            }
1234    
1235            start_timer();
1236            if (cbp & 32)
1237                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1238            if (cbp & 16)
1239                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1240            if (cbp & 8)
1241                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1242            if (cbp & 4)
1243                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1244            if (cbp & 2)
1245                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1246            if (cbp & 1)
1247                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1248            stop_transfer_timer();
1249    }
1250    
1251    /* decode an B-frame direct &  inter macroblock */
1252    void
1253    decoder_bf_interpolate_mbinter(DECODER * dec,
1254                                                               IMAGE forward,
1255                                                               IMAGE backward,
1256                                                               const MACROBLOCK * pMB,
1257                                                               const uint32_t x_pos,
1258                                                               const uint32_t y_pos,
1259                                                               Bitstream * bs)
1260    {
1261    
1262            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
1263            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
1264    
1265            uint32_t stride = dec->edged_width;
1266            uint32_t stride2 = stride / 2;
1267            uint32_t next_block = stride * 8;
1268            uint32_t iQuant = pMB->quant;
1269            int uv_dx, uv_dy;
1270            int b_uv_dx, b_uv_dy;
1271            uint32_t i;
1272            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1273        const uint32_t cbp = pMB->cbp;
1274    
1275            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1276            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1277            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1278    
1279    
1280            if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) {
1281                    uv_dx = pMB->mvs[0].x;
1282                    uv_dy = pMB->mvs[0].y;
1283    
1284                    b_uv_dx = pMB->b_mvs[0].x;
1285                    b_uv_dy = pMB->b_mvs[0].y;
1286    
1287                    if (dec->quarterpel)
1288                    {
1289                            uv_dx /= 2;
1290                            uv_dy /= 2;
1291    
1292                            b_uv_dx /= 2;
1293                            b_uv_dy /= 2;
1294                    }
1295    
1296                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1297                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1298    
1299                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1300                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1301            } else {
1302                    int sum;
1303    
1304                    if(dec->quarterpel)
1305                            sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2);
1306                    else
1307                            sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1308    
1309                    uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1310    
1311                    if(dec->quarterpel)
1312                            sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2);
1313                    else
1314                            sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1315    
1316                    uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1317    
1318    
1319                    if(dec->quarterpel)
1320                            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);
1321                    else
1322                            sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1323    
1324                    b_uv_dx = (sum >> 3) + roundtab_76[sum & 0xf];
1325    
1326                    if(dec->quarterpel)
1327                            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);
1328                    else
1329                            sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1330    
1331                    b_uv_dy = (sum >> 3) + roundtab_76[sum & 0xf];
1332            }
1333    
1334    
1335            start_timer();
1336            if(dec->quarterpel) {
1337                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1338                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1339                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1340                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1341                    else {
1342                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1343                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1344                                                                                pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1345                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1346                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1347                                                                                pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1348                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1349                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1350                                                                                pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1351                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1352                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1353                                                                                pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1354                    }
1355            }
1356            else {
1357                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1358                                                              pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1359                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1360                                                              pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1361                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1362                                                              pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1363                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8,
1364                                                              16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride,
1365                                                              0);
1366            }
1367    
1368            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1369                                                      uv_dy, stride2, 0);
1370            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1371                                                      uv_dy, stride2, 0);
1372    
1373    
1374            if(dec->quarterpel) {
1375                    if((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q))
1376                            interpolate16x16_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1377                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1378                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1379                    else {
1380                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1381                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1382                                                                                pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1383                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1384                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1385                                                                                pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1386                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1387                                                                                dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1388                                                                                pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1389                            interpolate8x8_quarterpel(dec->tmp.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1390                                                                                dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1391                                                                                pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1392                    }
1393            }
1394            else {
1395                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos, 16 * y_pos,
1396                                                              pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1397                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1398                                                              16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride,
1399                                                              0);
1400                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos,
1401                                                              16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y,
1402                                                              stride, 0);
1403                    interpolate8x8_switch(dec->tmp.y, backward.y, 16 * x_pos + 8,
1404                                                              16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y,
1405                                                              stride, 0);
1406            }
1407    
1408            interpolate8x8_switch(dec->tmp.u, backward.u, 8 * x_pos, 8 * y_pos,
1409                                                      b_uv_dx, b_uv_dy, stride2, 0);
1410            interpolate8x8_switch(dec->tmp.v, backward.v, 8 * x_pos, 8 * y_pos,
1411                                                      b_uv_dx, b_uv_dy, stride2, 0);
1412    
1413            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1414                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos,
1415                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos,
1416                                                    stride, 1, 8);
1417    
1418            interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1419                                                    dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1420                                                    dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8,
1421                                                    stride, 1, 8);
1422    
1423            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1424                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1425                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos,
1426                                                    stride, 1, 8);
1427    
1428            interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1429                                                    dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1430                                                    dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8,
1431                                                    stride, 1, 8);
1432    
1433            interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1434                                                    dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos,
1435                                                    dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos,
1436                                                    stride2, 1, 8);
1437    
1438            interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1439                                                    dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos,
1440                                                    dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos,
1441                                                    stride2, 1, 8);
1442    
1443            stop_comp_timer();
1444    
1445            for (i = 0; i < 6; i++) {
1446                    int direction = dec->alternate_vertical_scan ? 2 : 0;
1447    
1448                    if (cbp & (1 << (5 - i)))       /* coded */
1449                    {
1450                            memset(&block[i * 64], 0, 64 * sizeof(int16_t));        /* clear */
1451    
1452                            start_timer();
1453                            get_inter_block(bs, &block[i * 64], direction);
1454                            stop_coding_timer();
1455    
1456                            start_timer();
1457                            if (dec->quant_type == 0) {
1458                                    dequant_inter(&data[i * 64], &block[i * 64], iQuant);
1459                            } else {
1460                                    dequant4_inter(&data[i * 64], &block[i * 64], iQuant);
1461                            }
1462                            stop_iquant_timer();
1463    
1464                            start_timer();
1465                            idct(&data[i * 64]);
1466                            stop_idct_timer();
1467                    }
1468            }
1469    
1470            if (dec->interlacing && pMB->field_dct) {
1471                    next_block = stride;
1472                    stride *= 2;
1473            }
1474    
1475            start_timer();
1476            if (cbp & 32)
1477                    transfer_16to8add(pY_Cur, &data[0 * 64], stride);
1478            if (cbp & 16)
1479                    transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride);
1480            if (cbp & 8)
1481                    transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride);
1482            if (cbp & 4)
1483                    transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride);
1484            if (cbp & 2)
1485                    transfer_16to8add(pU_Cur, &data[4 * 64], stride2);
1486            if (cbp & 1)
1487                    transfer_16to8add(pV_Cur, &data[5 * 64], stride2);
1488            stop_transfer_timer();
1489    }
1490    
1491    
1492    /* for decode B-frame dbquant */
1493    int32_t __inline
1494    get_dbquant(Bitstream * bs)
1495    {
1496            if (!BitstreamGetBit(bs))      /*  '0' */
1497                    return (0);
1498            else if (!BitstreamGetBit(bs)) /* '10' */
1499                    return (-2);
1500            else                           /* '11' */
1501                    return (2);
1502    }
1503    
1504    /*
1505     * For decode B-frame mb_type
1506     * bit   ret_value
1507     * 1        0
1508     * 01       1
1509     * 001      2
1510     * 0001     3
1511     */
1512    int32_t __inline
1513    get_mbtype(Bitstream * bs)
1514    {
1515            int32_t mb_type;
1516    
1517            for (mb_type = 0; mb_type <= 3; mb_type++) {
1518                    if (BitstreamGetBit(bs))
1519                            break;
1520            }
1521    
1522            if (mb_type <= 3)
1523                    return (mb_type);
1524            else
1525                    return (-1);
1526    }
1527    
1528    void
1529    decoder_bframe(DECODER * dec,
1530                               Bitstream * bs,
1531                               int quant,
1532                               int fcode_forward,
1533                               int fcode_backward)
1534    {
1535            uint32_t x, y;
1536            VECTOR mv;
1537            const VECTOR zeromv = {0,0};
1538    #ifdef BFRAMES_DEC_DEBUG
1539            FILE *fp;
1540            static char first=0;
1541    #define BFRAME_DEBUG    if (!first && fp){ \
1542                    fprintf(fp,"Y=%3d   X=%3d   MB=%2d   CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \
1543            }
1544    #endif
1545    
1546            start_timer();
1547            image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1548                                       dec->width, dec->height);
1549            image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1550                                       dec->width, dec->height);
1551            stop_edges_timer();
1552    
1553    #ifdef BFRAMES_DEC_DEBUG
1554            if (!first){
1555                    fp=fopen("C:\\XVIDDBG.TXT","w");
1556            }
1557    #endif
1558    
1559            for (y = 0; y < dec->mb_height; y++) {
1560                    /* Initialize Pred Motion Vector */
1561                    dec->p_fmv = dec->p_bmv = zeromv;
1562                    for (x = 0; x < dec->mb_width; x++) {
1563                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1564                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1565    
1566                            mv =
1567                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1568                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1569    
1570                            /*
1571                             * skip if the co-located P_VOP macroblock is not coded
1572                             * if not codec in co-located S_VOP macroblock is _not_
1573                             * automatically skipped
1574                             */
1575    
1576                            if (last_mb->mode == MODE_NOT_CODED) {
1577                                    /* DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */
1578                                    mb->cbp = 0;
1579    #ifdef BFRAMES_DEC_DEBUG
1580                                    mb->mb_type = MODE_NOT_CODED;
1581            BFRAME_DEBUG
1582    #endif
1583                                    mb->mb_type = MODE_FORWARD;
1584                                    mb->quant = last_mb->quant;
1585                                    /*
1586                                      mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
1587                                      mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
1588                                    */
1589    
1590                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1);
1591                                    continue;
1592                            }
1593    
1594                            if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1595                                    const uint8_t modb2 = BitstreamGetBit(bs);
1596    
1597                                    mb->mb_type = get_mbtype(bs);
1598    
1599                                    if (!modb2) {   /* modb=='00' */
1600                                            mb->cbp = BitstreamGetBits(bs, 6);
1601                                    } else {
1602                                            mb->cbp = 0;
1603                                    }
1604                                    if (mb->mb_type && mb->cbp) {
1605                                            quant += get_dbquant(bs);
1606    
1607                                            if (quant > 31) {
1608                                                    quant = 31;
1609                                            } else if (quant < 1) {
1610                                                    quant = 1;
1611                                            }
1612                                    }
1613                            } else {
1614                                    mb->mb_type = MODE_DIRECT_NONE_MV;
1615                                    mb->cbp = 0;
1616                            }
1617    
1618                            mb->quant = quant;
1619                            mb->mode = MODE_INTER4V;
1620                            /* DEBUG1("Switch bm_type=",mb->mb_type); */
1621    
1622    #ifdef BFRAMES_DEC_DEBUG
1623            BFRAME_DEBUG
1624    #endif
1625    
1626                            switch (mb->mb_type) {
1627                            case MODE_DIRECT:
1628                                    get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv);
1629    
1630                            case MODE_DIRECT_NONE_MV:
1631                                    {
1632                                            const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp;
1633                                            int i;
1634    
1635                                            for (i = 0; i < 4; i++) {
1636                                                    mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x)
1637                                                                          / TRD + mv.x);
1638                                                    mb->b_mvs[i].x = (int32_t) ((mv.x == 0)
1639                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].x)
1640                                                                                      / TRD
1641                                                                                    : mb->mvs[i].x - last_mb->mvs[i].x);
1642                                                    mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y)
1643                                                                          / TRD + mv.y);
1644                                                    mb->b_mvs[i].y = (int32_t) ((mv.y == 0)
1645                                                                                    ? ((TRB - TRD) * last_mb->mvs[i].y)
1646                                                                                      / TRD
1647                                                                                : mb->mvs[i].y - last_mb->mvs[i].y);
1648                                            }
1649                                            /* DEBUG("B-frame Direct!\n"); */
1650                                    }
1651                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1652                                                                                               mb, x, y, bs);
1653                                    break;
1654    
1655                            case MODE_INTERPOLATE:
1656                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1657                                                                            dec->p_fmv);
1658                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1659    
1660                                    get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0],
1661                                                                            fcode_backward, dec->p_bmv);
1662                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] =
1663                                            mb->b_mvs[3] = mb->b_mvs[0];
1664    
1665                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1666                                                                                               mb, x, y, bs);
1667                                    /* DEBUG("B-frame Bidir!\n"); */
1668                                    break;
1669    
1670                            case MODE_BACKWARD:
1671                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward,
1672                                                                            dec->p_bmv);
1673                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1674    
1675                                    mb->mode = MODE_INTER;
1676                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0);
1677                                    /* DEBUG("B-frame Backward!\n"); */
1678                                    break;
1679    
1680                            case MODE_FORWARD:
1681                                    get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward,
1682                                                                            dec->p_fmv);
1683                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1684    
1685                                    mb->mode = MODE_INTER;
1686                                    decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1);
1687                                    /* DEBUG("B-frame Forward!\n"); */
1688                                    break;
1689    
1690                            default:
1691                                    DPRINTF(DPRINTF_ERROR,"Not support B-frame mb_type = %i", mb->mb_type);
1692                            }
1693                    } /* End of for */
1694            }
1695    
1696    #ifdef BFRAMES_DEC_DEBUG
1697            if (!first){
1698                    first=1;
1699                    if (fp)
1700                            fclose(fp);
1701            }
1702    #endif
1703    }
1704    
1705  /* swap two MACROBLOCK array */  /* swap two MACROBLOCK array */
1706  void  void
1707  mb_swap(MACROBLOCK ** mb1,  mb_swap(MACROBLOCK ** mb1,
# Line 739  Line 1713 
1713          *mb2 = temp;          *mb2 = temp;
1714  }  }
1715    
1716    
1717    /* perform post processing if necessary, and output the image */
1718    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1719                                            const XVID_DEC_FRAME * frame, int pp_disable)
1720    {
1721    
1722            if ((frame->general & (XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV)) && !pp_disable)   /* post process */
1723            {
1724                    /* note: image is stored to tmp */
1725                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1726                    image_deblock_rrv(&dec->tmp, dec->edged_width,
1727                                                    mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1728                                                    8, frame->general);
1729                    img = &dec->tmp;
1730            }
1731    
1732            image_output(img, dec->width, dec->height,
1733                                     dec->edged_width, frame->image, frame->stride,
1734                                     frame->colorspace, dec->interlacing);
1735    }
1736    
1737    
1738  int  int
1739  decoder_decode(DECODER * dec,  decoder_decode(DECODER * dec,
1740                             XVID_DEC_FRAME * frame)                             XVID_DEC_FRAME * frame, XVID_DEC_STATS * stats)
1741  {  {
1742    
1743          Bitstream bs;          Bitstream bs;
1744          uint32_t rounding;          uint32_t rounding;
1745            uint32_t reduced_resolution;
1746          uint32_t quant;          uint32_t quant;
1747          uint32_t fcode_forward;          uint32_t fcode_forward;
1748          uint32_t fcode_backward;          uint32_t fcode_backward;
1749          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1750            WARPPOINTS gmc_warp;
1751          uint32_t vop_type;          uint32_t vop_type;
1752            int success = 0;
1753            int output = 0;
1754            int seen_something = 0;
1755    
1756          start_global_timer();          start_global_timer();
1757    
1758            dec->low_delay_default = (frame->general & XVID_DEC_LOWDELAY);
1759          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;          dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL;
1760    
1761            if ((frame->general & XVID_DEC_DISCONTINUITY))
1762                    dec->frames = 0;
1763    
1764            if (frame->length < 0)  /* decoder flush */
1765            {
1766                    /* if  not decoding "low_delay/packed", and this isn't low_delay and
1767                        we have a reference frame, then outout the reference frame */
1768                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0)
1769                    {
1770                            decoder_output(dec, &dec->refn[0], dec->mbs, frame, dec->last_reduced_resolution);
1771                            output = 1;
1772                    }
1773    
1774                    frame->length = 0;
1775                    if (stats)
1776                    {
1777                            stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1778                            stats->data.vop.time_base = (int)dec->time_base;
1779                            stats->data.vop.time_increment = 0;     /* XXX: todo */
1780                    }
1781    
1782                    emms();
1783    
1784                    stop_global_timer();
1785                    return XVID_ERR_OK;
1786            }
1787    
1788          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1789    
1790          /* add by chenm001 <chenm001@163.com> */          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1791          /* for support B-frame to reference last 2 frame */          if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1792          dec->frames++;          {
1793          vop_type =                  if (stats)
1794                  BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward,                          stats->notify = XVID_DEC_VOP;
1795                                                           &fcode_backward, &intra_dc_threshold);                  frame->length = 1;
1796                    image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1797                                             frame->image, frame->stride, frame->colorspace, dec->interlacing);
1798                    emms();
1799                    return XVID_ERR_OK;
1800            }
1801    
1802    repeat:
1803    
1804            vop_type =      BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1805                            &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1806    
1807            DPRINTF(DPRINTF_HEADER, "vop_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i",
1808                                                            vop_type,       dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1809    
1810            if (vop_type == - 1)
1811            {
1812                    if (success) goto done;
1813                    emms();
1814                    return XVID_ERR_FAIL;
1815            }
1816    
1817            if (vop_type == -2 || vop_type == -3)
1818            {
1819                    if (vop_type == -3)
1820                            decoder_resize(dec);
1821    
1822                    if (stats)
1823                    {
1824                            stats->notify = XVID_DEC_VOL;
1825                            stats->data.vol.general = 0;
1826                            if (dec->interlacing)
1827                                    stats->data.vol.general |= XVID_INTERLACING;
1828                            stats->data.vol.width = dec->width;
1829                            stats->data.vol.height = dec->height;
1830                            stats->data.vol.aspect_ratio = dec->aspect_ratio;
1831                            stats->data.vol.par_width = dec->par_width;
1832                            stats->data.vol.par_height = dec->par_height;
1833                            frame->length = BitstreamPos(&bs) / 8;
1834                            emms();
1835                            return XVID_ERR_OK;
1836                    }
1837                    goto repeat;
1838            }
1839    
1840          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 */
1841    
         switch (vop_type) {  
         case P_VOP:  
                 decoder_pframe(dec, &bs, rounding, quant, fcode_forward,  
                                            intra_dc_threshold);  
                 break;  
1842    
1843            /* packed_mode: special-N_VOP treament */
1844            if (dec->packed_mode && vop_type == N_VOP)
1845            {
1846                    if (dec->low_delay_default && dec->frames > 0)
1847                    {
1848                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1849                            output = 1;
1850                    }
1851                    /* ignore otherwise */
1852            }
1853            else if (vop_type != B_VOP)
1854            {
1855                    switch(vop_type)
1856                    {
1857          case I_VOP:          case I_VOP:
1858                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);                          decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1859                  break;                  break;
1860          case B_VOP:                  case P_VOP :
1861                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                          decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1862                                                    fcode_forward, intra_dc_threshold, NULL);
1863                            break;
1864                    case S_VOP :
1865                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1866                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1867                  break;                  break;
1868          case N_VOP:          case N_VOP:
                 /* when low_delay==0, N_VOP's should interpolate between the past and future frames */  
1869                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);                  image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1870                  break;                  break;
   
         default:  
                 return XVID_ERR_FAIL;  
1871          }          }
1872    
1873          frame->length = BitstreamPos(&bs) / 8;                  if (reduced_resolution)
1874                    {
1875                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1876                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1877                                    16, XVID_DEC_DEBLOCKY|XVID_DEC_DEBLOCKUV);
1878                    }
1879    
1880          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                  /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1881                                           frame->image, frame->stride, frame->colorspace);                  if (!(dec->low_delay_default && dec->packed_mode))
1882                    {
1883                            if (dec->low_delay)
1884                            {
1885                                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1886                                    output = 1;
1887                            }
1888                            else if (dec->frames > 0)       /* is the reference frame valid? */
1889                            {
1890                                    /* output the reference frame */
1891                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1892                                    output = 1;
1893                            }
1894                    }
1895    
         if (vop_type == I_VOP || vop_type == P_VOP) {  
1896                  image_swap(&dec->refn[0], &dec->refn[1]);                  image_swap(&dec->refn[0], &dec->refn[1]);
1897                  image_swap(&dec->cur, &dec->refn[0]);                  image_swap(&dec->cur, &dec->refn[0]);
   
                 /* swap MACROBLOCK */  
                 /* the Divx will not set the low_delay flage some times */  
                 /* so follow code will wrong to not swap at that time */  
                 /* this will broken bitstream! so I'm change it, */  
                 /* But that is not the best way! can anyone tell me how */  
                 /* to do another way? */  
                 /* 18-07-2002   MinChen<chenm001@163.com> */  
                 /*if (!dec->low_delay && vop_type == P_VOP) */  
                 if (vop_type == P_VOP)  
1898                          mb_swap(&dec->mbs, &dec->last_mbs);                          mb_swap(&dec->mbs, &dec->last_mbs);
1899                    dec->last_reduced_resolution = reduced_resolution;
1900    
1901                    dec->frames++;
1902                    seen_something = 1;
1903    
1904            }else{  /* B_VOP */
1905    
1906                    if (dec->low_delay)
1907                    {
1908                            DPRINTF(DPRINTF_ERROR, "warning: bvop found in low_delay==1 stream");
1909                            dec->low_delay = 1;
1910                    }
1911    
1912                    if (dec->frames < 2)
1913                    {
1914                            /* attemping to decode a bvop without atleast 2 reference frames */
1915                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1916                                                    "broken b-frame, mising ref frames");
1917                    }else if (dec->time_pp <= dec->time_bp) {
1918                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1919                            decoded in vfw. */
1920                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1921                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1922                    }else{
1923                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1924                    }
1925    
1926                    decoder_output(dec, &dec->cur, dec->mbs, frame, reduced_resolution);
1927                    output = 1;
1928                    dec->frames++;
1929            }
1930    
1931            BitstreamByteAlign(&bs);
1932    
1933            /* low_delay_default mode: repeat in packed_mode */
1934            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0)
1935            {
1936                    success = 1;
1937                    goto repeat;
1938            }
1939    
1940    done :
1941    
1942            /* low_delay_default mode: if we've gotten here without outputting anything,
1943               then output the recently decoded frame, or print an error message  */
1944            if (dec->low_delay_default && output == 0)
1945            {
1946                    if (dec->packed_mode && seen_something)
1947                    {
1948                            /* output the recently decoded frame */
1949                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, dec->last_reduced_resolution);
1950                            output = 1;
1951                    }
1952                    else
1953                    {
1954                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1955                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1956                                    "warning: nothing to output");
1957                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1958                                    "bframe decoder lag");
1959    
1960                            decoder_output(dec, &dec->cur, NULL, frame, 1 /*disable pp*/);
1961                    }
1962            }
1963    
1964            frame->length = BitstreamPos(&bs) / 8;
1965    
1966            if (stats)
1967            {
1968                    stats->notify = output ? XVID_DEC_VOP : XVID_DEC_NOTHING;
1969                    stats->data.vop.time_base = (int)dec->time_base;
1970                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1971          }          }
1972    
1973          emms();          emms();

Legend:
Removed from v.1.46  
changed lines
  Added in v.1.48

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