[cvs] / xvidcore / src / decoder.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.72 - (view) (download)

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

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