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

Annotation of /trunk/xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1617 - (view) (download)

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

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