[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.4, Wed Mar 20 14:02:59 2002 UTC revision 1.67, Mon Aug 16 22:38:06 2004 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC   *      XVID MPEG-4 VIDEO CODEC
4   *      decoder main   *  - Decoder Module -
5   *   *
6   *      This program is an implementation of a part of one or more MPEG-4   *  Copyright(C) 2002      MinChen <chenm001@163.com>
7   *      Video tools as specified in ISO/IEC 14496-2 standard.  Those intending   *               2002-2004 Peter Ross <pross@xvid.org>
  *      to use this software module in hardware or software products are  
  *      advised that its use may infringe existing patents or copyrights, and  
  *      any such use would be at such party's own risk.  The original  
  *      developer of this software module and his/her company, and subsequent  
  *      editors and their companies, will have no liability for use of this  
  *      software or modifications or derivatives thereof.  
8   *   *
9   *      This program is xvid_free software; you can redistribute it and/or modify   *  This program is free software ; you can redistribute it and/or modify
10   *      it 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
11   *      the xvid_free Software Foundation; either version 2 of the License, or   *  the Free Software Foundation ; either version 2 of the License, or
12   *      (at your option) any later version.   *      (at your option) any later version.
13   *   *
14   *      This program is distributed in the hope that it will be useful,   *      This program is distributed in the hope that it will be useful,
# Line 23  Line 17 
17   *      GNU General Public License for more details.   *      GNU General Public License for more details.
18   *   *
19   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
20   *      along with this program; if not, write to the xvid_free Software   *  along with this program ; if not, write to the Free Software
21   *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
23   *************************************************************************/   * $Id$
   
 /**************************************************************************  
  *  
  *      History:  
  *  
  *      26.12.2001      decoder_mbinter: dequant/idct moved within if(coded) block  
  *      22.12.2001      block based interpolation  
  *      01.12.2001      inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>  
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27    #include <stdio.h>
28  #include <stdlib.h>  #include <stdlib.h>
29  #include <string.h>  // memset  #include <string.h>
30    
31    #ifdef BFRAMES_DEC_DEBUG
32            #define BFRAMES_DEC
33    #endif
34    
35  #include "xvid.h"  #include "xvid.h"
36  #include "portab.h"  #include "portab.h"
37    #include "global.h"
38    
39  #include "decoder.h"  #include "decoder.h"
40  #include "bitstream/bitstream.h"  #include "bitstream/bitstream.h"
41  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
42    
43  #include "quant/quant_h263.h"  #include "quant/quant.h"
44  #include "quant/quant_mpeg4.h"  #include "quant/quant_matrix.h"
45  #include "dct/idct.h"  #include "dct/idct.h"
46  #include "dct/fdct.h"  #include "dct/fdct.h"
47  #include "utils/mem_transfer.h"  #include "utils/mem_transfer.h"
48  #include "image/interpolate8x8.h"  #include "image/interpolate8x8.h"
49    #include "image/reduced.h"
50    #include "image/font.h"
51    #include "image/qpel.h"
52    
53  #include "bitstream/mbcoding.h"  #include "bitstream/mbcoding.h"
54  #include "prediction/mbprediction.h"  #include "prediction/mbprediction.h"
55  #include "utils/timer.h"  #include "utils/timer.h"
56  #include "utils/emms.h"  #include "utils/emms.h"
57    #include "motion/motion.h"
58    #include "motion/gmc.h"
59    
60  #include "image/image.h"  #include "image/image.h"
61  #include "image/colorspace.h"  #include "image/colorspace.h"
62    #include "image/postprocessing.h"
63  #include "utils/mem_align.h"  #include "utils/mem_align.h"
64    
65  int decoder_create(XVID_DEC_PARAM * param)  static int
66  {  decoder_resize(DECODER * dec)
         DECODER * dec;  
   
         dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);  
         if (dec == NULL)  
67          {          {
68                  return XVID_ERR_MEMORY;          /* free existing */
69          }          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
70          param->handle = dec;          image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
71            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
72          dec->width = param->width;          image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
73          dec->height = param->height;          image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
74    
75            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
76    
77            if (dec->last_mbs)
78                    xvid_free(dec->last_mbs);
79            if (dec->mbs)
80                    xvid_free(dec->mbs);
81            if (dec->qscale)
82                    xvid_free(dec->qscale);
83    
84            /* realloc */
85          dec->mb_width = (dec->width + 15) / 16;          dec->mb_width = (dec->width + 15) / 16;
86          dec->mb_height = (dec->height + 15) / 16;          dec->mb_height = (dec->height + 15) / 16;
87    
88          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;          dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
89          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;          dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
90    
91          if (image_create(&dec->cur, dec->edged_width, dec->edged_height))          if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) {
         {  
92                  xvid_free(dec);                  xvid_free(dec);
93                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
94          }          }
95    
96          if (image_create(&dec->refn, dec->edged_width, dec->edged_height))          if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) {
         {  
97                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
98                  xvid_free(dec);                  xvid_free(dec);
99                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
100          }          }
101    
102          dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE);          /* Support B-frame to reference last 2 frame */
103          if (dec->mbs == NULL)          if (image_create(&dec->refn[1], 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                    xvid_free(dec);
107                    return XVID_ERR_MEMORY;
108            }
109            if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) {
110                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
111                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
112                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
113                  xvid_free(dec);                  xvid_free(dec);
114                  return XVID_ERR_MEMORY;                  return XVID_ERR_MEMORY;
115          }          }
116    
117          init_timer();          if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) {
118          create_vlc_tables();                  image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
119                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
120                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
121                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
122                    xvid_free(dec);
123                    return XVID_ERR_MEMORY;
124            }
125    
126          return XVID_ERR_OK;          if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) {
127                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
128                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
129                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
130                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
131                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
132                    xvid_free(dec);
133                    return XVID_ERR_MEMORY;
134  }  }
135    
136            dec->mbs =
137                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
138                                            CACHE_LINE);
139            if (dec->mbs == NULL) {
140                    image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
141                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
142                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
143                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
144                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
145                    xvid_free(dec);
146                    return XVID_ERR_MEMORY;
147            }
148            memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
149    
150  int decoder_destroy(DECODER * dec)          /* For skip MB flag */
151  {          dec->last_mbs =
152                    xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height,
153                                            CACHE_LINE);
154            if (dec->last_mbs == NULL) {
155          xvid_free(dec->mbs);          xvid_free(dec->mbs);
         image_destroy(&dec->refn, dec->edged_width, dec->edged_height);  
156          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);          image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
157                    image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
158                    image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
159                    image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
160                    image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
161          xvid_free(dec);          xvid_free(dec);
162                    return XVID_ERR_MEMORY;
163            }
164    
165          destroy_vlc_tables();          memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
166    
167          write_timer();          /* nothing happens if that fails */
168          return XVID_ERR_OK;          dec->qscale =
169                    xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE);
170    
171            if (dec->qscale)
172                    memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height);
173    
174            return 0;
175  }  }
176    
177    
178    int
179    decoder_create(xvid_dec_create_t * create)
180    {
181            DECODER *dec;
182    
183            if (XVID_VERSION_MAJOR(create->version) != 1)   /* v1.x.x */
184                    return XVID_ERR_VERSION;
185    
186  static const int32_t dquant_table[4] =          dec = xvid_malloc(sizeof(DECODER), CACHE_LINE);
187            if (dec == NULL) {
188                    return XVID_ERR_MEMORY;
189            }
190    
191            memset(dec, 0, sizeof(DECODER));
192    
193            dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE);
194            if (dec->mpeg_quant_matrices == NULL) {
195                    xvid_free(dec);
196                    return XVID_ERR_MEMORY;
197            }
198    
199            create->handle = dec;
200    
201            dec->width = create->width;
202            dec->height = create->height;
203    
204            image_null(&dec->cur);
205            image_null(&dec->refn[0]);
206            image_null(&dec->refn[1]);
207            image_null(&dec->tmp);
208            image_null(&dec->qtmp);
209    
210            /* image based GMC */
211            image_null(&dec->gmc);
212    
213            dec->mbs = NULL;
214            dec->last_mbs = NULL;
215            dec->qscale = NULL;
216    
217            init_timer();
218            init_postproc(&dec->postproc);
219            init_mpeg_matrix(dec->mpeg_quant_matrices);
220    
221            /* For B-frame support (used to save reference frame's time */
222            dec->frames = 0;
223            dec->time = dec->time_base = dec->last_time_base = 0;
224            dec->low_delay = 0;
225            dec->packed_mode = 0;
226            dec->time_inc_resolution = 1; /* until VOL header says otherwise */
227    
228            dec->fixed_dimensions = (dec->width > 0 && dec->height > 0);
229    
230            if (dec->fixed_dimensions)
231                    return decoder_resize(dec);
232            else
233                    return 0;
234    }
235    
236    
237    int
238    decoder_destroy(DECODER * dec)
239  {  {
240            xvid_free(dec->last_mbs);
241            xvid_free(dec->mbs);
242            xvid_free(dec->qscale);
243    
244            /* image based GMC */
245            image_destroy(&dec->gmc, dec->edged_width, dec->edged_height);
246    
247            image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height);
248            image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height);
249            image_destroy(&dec->tmp, dec->edged_width, dec->edged_height);
250            image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height);
251            image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
252            xvid_free(dec->mpeg_quant_matrices);
253            xvid_free(dec);
254    
255            write_timer();
256            return 0;
257    }
258    
259    static const int32_t dquant_table[4] = {
260          -1, -2, 1, 2          -1, -2, 1, 2
261  };  };
262    
263    /* decode an intra macroblock */
264    static void
265    decoder_mbintra(DECODER * dec,
266                                    MACROBLOCK * pMB,
267                                    const uint32_t x_pos,
268                                    const uint32_t y_pos,
269                                    const uint32_t acpred_flag,
270                                    const uint32_t cbp,
271                                    Bitstream * bs,
272                                    const uint32_t quant,
273                                    const uint32_t intra_dc_threshold,
274                                    const unsigned int bound,
275                                    const int reduced_resolution)
276    {
277    
278            DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE);
279            DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE);
280    
281            uint32_t stride = dec->edged_width;
282            uint32_t stride2 = stride / 2;
283            uint32_t next_block = stride * 8;
284            uint32_t i;
285            uint32_t iQuant = pMB->quant;
286            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
287    
288            if (reduced_resolution) {
289                    pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
290                    pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
291                    pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
292            }else{
293                    pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
294                    pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
295                    pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
296            }
297    
298  // decode an intra macroblock          memset(block, 0, 6 * 64 * sizeof(int16_t));     /* clear */
   
 void decoder_mbintra(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int intra_dc_threshold)  
 {  
         uint32_t k;  
299    
300          for (k = 0; k < 6; k++)          for (i = 0; i < 6; i++) {
301          {                  uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
                 uint32_t dcscalar;  
                 CACHE_ALIGN int16_t block[64];  
                 CACHE_ALIGN int16_t data[64];  
302                  int16_t predictors[8];                  int16_t predictors[8];
303                  int start_coeff;                  int start_coeff;
304    
                 dcscalar = get_dc_scaler(mb->quant, k < 4);  
   
305                  start_timer();                  start_timer();
306                  predict_acdc(dec->mbs, x, y, dec->mb_width, k, block, mb->quant, dcscalar, predictors);                  predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64],
307                  if (!acpred_flag)                                           iQuant, iDcScaler, predictors, bound);
308                  {                  if (!acpred_flag) {
309                          mb->acpred_directions[k] = 0;                          pMB->acpred_directions[i] = 0;
310                  }                  }
311                  stop_prediction_timer();                  stop_prediction_timer();
312    
313                  memset(block, 0, 64*sizeof(int16_t));           // clear                  if (quant < intra_dc_threshold) {
   
                 if (quant < intra_dc_threshold)  
                 {  
314                          int dc_size;                          int dc_size;
315                          int dc_dif;                          int dc_dif;
316    
317                          dc_size = k < 4 ?  get_dc_size_lum(bs) : get_dc_size_chrom(bs);                          dc_size = i < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs);
318                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;                          dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
319    
320                          if (dc_size > 8)                          if (dc_size > 8) {
321                          {                                  BitstreamSkip(bs, 1);   /* marker */
                                 BitstreamSkip(bs, 1);           // marker  
322                          }                          }
323    
324                          block[0] = dc_dif;                          block[i * 64 + 0] = dc_dif;
325                          start_coeff = 1;                          start_coeff = 1;
326                  }  
327                  else                          DPRINTF(XVID_DEBUG_COEFF,"block[0] %i\n", dc_dif);
328                  {                  } else {
329                          start_coeff = 0;                          start_coeff = 0;
330                  }                  }
331    
332                  start_timer();                  start_timer();
333                  if (cbp & (1 << (5-k)))                 // coded                  if (cbp & (1 << (5 - i)))       /* coded */
334                  {                  {
335                          get_intra_block(bs, block, mb->acpred_directions[k], start_coeff);                          int direction = dec->alternate_vertical_scan ?
336                                    2 : pMB->acpred_directions[i];
337    
338                            get_intra_block(bs, &block[i * 64], direction, start_coeff);
339                  }                  }
340                  stop_coding_timer();                  stop_coding_timer();
341    
342                  start_timer();                  start_timer();
343                  add_acdc(mb, k, block, dcscalar, predictors);                  add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version);
344                  stop_prediction_timer();                  stop_prediction_timer();
345    
346                  start_timer();                  start_timer();
347                  if (dec->quant_type == 0)                  if (dec->quant_type == 0) {
348                  {                          dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
349                          dequant_intra(data, block, mb->quant, dcscalar);                  } else {
350                  }                          dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices);
                 else  
                 {  
                         dequant4_intra(data, block, mb->quant, dcscalar);  
351                  }                  }
352                  stop_iquant_timer();                  stop_iquant_timer();
353    
354                  start_timer();                  start_timer();
355                  idct(data);                  idct(&data[i * 64]);
356                  stop_idct_timer();                  stop_idct_timer();
357    
                 start_timer();  
                 if (k < 4)  
                 {  
                         transfer_16to8copy(dec->cur.y + (16*y*dec->edged_width) + 16*x + (4*(k&2)*dec->edged_width) + 8*(k&1), data, dec->edged_width);  
358                  }                  }
359                  else if (k == 4)  
360                  {          if (dec->interlacing && pMB->field_dct) {
361                          transfer_16to8copy(dec->cur.u+ 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));                  next_block = stride;
362                    stride *= 2;
363                  }                  }
364                  else    // if (k == 5)  
365            start_timer();
366    
367            if (reduced_resolution)
368                  {                  {
369                          transfer_16to8copy(dec->cur.v + 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));                  next_block*=2;
370                    copy_upsampled_8x8_16to8(pY_Cur, &data[0 * 64], stride);
371                    copy_upsampled_8x8_16to8(pY_Cur + 16, &data[1 * 64], stride);
372                    copy_upsampled_8x8_16to8(pY_Cur + next_block, &data[2 * 64], stride);
373                    copy_upsampled_8x8_16to8(pY_Cur + 16 + next_block, &data[3 * 64], stride);
374                    copy_upsampled_8x8_16to8(pU_Cur, &data[4 * 64], stride2);
375                    copy_upsampled_8x8_16to8(pV_Cur, &data[5 * 64], stride2);
376            }else{
377                    transfer_16to8copy(pY_Cur, &data[0 * 64], stride);
378                    transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride);
379                    transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride);
380                    transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride);
381                    transfer_16to8copy(pU_Cur, &data[4 * 64], stride2);
382                    transfer_16to8copy(pV_Cur, &data[5 * 64], stride2);
383                  }                  }
384                  stop_transfer_timer();                  stop_transfer_timer();
385          }          }
 }  
386    
387    static void
388    decoder_mb_decode(DECODER * dec,
389                                    const uint32_t cbp,
390                                    Bitstream * bs,
391                                    uint8_t * pY_Cur,
392                                    uint8_t * pU_Cur,
393                                    uint8_t * pV_Cur,
394                                    int reduced_resolution,
395                                    const MACROBLOCK * pMB)
396    {
397            DECLARE_ALIGNED_MATRIX(data, 1, 64, int16_t, CACHE_LINE);
398    
399            int stride = dec->edged_width;
400            int next_block = stride * (reduced_resolution ? 16 : 8);
401            int i;
402            const uint32_t iQuant = pMB->quant;
403            const int direction = dec->alternate_vertical_scan ? 2 : 0;
404            typedef void (*get_inter_block_function_t)(
405                            Bitstream * bs,
406                            int16_t * block,
407                            int direction,
408                            const int quant,
409                            const uint16_t *matrix);
410            typedef void (*add_residual_function_t)(
411                            uint8_t *predicted_block,
412                            const int16_t *residual,
413                            int stride);
414    
415            const get_inter_block_function_t get_inter_block = (dec->quant_type == 0)
416                    ? (get_inter_block_function_t)get_inter_block_h263
417                    : (get_inter_block_function_t)get_inter_block_mpeg;
418    
419            const add_residual_function_t add_residual = (reduced_resolution)
420                    ? (add_residual_function_t)add_upsampled_8x8_16to8
421                    : (add_residual_function_t)transfer_16to8add;
422    
423            uint8_t *dst[6];
424            int strides[6];
425    
426    
427            if (dec->interlacing && pMB->field_dct) {
428                    next_block = stride;
429                    stride *= 2;
430            }
431    
432            reduced_resolution = !!reduced_resolution;
433            dst[0] = pY_Cur;
434            dst[2] = pY_Cur + next_block;
435            dst[1] = dst[0] + (8<<reduced_resolution);
436            dst[3] = dst[2] + (8<<reduced_resolution);
437            dst[4] = pU_Cur;
438            dst[5] = pV_Cur;
439            strides[0] = strides[1] = strides[2] = strides[3] = stride;
440            strides[4] = stride/2;
441            strides[5] = stride/2;
442    
443            for (i = 0; i < 6; i++) {
444                    /* Process only coded blocks */
445                    if (cbp & (1 << (5 - i))) {
446    
447                            /* Clear the block */
448                            memset(&data[0], 0, 64*sizeof(int16_t));
449    
450                            /* Decode coeffs and dequantize on the fly */
451                            start_timer();
452                            get_inter_block(bs, &data[0], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices));
453                            stop_coding_timer();
454    
455                            /* iDCT */
456                            start_timer();
457                            idct(&data[0]);
458                            stop_idct_timer();
459    
460  #define SIGN(X) (((X)>0)?1:-1)                          /* Add this residual to the predicted block */
461  #define ABS(X) (((X)>0)?(X):-(X))                          start_timer();
462  static const uint32_t roundtab[16] =                          add_residual(dst[i], &data[0], strides[i]);
463                  { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };                          stop_transfer_timer();
464                    }
465            }
466    }
467    
468    static void __inline
469    validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec)
470    {
471            /* clip a vector to valid range
472               prevents crashes if bitstream is broken
473            */
474            int shift = 5 + dec->quarterpel;
475            int xborder_high = (int)(dec->mb_width - x_pos) << shift;
476            int xborder_low = (-(int)x_pos-1) << shift;
477            int yborder_high = (int)(dec->mb_height - y_pos) << shift;
478            int yborder_low = (-(int)y_pos-1) << shift;
479    
480    #define CHECK_MV(mv) \
481            do { \
482            if ((mv).x > xborder_high) { \
483                    DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \
484                    (mv).x = xborder_high; \
485            } else if ((mv).x < xborder_low) { \
486                    DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \
487                    (mv).x = xborder_low; \
488            } \
489            if ((mv).y > yborder_high) { \
490                    DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \
491                    (mv).y = yborder_high; \
492            } else if ((mv).y < yborder_low) { \
493                    DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \
494                    (mv).y = yborder_low; \
495            } \
496            } while (0)
497    
498            CHECK_MV(mv[0]);
499            CHECK_MV(mv[1]);
500            CHECK_MV(mv[2]);
501            CHECK_MV(mv[3]);
502    }
503    
504    /* decode an inter macroblock */
505    static void
506    decoder_mbinter(DECODER * dec,
507                                    const MACROBLOCK * pMB,
508                                    const uint32_t x_pos,
509                                    const uint32_t y_pos,
510                                    const uint32_t cbp,
511                                    Bitstream * bs,
512                                    const uint32_t rounding,
513                                    const int reduced_resolution,
514                                    const int ref)
515    {
516            uint32_t stride = dec->edged_width;
517            uint32_t stride2 = stride / 2;
518            uint32_t i;
519    
520  // decode an inter macroblock          uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
521    
 void decoder_mbinter(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int rounding)  
 {  
         const uint32_t stride = dec->edged_width;  
         const uint32_t stride2 = dec->edged_width / 2;  
522          int uv_dx, uv_dy;          int uv_dx, uv_dy;
523          uint32_t k;          VECTOR mv[4];   /* local copy of mvs */
524    
525          if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)          if (reduced_resolution) {
526          {                  pY_Cur = dec->cur.y + (y_pos << 5) * stride + (x_pos << 5);
527                  uv_dx = mb->mvs[0].x;                  pU_Cur = dec->cur.u + (y_pos << 4) * stride2 + (x_pos << 4);
528                  uv_dy = mb->mvs[0].y;                  pV_Cur = dec->cur.v + (y_pos << 4) * stride2 + (x_pos << 4);
529                    for (i = 0; i < 4; i++) {
530                  uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;                          mv[i].x = RRV_MV_SCALEUP(pMB->mvs[i].x);
531                  uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;                          mv[i].y = RRV_MV_SCALEUP(pMB->mvs[i].y);
532                    }
533            } else {
534                    pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
535                    pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
536                    pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
537                    for (i = 0; i < 4; i++)
538                            mv[i] = pMB->mvs[i];
539          }          }
540    
541            validate_vector(mv, x_pos, y_pos, dec);
542    
543            start_timer();
544    
545            if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */
546    
547                    uv_dx = mv[0].x;
548                    uv_dy = mv[0].y;
549                    if (dec->quarterpel) {
550                            uv_dx /= 2;
551                            uv_dy /= 2;
552                    }
553                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
554                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
555    
556                    if (reduced_resolution)
557                            interpolate32x32_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
558                                                                            mv[0].x, mv[0].y, stride, rounding);
559                    else if (dec->quarterpel)
560                            interpolate16x16_quarterpel(dec->cur.y, dec->refn[ref].y, dec->qtmp.y, dec->qtmp.y + 64,
561                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
562                                                                                            mv[0].x, mv[0].y, stride, rounding);
563          else          else
564          {                          interpolate16x16_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos,
565                  int sum;                                                                          mv[0].x, mv[0].y, stride, rounding);
                 sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x;  
                 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
566    
567                  sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y;          } else {        /* MODE_INTER4V */
568                  uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );  
569                    if(dec->quarterpel) {
570                            uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2);
571                            uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2);
572                    } else {
573                            uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x;
574                            uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y;
575                    }
576    
577                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
578                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
579    
580                    if (reduced_resolution) {
581                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y, 32*x_pos, 32*y_pos,
582                                                                    mv[0].x, mv[0].y, stride, rounding);
583                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos,
584                                                                    mv[1].x, mv[1].y, stride, rounding);
585                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos, 32*y_pos + 16,
586                                                                    mv[2].x, mv[2].y, stride, rounding);
587                            interpolate16x16_switch(dec->cur.y, dec->refn[0].y , 32*x_pos + 16, 32*y_pos + 16,
588                                                                    mv[3].x, mv[3].y, stride, rounding);
589                            interpolate16x16_switch(dec->cur.u, dec->refn[0].u , 16 * x_pos, 16 * y_pos,
590                                                                    uv_dx, uv_dy, stride2, rounding);
591                            interpolate16x16_switch(dec->cur.v, dec->refn[0].v , 16 * x_pos, 16 * y_pos,
592                                                                    uv_dx, uv_dy, stride2, rounding);
593    
594                    } else if (dec->quarterpel) {
595                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
596                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
597                                                                            mv[0].x, mv[0].y, stride, rounding);
598                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
599                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
600                                                                            mv[1].x, mv[1].y, stride, rounding);
601                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
602                                                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
603                                                                            mv[2].x, mv[2].y, stride, rounding);
604                            interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y , dec->qtmp.y, dec->qtmp.y + 64,
605                                                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
606                                                                            mv[3].x, mv[3].y, stride, rounding);
607                    } else {
608                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos,
609                                                                    mv[0].x, mv[0].y, stride, rounding);
610                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos,
611                                                                    mv[1].x, mv[1].y, stride, rounding);
612                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos, 16*y_pos + 8,
613                                                                    mv[2].x, mv[2].y, stride, rounding);
614                            interpolate8x8_switch(dec->cur.y, dec->refn[0].y , 16*x_pos + 8, 16*y_pos + 8,
615                                                                    mv[3].x, mv[3].y, stride, rounding);
616                    }
617            }
618    
619            /* chroma */
620            if (reduced_resolution) {
621                    interpolate16x16_switch(dec->cur.u, dec->refn[0].u, 16 * x_pos, 16 * y_pos,
622                                                                    uv_dx, uv_dy, stride2, rounding);
623                    interpolate16x16_switch(dec->cur.v, dec->refn[0].v, 16 * x_pos, 16 * y_pos,
624                                                                    uv_dx, uv_dy, stride2, rounding);
625            } else {
626                    interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos,
627                                                                    uv_dx, uv_dy, stride2, rounding);
628                    interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos,
629                                                                    uv_dx, uv_dy, stride2, rounding);
630          }          }
631    
         start_timer();  
         interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x,     16*y    , mb->mvs[0].x, mb->mvs[0].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y    , mb->mvs[1].x, mb->mvs[1].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x,     16*y + 8, mb->mvs[2].x, mb->mvs[2].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y + 8, mb->mvs[3].x, mb->mvs[3].y, stride,  rounding);  
         interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);  
         interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);  
632          stop_comp_timer();          stop_comp_timer();
633    
634            if (cbp)
635                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur,
636                                                            reduced_resolution, pMB);
637    }
638    
639          for (k = 0; k < 6; k++)  static void
640    decoder_mbgmc(DECODER * dec,
641                                    MACROBLOCK * const pMB,
642                                    const uint32_t x_pos,
643                                    const uint32_t y_pos,
644                                    const uint32_t fcode,
645                                    const uint32_t cbp,
646                                    Bitstream * bs,
647                                    const uint32_t rounding)
648          {          {
649                  CACHE_ALIGN int16_t block[64];          const uint32_t stride = dec->edged_width;
650                  CACHE_ALIGN int16_t data[64];          const uint32_t stride2 = stride / 2;
651    
652                  if (cbp & (1 << (5-k)))                 // coded          uint8_t *const pY_Cur=dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
653                  {          uint8_t *const pU_Cur=dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
654                          memset(block, 0, 64 * sizeof(int16_t));         // clear          uint8_t *const pV_Cur=dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
655    
656                          start_timer();          NEW_GMC_DATA * gmc_data = &dec->new_gmc_data;
                         get_inter_block(bs, block);  
                         stop_coding_timer();  
657    
658                          start_timer();          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
                         if (dec->quant_type == 0)  
                         {  
                                 dequant_inter(data, block, mb->quant);  
                         }  
                         else  
                         {  
                                 dequant4_inter(data, block, mb->quant);  
                         }  
                         stop_iquant_timer();  
659    
660                          start_timer();                          start_timer();
                         idct(data);  
                         stop_idct_timer();  
661    
662                          start_timer();  /* this is where the calculations are done */
663                          if (k < 4)  
664                          {          gmc_data->predict_16x16(gmc_data,
665                                  transfer_16to8add(dec->cur.y + (16*y + 4*(k&2))*stride + 16*x + 8*(k&1), data, stride);                          dec->cur.y + y_pos*16*stride + x_pos*16, dec->refn[0].y,
666                          }                          stride, stride, x_pos, y_pos, rounding);
667                          else if (k == 4)  
668                          {          gmc_data->predict_8x8(gmc_data,
669                                  transfer_16to8add(dec->cur.u + 8*y*stride2 + 8*x, data, stride2);                          dec->cur.u + y_pos*8*stride2 + x_pos*8, dec->refn[0].u,
670                          }                          dec->cur.v + y_pos*8*stride2 + x_pos*8, dec->refn[0].v,
671                          else // k == 5                          stride2, stride2, x_pos, y_pos, rounding);
672                          {  
673                                  transfer_16to8add(dec->cur.v + 8*y*stride2 + 8*x, data, stride2);          gmc_data->get_average_mv(gmc_data, &pMB->amv, x_pos, y_pos, dec->quarterpel);
674                          }  
675            pMB->amv.x = gmc_sanitize(pMB->amv.x, dec->quarterpel, fcode);
676            pMB->amv.y = gmc_sanitize(pMB->amv.y, dec->quarterpel, fcode);
677    
678            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
679    
680                          stop_transfer_timer();                          stop_transfer_timer();
                 }  
         }  
 }  
681    
682            if (cbp)
683                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
684    
685    }
686    
687    
688  void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)  static void
689    decoder_iframe(DECODER * dec,
690                                    Bitstream * bs,
691                                    int reduced_resolution,
692                                    int quant,
693                                    int intra_dc_threshold)
694  {  {
695            uint32_t bound;
696          uint32_t x, y;          uint32_t x, y;
697            uint32_t mb_width = dec->mb_width;
698            uint32_t mb_height = dec->mb_height;
699    
700          for (y = 0; y < dec->mb_height; y++)          if (reduced_resolution) {
701          {                  mb_width = (dec->width + 31) / 32;
702                  for (x = 0; x < dec->mb_width; x++)                  mb_height = (dec->height + 31) / 32;
703                  {          }
704                          MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
705            bound = 0;
706    
707            for (y = 0; y < mb_height; y++) {
708                    for (x = 0; x < mb_width; x++) {
709                            MACROBLOCK *mb;
710                          uint32_t mcbpc;                          uint32_t mcbpc;
711                          uint32_t cbpc;                          uint32_t cbpc;
712                          uint32_t acpred_flag;                          uint32_t acpred_flag;
713                          uint32_t cbpy;                          uint32_t cbpy;
714                          uint32_t cbp;                          uint32_t cbp;
715    
716                            while (BitstreamShowBits(bs, 9) == 1)
717                                    BitstreamSkip(bs, 9);
718    
719                            if (check_resync_marker(bs, 0))
720                            {
721                                    bound = read_video_packet_header(bs, dec, 0,
722                                                            &quant, NULL, NULL, &intra_dc_threshold);
723                                    x = bound % mb_width;
724                                    y = bound / mb_width;
725                            }
726                            mb = &dec->mbs[y * dec->mb_width + x];
727    
728                            DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
729    
730                          mcbpc = get_mcbpc_intra(bs);                          mcbpc = get_mcbpc_intra(bs);
731                          mb->mode = mcbpc & 7;                          mb->mode = mcbpc & 7;
732                          cbpc = (mcbpc >> 4);                          cbpc = (mcbpc >> 4);
733    
734                          acpred_flag = BitstreamGetBit(bs);                          acpred_flag = BitstreamGetBit(bs);
735    
                         if (mb->mode == MODE_STUFFING)  
                         {  
                                 DEBUG("-- STUFFING ?");  
                                 continue;  
                         }  
   
736                          cbpy = get_cbpy(bs, 1);                          cbpy = get_cbpy(bs, 1);
737                          cbp = (cbpy << 2) | cbpc;                          cbp = (cbpy << 2) | cbpc;
738    
739                          if (mb->mode == MODE_INTRA_Q)                          if (mb->mode == MODE_INTRA_Q) {
                         {  
740                                  quant += dquant_table[BitstreamGetBits(bs,2)];                                  quant += dquant_table[BitstreamGetBits(bs,2)];
741                                  if (quant > 31)                                  if (quant > 31) {
                                 {  
742                                          quant = 31;                                          quant = 31;
743                                  }                                  } else if (quant < 1) {
                                 else if (quant < 1)  
                                 {  
744                                          quant = 1;                                          quant = 1;
745                                  }                                  }
746                          }                          }
747                          mb->quant = quant;                          mb->quant = quant;
748                            mb->mvs[0].x = mb->mvs[0].y =
749                            mb->mvs[1].x = mb->mvs[1].y =
750                            mb->mvs[2].x = mb->mvs[2].y =
751                            mb->mvs[3].x = mb->mvs[3].y =0;
752    
753                            if (dec->interlacing) {
754                                    mb->field_dct = BitstreamGetBit(bs);
755                                    DPRINTF(XVID_DEBUG_MB,"deci: field_dct: %i\n", mb->field_dct);
756                            }
757    
758                            decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
759                                                            intra_dc_threshold, bound, reduced_resolution);
760    
                         decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);  
761                  }                  }
762                    if(dec->out_frm)
763                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,mb_width);
764          }          }
765    
766  }  }
767    
768    
769  void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)  static void
770    get_motion_vector(DECODER * dec,
771                                    Bitstream * bs,
772                                    int x,
773                                    int y,
774                                    int k,
775                                    VECTOR * ret_mv,
776                                    int fcode,
777                                    const int bound)
778  {  {
         int scale_fac = 1 << (fcode - 1);  
         int high = (32 * scale_fac) - 1;  
         int low = ((-32) * scale_fac);  
         int range = (64 * scale_fac);  
   
         VECTOR pmv[4];  
         uint32_t psad[4];  
779    
780          int mv_x, mv_y;          const int scale_fac = 1 << (fcode - 1);
781          int pmv_x, pmv_y;          const int high = (32 * scale_fac) - 1;
782            const int low = ((-32) * scale_fac);
783            const int range = (64 * scale_fac);
784    
785            const VECTOR pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k);
786            VECTOR mv;
787    
788          get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);          mv.x = get_mv(bs, fcode);
789            mv.y = get_mv(bs, fcode);
790    
791          pmv_x = pmv[0].x;          DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", mv.x, mv.y, pmv.x, pmv.y, mv.x+pmv.x, mv.y+pmv.y);
         pmv_y = pmv[0].y;  
792    
793          mv_x = get_mv(bs, fcode);          mv.x += pmv.x;
794          mv_y = get_mv(bs, fcode);          mv.y += pmv.y;
795    
796          mv_x += pmv_x;          if (mv.x < low) {
797          mv_y += pmv_y;                  mv.x += range;
798            } else if (mv.x > high) {
799                    mv.x -= range;
800            }
801    
802          if (mv_x < low)          if (mv.y < low) {
803          {                  mv.y += range;
804                  mv_x += range;          } else if (mv.y > high) {
805                    mv.y -= range;
806          }          }
807          else if (mv_x > high)  
808          {          ret_mv->x = mv.x;
809                  mv_x -= range;          ret_mv->y = mv.y;
810          }          }
811    
812          if (mv_y < low)  /* for P_VOP set gmc_warp to NULL */
813    static void
814    decoder_pframe(DECODER * dec,
815                                    Bitstream * bs,
816                                    int rounding,
817                                    int reduced_resolution,
818                                    int quant,
819                                    int fcode,
820                                    int intra_dc_threshold,
821                                    const WARPPOINTS *const gmc_warp)
822          {          {
823                  mv_y += range;          uint32_t x, y;
824            uint32_t bound;
825            int cp_mb, st_mb;
826            uint32_t mb_width = dec->mb_width;
827            uint32_t mb_height = dec->mb_height;
828    
829            if (reduced_resolution) {
830                    mb_width = (dec->width + 31) / 32;
831                    mb_height = (dec->height + 31) / 32;
832          }          }
833          else if (mv_y > high)  
834          {          if (!dec->is_edged[0]) {
835                  mv_y -= range;                  start_timer();
836                    image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
837                                                    dec->width, dec->height, dec->bs_version);
838                    dec->is_edged[0] = 1;
839                    stop_edges_timer();
840          }          }
841    
842          mv->x = mv_x;          if (gmc_warp) {
843          mv->y = mv_y;                  /* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */
844                    generate_GMCparameters( dec->sprite_warping_points,
845                                    dec->sprite_warping_accuracy, gmc_warp,
846                                    dec->width, dec->height, &dec->new_gmc_data);
847    
848                    /* image warping is done block-based in decoder_mbgmc(), now */
849  }  }
850    
851            bound = 0;
852    
853  void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)          for (y = 0; y < mb_height; y++) {
854  {                  cp_mb = st_mb = 0;
855          uint32_t x, y;                  for (x = 0; x < mb_width; x++) {
856                            MACROBLOCK *mb;
857    
858          image_swap(&dec->cur, &dec->refn);                          /* skip stuffing */
859                            while (BitstreamShowBits(bs, 10) == 1)
860                                    BitstreamSkip(bs, 10);
861    
862          start_timer();                          if (check_resync_marker(bs, fcode - 1)) {
863          image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height);                                  bound = read_video_packet_header(bs, dec, fcode - 1,
864          stop_edges_timer();                                          &quant, &fcode, NULL, &intra_dc_threshold);
865                                    x = bound % mb_width;
866                                    y = bound / mb_width;
867                            }
868                            mb = &dec->mbs[y * dec->mb_width + x];
869    
870          for (y = 0; y < dec->mb_height; y++)                          DPRINTF(XVID_DEBUG_MB, "macroblock (%i,%i) %08x\n", x, y, BitstreamShowBits(bs, 32));
         {  
                 for (x = 0; x < dec->mb_width; x++)  
                 {  
                         MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];  
871    
872                          if (!BitstreamGetBit(bs))                       // not_coded                          if (!(BitstreamGetBit(bs)))     { /* block _is_ coded */
873                          {                                  uint32_t mcbpc, cbpc, cbpy, cbp;
874                                  uint32_t mcbpc;                                  uint32_t intra, acpred_flag = 0;
875                                  uint32_t cbpc;                                  int mcsel = 0;          /* mcsel: '0'=local motion, '1'=GMC */
                                 uint32_t acpred_flag;  
                                 uint32_t cbpy;  
                                 uint32_t cbp;  
                                 uint32_t intra;  
876    
877                                    cp_mb++;
878                                  mcbpc = get_mcbpc_inter(bs);                                  mcbpc = get_mcbpc_inter(bs);
879                                  mb->mode = mcbpc & 7;                                  mb->mode = mcbpc & 7;
880                                  cbpc = (mcbpc >> 4);                                  cbpc = (mcbpc >> 4);
881                                  acpred_flag = 0;  
882                                    DPRINTF(XVID_DEBUG_MB, "mode %i\n", mb->mode);
883                                    DPRINTF(XVID_DEBUG_MB, "cbpc %i\n", cbpc);
884    
885                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);                                  intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
886    
887                                  if (intra)                                  if (gmc_warp && (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q))
888                                  {                                          mcsel = BitstreamGetBit(bs);
889                                    else if (intra)
890                                          acpred_flag = BitstreamGetBit(bs);                                          acpred_flag = BitstreamGetBit(bs);
                                 }  
   
                                 if (mb->mode == MODE_STUFFING)  
                                 {  
                                         DEBUG("-- STUFFING ?");  
                                         continue;  
                                 }  
891    
892                                  cbpy = get_cbpy(bs, intra);                                  cbpy = get_cbpy(bs, intra);
893                                    DPRINTF(XVID_DEBUG_MB, "cbpy %i mcsel %i \n", cbpy,mcsel);
894    
895                                  cbp = (cbpy << 2) | cbpc;                                  cbp = (cbpy << 2) | cbpc;
896    
897                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)                                  if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) {
898                                  {                                          int dquant = dquant_table[BitstreamGetBits(bs, 2)];
899                                          quant += dquant_table[BitstreamGetBits(bs,2)];                                          DPRINTF(XVID_DEBUG_MB, "dquant %i\n", dquant);
900                                          if (quant > 31)                                          quant += dquant;
901                                          {                                          if (quant > 31) {
902                                                  quant = 31;                                                  quant = 31;
903                                          }                                          } else if (quant < 1) {
                                         else if (mb->quant < 1)  
                                         {  
904                                                  quant = 1;                                                  quant = 1;
905                                          }                                          }
906                                            DPRINTF(XVID_DEBUG_MB, "quant %i\n", quant);
907                                  }                                  }
908                                  mb->quant = quant;                                  mb->quant = quant;
909    
910                                  if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)                                  if (dec->interlacing) {
911                                  {                                          if (cbp || intra) {
912                                                    mb->field_dct = BitstreamGetBit(bs);
913                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
914                                            }
915    
916                                          get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);                                          if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) {
917                                          mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;                                                  mb->field_pred = BitstreamGetBit(bs);
918                                          mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;                                                  DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
919    
920                                                    if (mb->field_pred) {
921                                                            mb->field_for_top = BitstreamGetBit(bs);
922                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
923                                                            mb->field_for_bot = BitstreamGetBit(bs);
924                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
925                                  }                                  }
                                 else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)  
                                 {  
                                         get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);  
                                         get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);  
                                         get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);  
                                         get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);  
926                                  }                                  }
927                                  else  // MODE_INTRA, MODE_INTRA_Q                                  }
928                                  {  
929                                    if (mcsel) {
930                                            decoder_mbgmc(dec, mb, x, y, fcode, cbp, bs, rounding);
931                                            continue;
932    
933                                    } else if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) {
934    
935                                            if (dec->interlacing && mb->field_pred) {
936                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
937                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode, bound);
938                                            } else {
939                                                    get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
940                                                    mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0];
941                                            }
942                                    } else if (mb->mode == MODE_INTER4V ) {
943                                            get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound);
944                                            get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound);
945                                            get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound);
946                                            get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound);
947                                    } else {                /* MODE_INTRA, MODE_INTRA_Q */
948                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                          mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
949                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                          mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
950                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);                                          decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant,
951                                                                            intra_dc_threshold, bound, reduced_resolution);
952                                          continue;                                          continue;
953                                  }                                  }
954    
955                                  decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);                                  decoder_mbinter(dec, mb, x, y, cbp, bs,
956                          }                                                                  rounding, reduced_resolution, 0);
957                          else    // not coded  
958                          {                          } else if (gmc_warp) {  /* a not coded S(GMC)-VOP macroblock */
959                                    mb->mode = MODE_NOT_CODED_GMC;
960                                    mb->quant = quant;
961                                    decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding);
962    
963                                    if(dec->out_frm && cp_mb > 0) {
964                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
965                                            cp_mb = 0;
966                                    }
967                                    st_mb = x+1;
968                            } else {        /* not coded P_VOP macroblock */
969                                  mb->mode = MODE_NOT_CODED;                                  mb->mode = MODE_NOT_CODED;
970                                    mb->quant = quant;
971    
972                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;                                  mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
973                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;                                  mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
974    
975                                  // copy macroblock directly from ref to cur                                  decoder_mbinter(dec, mb, x, y, 0, bs,
976                                                                    rounding, reduced_resolution, 0);
977    
978                                    if(dec->out_frm && cp_mb > 0) {
979                                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
980                                            cp_mb = 0;
981                                    }
982                                    st_mb = x+1;
983                            }
984                    }
985    
986                    if(dec->out_frm && cp_mb > 0)
987                            output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb);
988            }
989    }
990    
991    
992    /* decode B-frame motion vector */
993    static void
994    get_b_motion_vector(Bitstream * bs,
995                                            VECTOR * mv,
996                                            int fcode,
997                                            const VECTOR pmv,
998                                            const DECODER * const dec,
999                                            const int x, const int y)
1000    {
1001            const int scale_fac = 1 << (fcode - 1);
1002            const int high = (32 * scale_fac) - 1;
1003            const int low = ((-32) * scale_fac);
1004            const int range = (64 * scale_fac);
1005    
1006            int mv_x = get_mv(bs, fcode);
1007            int mv_y = get_mv(bs, fcode);
1008    
1009            mv_x += pmv.x;
1010            mv_y += pmv.y;
1011    
1012            if (mv_x < low)
1013                    mv_x += range;
1014            else if (mv_x > high)
1015                    mv_x -= range;
1016    
1017            if (mv_y < low)
1018                    mv_y += range;
1019            else if (mv_y > high)
1020                    mv_y -= range;
1021    
1022            mv->x = mv_x;
1023            mv->y = mv_y;
1024    }
1025    
1026    /* decode an B-frame direct & interpolate macroblock */
1027    static void
1028    decoder_bf_interpolate_mbinter(DECODER * dec,
1029                                                                    IMAGE forward,
1030                                                                    IMAGE backward,
1031                                                                    MACROBLOCK * pMB,
1032                                                                    const uint32_t x_pos,
1033                                                                    const uint32_t y_pos,
1034                                                                    Bitstream * bs,
1035                                                                    const int direct)
1036    {
1037            uint32_t stride = dec->edged_width;
1038            uint32_t stride2 = stride / 2;
1039            int uv_dx, uv_dy;
1040            int b_uv_dx, b_uv_dy;
1041            uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
1042            const uint32_t cbp = pMB->cbp;
1043    
1044            pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4);
1045            pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3);
1046            pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3);
1047    
1048            validate_vector(pMB->mvs, x_pos, y_pos, dec);
1049            validate_vector(pMB->b_mvs, x_pos, y_pos, dec);
1050    
1051            if (!direct) {
1052                    uv_dx = pMB->mvs[0].x;
1053                    uv_dy = pMB->mvs[0].y;
1054                    b_uv_dx = pMB->b_mvs[0].x;
1055                    b_uv_dy = pMB->b_mvs[0].y;
1056    
1057                    if (dec->quarterpel) {
1058                            uv_dx /= 2;
1059                            uv_dy /= 2;
1060                            b_uv_dx /= 2;
1061                            b_uv_dy /= 2;
1062                    }
1063    
1064                    uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3];
1065                    uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3];
1066                    b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3];
1067                    b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3];
1068    
1069            } else {
1070                    uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x;
1071                    uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y;
1072                    b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x;
1073                    b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y;
1074    
1075                    if (dec->quarterpel) {
1076                            uv_dx /= 2;
1077                            uv_dy /= 2;
1078                            b_uv_dx /= 2;
1079                            b_uv_dy /= 2;
1080                    }
1081    
1082                    uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf];
1083                    uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf];
1084                    b_uv_dx = (b_uv_dx >> 3) + roundtab_76[b_uv_dx & 0xf];
1085                    b_uv_dy = (b_uv_dy >> 3) + roundtab_76[b_uv_dy & 0xf];
1086            }
1087    
1088            start_timer();
1089            if(dec->quarterpel) {
1090                    if(!direct) {
1091                            interpolate16x16_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1092                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1093                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1094                    } else {
1095                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1096                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1097                                                                                    pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1098                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1099                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1100                                                                                    pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1101                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1102                                                                                    dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1103                                                                                    pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1104                            interpolate8x8_quarterpel(dec->cur.y, forward.y, dec->qtmp.y, dec->qtmp.y + 64,
1105                                                                                    dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1106                                                                                    pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1107                    }
1108            } else {
1109                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos,
1110                                                            pMB->mvs[0].x, pMB->mvs[0].y, stride, 0);
1111                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos,
1112                                                            pMB->mvs[1].x, pMB->mvs[1].y, stride, 0);
1113                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8,
1114                                                            pMB->mvs[2].x, pMB->mvs[2].y, stride, 0);
1115                    interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8,
1116                                                            pMB->mvs[3].x, pMB->mvs[3].y, stride, 0);
1117            }
1118    
1119            interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx,
1120                                                    uv_dy, stride2, 0);
1121            interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx,
1122                                                    uv_dy, stride2, 0);
1123    
1124    
1125            if(dec->quarterpel) {
1126                    if(!direct) {
1127                            interpolate16x16_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1128                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1129                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1130                    } else {
1131                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1132                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos,
1133                                            pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1134                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1135                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos,
1136                                            pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1137                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1138                                            dec->qtmp.y + 128, 16*x_pos, 16*y_pos + 8,
1139                                            pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1140                            interpolate8x8_add_quarterpel(dec->cur.y, backward.y, dec->qtmp.y, dec->qtmp.y + 64,
1141                                            dec->qtmp.y + 128, 16*x_pos + 8, 16*y_pos + 8,
1142                                            pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1143                    }
1144            } else {
1145                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos, 16 * y_pos,
1146                                    pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0);
1147                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1148                                    16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0);
1149                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos,
1150                                    16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0);
1151                    interpolate8x8_add_switch(dec->cur.y, backward.y, 16 * x_pos + 8,
1152                                    16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0);
1153            }
1154    
1155            interpolate8x8_add_switch(dec->cur.u, backward.u, 8 * x_pos, 8 * y_pos,
1156                            b_uv_dx, b_uv_dy, stride2, 0);
1157            interpolate8x8_add_switch(dec->cur.v, backward.v, 8 * x_pos, 8 * y_pos,
1158                            b_uv_dx, b_uv_dy, stride2, 0);
1159    
1160            stop_comp_timer();
1161    
1162            if (cbp)
1163                    decoder_mb_decode(dec, cbp, bs, pY_Cur, pU_Cur, pV_Cur, 0, pMB);
1164    }
1165    
1166    /* for decode B-frame dbquant */
1167    static __inline int32_t
1168    get_dbquant(Bitstream * bs)
1169    {
1170            if (!BitstreamGetBit(bs))               /*  '0' */
1171                    return (0);
1172            else if (!BitstreamGetBit(bs))  /* '10' */
1173                    return (-2);
1174            else                                                    /* '11' */
1175                    return (2);
1176    }
1177    
1178    /*
1179     * decode B-frame mb_type
1180     * bit          ret_value
1181     * 1            0
1182     * 01           1
1183     * 001          2
1184     * 0001         3
1185     */
1186    static int32_t __inline
1187    get_mbtype(Bitstream * bs)
1188    {
1189            int32_t mb_type;
1190    
1191            for (mb_type = 0; mb_type <= 3; mb_type++)
1192                    if (BitstreamGetBit(bs))
1193                            return (mb_type);
1194    
1195            return -1;
1196    }
1197    
1198    static void
1199    decoder_bframe(DECODER * dec,
1200                                    Bitstream * bs,
1201                                    int quant,
1202                                    int fcode_forward,
1203                                    int fcode_backward)
1204    {
1205            uint32_t x, y;
1206            VECTOR mv;
1207            const VECTOR zeromv = {0,0};
1208            int i;
1209    
1210            if (!dec->is_edged[0]) {
1211                    start_timer();
1212                    image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height,
1213                                                    dec->width, dec->height, dec->bs_version);
1214                    dec->is_edged[0] = 1;
1215                    stop_edges_timer();
1216            }
1217    
1218            if (!dec->is_edged[1]) {
1219                                  start_timer();                                  start_timer();
1220                    image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height,
1221                                                    dec->width, dec->height, dec->bs_version);
1222                    dec->is_edged[1] = 1;
1223                    stop_edges_timer();
1224            }
1225    
1226                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),          for (y = 0; y < dec->mb_height; y++) {
1227                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x),                  /* Initialize Pred Motion Vector */
1228                                                                  dec->edged_width);                  dec->p_fmv = dec->p_bmv = zeromv;
1229                    for (x = 0; x < dec->mb_width; x++) {
1230                            MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x];
1231                            MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x];
1232                            const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward;
1233                            uint32_t intra_dc_threshold; /* fake variable */
1234    
1235                            if (check_resync_marker(bs, fcode_max  - 1)) {
1236                                    int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant,
1237                                                                                                             &fcode_forward, &fcode_backward, &intra_dc_threshold);
1238                                    x = bound % dec->mb_width;
1239                                    y = bound / dec->mb_width;
1240                                    /* reset predicted macroblocks */
1241                                    dec->p_fmv = dec->p_bmv = zeromv;
1242                            }
1243    
1244                            mv =
1245                            mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] =
1246                            mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv;
1247                            mb->quant = quant;
1248    
1249                                  transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),                          /*
1250                                                                  dec->refn.y + (16*y)*dec->edged_width + (16*x+8),                           * skip if the co-located P_VOP macroblock is not coded
1251                                                                  dec->edged_width);                           * if not codec in co-located S_VOP macroblock is _not_
1252                             * automatically skipped
1253                             */
1254    
1255                            if (last_mb->mode == MODE_NOT_CODED) {
1256                                    mb->cbp = 0;
1257                                    mb->mode = MODE_FORWARD;
1258                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
1259                                    continue;
1260                            }
1261    
1262                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),                          if (!BitstreamGetBit(bs)) {     /* modb=='0' */
1263                                                                  dec->refn.y + (16*y+8)*dec->edged_width + (16*x),                                  const uint8_t modb2 = BitstreamGetBit(bs);
                                                                 dec->edged_width);  
1264    
1265                                  transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),                                  mb->mode = get_mbtype(bs);
                                                                 dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),  
                                                                 dec->edged_width);  
1266    
1267                                  transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),                                  if (!modb2)             /* modb=='00' */
1268                                                                  dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),                                          mb->cbp = BitstreamGetBits(bs, 6);
1269                                                                  dec->edged_width/2);                                  else
1270                                            mb->cbp = 0;
1271    
1272                                  transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),                                  if (mb->mode && mb->cbp) {
1273                                                                  dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),                                          quant += get_dbquant(bs);
1274                                                                  dec->edged_width/2);                                          if (quant > 31)
1275                                                    quant = 31;
1276                                            else if (quant < 1)
1277                                                    quant = 1;
1278                                    }
1279                                    mb->quant = quant;
1280    
1281                                  stop_transfer_timer();                                  if (dec->interlacing) {
1282                                            if (mb->cbp) {
1283                                                    mb->field_dct = BitstreamGetBit(bs);
1284                                                    DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct);
1285                                            }
1286    
1287                                            if (mb->mode) {
1288                                                    mb->field_pred = BitstreamGetBit(bs);
1289                                                    DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred);
1290    
1291                                                    if (mb->field_pred) {
1292                                                            mb->field_for_top = BitstreamGetBit(bs);
1293                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_top: %i\n", mb->field_for_top);
1294                                                            mb->field_for_bot = BitstreamGetBit(bs);
1295                                                            DPRINTF(XVID_DEBUG_MB,"decp: field_for_bot: %i\n", mb->field_for_bot);
1296                                                    }
1297                                            }
1298                                    }
1299    
1300                            } else {
1301                                    mb->mode = MODE_DIRECT_NONE_MV;
1302                                    mb->cbp = 0;
1303                            }
1304    
1305                            switch (mb->mode) {
1306                            case MODE_DIRECT:
1307                                    get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y);
1308    
1309                            case MODE_DIRECT_NONE_MV:
1310                                    for (i = 0; i < 4; i++) {
1311                                            mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x;
1312                                            mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y;
1313    
1314                                            mb->b_mvs[i].x = (mv.x)
1315                                                    ?  mb->mvs[i].x - last_mb->mvs[i].x
1316                                                    : last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp;
1317                                            mb->b_mvs[i].y = (mv.y)
1318                                                    ? mb->mvs[i].y - last_mb->mvs[i].y
1319                                                    : last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp;
1320                                    }
1321    
1322                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1323                                                                                                    mb, x, y, bs, 1);
1324                                    break;
1325    
1326                            case MODE_INTERPOLATE:
1327                                    get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1328                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1329    
1330                                    get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1331                                    dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0];
1332    
1333                                    decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0],
1334                                                                                            mb, x, y, bs, 0);
1335                                    break;
1336    
1337                            case MODE_BACKWARD:
1338                                    get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y);
1339                                    dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1340    
1341                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0);
1342                                    break;
1343    
1344                            case MODE_FORWARD:
1345                                    get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y);
1346                                    dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] =     mb->mvs[0];
1347    
1348                                    decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1);
1349                                    break;
1350    
1351                            default:
1352                                    DPRINTF(XVID_DEBUG_ERROR,"Not supported B-frame mb_type = %i\n", mb->mode);
1353                            }
1354                    } /* End of for */
1355            }
1356                          }                          }
1357    
1358    /* perform post processing if necessary, and output the image */
1359    void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs,
1360                                            xvid_dec_frame_t * frame, xvid_dec_stats_t * stats,
1361                                            int coding_type, int quant)
1362    {
1363            const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0;
1364    
1365            if (dec->cartoon_mode)
1366                    frame->general &= ~XVID_FILMEFFECT;
1367    
1368            if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0)
1369                    && mbs != NULL) /* post process */
1370            {
1371                    /* note: image is stored to tmp */
1372                    image_copy(&dec->tmp, img, dec->edged_width, dec->height);
1373                    image_postproc(&dec->postproc, &dec->tmp, dec->edged_width,
1374                                               mbs, dec->mb_width, dec->mb_height, dec->mb_width,
1375                                               frame->general, brightness, dec->frames, (coding_type == B_VOP));
1376                    img = &dec->tmp;
1377                  }                  }
1378    
1379            image_output(img, dec->width, dec->height,
1380                                     dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride,
1381                                     frame->output.csp, dec->interlacing);
1382    
1383            if (stats) {
1384                    stats->type = coding2type(coding_type);
1385                    stats->data.vop.time_base = (int)dec->time_base;
1386                    stats->data.vop.time_increment = 0;     /* XXX: todo */
1387                    stats->data.vop.qscale_stride = dec->mb_width;
1388                    stats->data.vop.qscale = dec->qscale;
1389                    if (stats->data.vop.qscale != NULL && mbs != NULL) {
1390                            int i;
1391                            for (i = 0; i < dec->mb_width*dec->mb_height; i++)
1392                                    stats->data.vop.qscale[i] = mbs[i].quant;
1393                    } else
1394                            stats->data.vop.qscale = NULL;
1395          }          }
1396  }  }
1397    
1398  int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)  int
1399    decoder_decode(DECODER * dec,
1400                                    xvid_dec_frame_t * frame, xvid_dec_stats_t * stats)
1401  {  {
1402    
1403          Bitstream bs;          Bitstream bs;
1404          uint32_t rounding;          uint32_t rounding;
1405          uint32_t quant;          uint32_t reduced_resolution;
1406          uint32_t fcode;          uint32_t quant = 2;
1407            uint32_t fcode_forward;
1408            uint32_t fcode_backward;
1409          uint32_t intra_dc_threshold;          uint32_t intra_dc_threshold;
1410            WARPPOINTS gmc_warp;
1411            int coding_type;
1412            int success, output, seen_something;
1413    
1414            if (XVID_VERSION_MAJOR(frame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1))      /* v1.x.x */
1415                    return XVID_ERR_VERSION;
1416    
1417          start_global_timer();          start_global_timer();
1418    
1419            dec->low_delay_default = (frame->general & XVID_LOWDELAY);
1420            if ((frame->general & XVID_DISCONTINUITY))
1421                    dec->frames = 0;
1422            dec->out_frm = (frame->output.csp == XVID_CSP_SLICE) ? &frame->output : NULL;
1423    
1424            if (frame->length < 0) {        /* decoder flush */
1425                    int ret;
1426                    /* if not decoding "low_delay/packed", and this isn't low_delay and
1427                            we have a reference frame, then outout the reference frame */
1428                    if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) {
1429                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1430                            dec->frames = 0;
1431                            ret = 0;
1432                    } else {
1433                            if (stats) stats->type = XVID_TYPE_NOTHING;
1434                            ret = XVID_ERR_END;
1435                    }
1436    
1437                    emms();
1438                    stop_global_timer();
1439                    return ret;
1440            }
1441    
1442          BitstreamInit(&bs, frame->bitstream, frame->length);          BitstreamInit(&bs, frame->bitstream, frame->length);
1443    
1444          switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))          /* XXX: 0x7f is only valid whilst decoding vfw xvid/divx5 avi's */
1445            if(dec->low_delay_default && frame->length == 1 && BitstreamShowBits(&bs, 8) == 0x7f)
1446          {          {
1447          case P_VOP :                  image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width,
1448                  decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);                                           (uint8_t**)frame->output.plane, frame->output.stride, frame->output.csp, dec->interlacing);
1449                  break;                  if (stats) stats->type = XVID_TYPE_NOTHING;
1450                    emms();
1451                    return 1;       /* one byte consumed */
1452            }
1453    
1454          case I_VOP :          success = 0;
1455                  //DEBUG1("",intra_dc_threshold);          output = 0;
1456                  decoder_iframe(dec, &bs, quant, intra_dc_threshold);          seen_something = 0;
                 break;  
1457    
1458          case B_VOP :    // ignore  repeat:
                 break;  
1459    
1460          case N_VOP :    // vop not coded          coding_type = BitstreamReadHeaders(&bs, dec, &rounding, &reduced_resolution,
1461                  break;                          &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold, &gmc_warp);
1462    
1463          default :          DPRINTF(XVID_DEBUG_HEADER, "coding_type=%i,  packed=%i,  time=%lli,  time_pp=%i,  time_bp=%i\n",
1464                  return XVID_ERR_FAIL;                                                          coding_type,    dec->packed_mode, dec->time, dec->time_pp, dec->time_bp);
1465    
1466            if (coding_type == -1) { /* nothing */
1467                    if (success) goto done;
1468                    if (stats) stats->type = XVID_TYPE_NOTHING;
1469                    emms();
1470                    return BitstreamPos(&bs)/8;
1471          }          }
1472    
1473          frame->length = BitstreamPos(&bs) / 8;          if (coding_type == -2 || coding_type == -3) {   /* vol and/or resize */
1474    
1475          start_timer();                  if (coding_type == -3)
1476          image_output(&dec->cur, dec->width, dec->height, dec->edged_width,                          decoder_resize(dec);
                                 frame->image, frame->stride, frame->colorspace);  
         stop_conv_timer();  
1477    
1478                    if (stats) {
1479                            stats->type = XVID_TYPE_VOL;
1480                            stats->data.vol.general = 0;
1481                            /*XXX: if (dec->interlacing)
1482                                    stats->data.vol.general |= ++INTERLACING; */
1483                            stats->data.vol.width = dec->width;
1484                            stats->data.vol.height = dec->height;
1485                            stats->data.vol.par = dec->aspect_ratio;
1486                            stats->data.vol.par_width = dec->par_width;
1487                            stats->data.vol.par_height = dec->par_height;
1488          emms();          emms();
1489                            return BitstreamPos(&bs)/8;     /* number of bytes consumed */
1490                    }
1491                    goto repeat;
1492            }
1493    
1494            if(dec->frames == 0 && coding_type != I_VOP) {
1495                    /* 1st frame is not an i-vop */
1496                    goto repeat;
1497            }
1498    
1499            dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0;  /* init pred vector to 0 */
1500    
1501            /* packed_mode: special-N_VOP treament */
1502            if (dec->packed_mode && coding_type == N_VOP) {
1503                    if (dec->low_delay_default && dec->frames > 0) {
1504                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1505                            output = 1;
1506                    }
1507                    /* ignore otherwise */
1508            } else if (coding_type != B_VOP) {
1509                    switch(coding_type) {
1510                    case I_VOP :
1511                            decoder_iframe(dec, &bs, reduced_resolution, quant, intra_dc_threshold);
1512                            break;
1513                    case P_VOP :
1514                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1515                                                    fcode_forward, intra_dc_threshold, NULL);
1516                            break;
1517                    case S_VOP :
1518                            decoder_pframe(dec, &bs, rounding, reduced_resolution, quant,
1519                                                    fcode_forward, intra_dc_threshold, &gmc_warp);
1520                            break;
1521                    case N_VOP :
1522                            /* XXX: not_coded vops are not used for forward prediction */
1523                            /* we should not swap(last_mbs,mbs) */
1524                            image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height);
1525                            SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */
1526                            break;
1527                    }
1528    
1529                    if (reduced_resolution) {
1530                            image_deblock_rrv(&dec->cur, dec->edged_width, dec->mbs,
1531                                    (dec->width + 31) / 32, (dec->height + 31) / 32, dec->mb_width,
1532                                    16, 0);
1533                    }
1534    
1535                    /* note: for packed_mode, output is performed when the special-N_VOP is decoded */
1536                    if (!(dec->low_delay_default && dec->packed_mode)) {
1537                            if (dec->low_delay) {
1538                                    decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1539                                    output = 1;
1540                            } else if (dec->frames > 0)     { /* is the reference frame valid? */
1541                                    /* output the reference frame */
1542                                    decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1543                                    output = 1;
1544                            }
1545                    }
1546    
1547                    image_swap(&dec->refn[0], &dec->refn[1]);
1548                    dec->is_edged[1] = dec->is_edged[0];
1549                    image_swap(&dec->cur, &dec->refn[0]);
1550                    dec->is_edged[0] = 0;
1551                    SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs);
1552                    dec->last_reduced_resolution = reduced_resolution;
1553                    dec->last_coding_type = coding_type;
1554    
1555                    dec->frames++;
1556                    seen_something = 1;
1557    
1558            } else {        /* B_VOP */
1559    
1560                    if (dec->low_delay) {
1561                            DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n");
1562                            dec->low_delay = 0;
1563                    }
1564    
1565                    if (dec->frames < 2) {
1566                            /* attemping to decode a bvop without atleast 2 reference frames */
1567                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1568                                                    "broken b-frame, mising ref frames");
1569                            if (stats) stats->type = XVID_TYPE_NOTHING;
1570                    } else if (dec->time_pp <= dec->time_bp) {
1571                            /* this occurs when dx50_bvop_compatibility==0 sequences are
1572                            decoded in vfw. */
1573                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1574                                                    "broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp);
1575                            if (stats) stats->type = XVID_TYPE_NOTHING;
1576                    } else {
1577                            decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward);
1578                            decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant);
1579                    }
1580    
1581                    output = 1;
1582                    dec->frames++;
1583            }
1584    
1585    #if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */
1586             BitstreamByteAlign(&bs);
1587    #endif
1588    
1589            /* low_delay_default mode: repeat in packed_mode */
1590            if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) {
1591                    success = 1;
1592                    goto repeat;
1593            }
1594    
1595    done :
1596    
1597            /* low_delay_default mode: if we've gotten here without outputting anything,
1598               then output the recently decoded frame, or print an error message  */
1599            if (dec->low_delay_default && output == 0) {
1600                    if (dec->packed_mode && seen_something) {
1601                            /* output the recently decoded frame */
1602                            decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant);
1603                    } else {
1604                            image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128);
1605                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16,
1606                                    "warning: nothing to output");
1607                            image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64,
1608                                    "bframe decoder lag");
1609    
1610                            decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant);
1611                            if (stats) stats->type = XVID_TYPE_NOTHING;
1612                    }
1613            }
1614    
1615            emms();
1616          stop_global_timer();          stop_global_timer();
1617    
1618          return XVID_ERR_OK;          return (BitstreamPos(&bs) + 7) / 8;     /* number of bytes consumed */
1619  }  }

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.67

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