3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Decoder main module - |
* - Decoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 MinChen <chenm001@163.com> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002 Peter Ross <pross@xvid.org> |
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
10 |
* it under the terms of the GNU General Public License as published by |
* |
11 |
|
* XviD is free software; you can redistribute it and/or modify it |
12 |
|
* under the terms of the GNU General Public License as published by |
13 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
14 |
* (at your option) any later version. |
* (at your option) any later version. |
15 |
* |
* |
22 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
* |
* |
25 |
*************************************************************************/ |
* Under section 8 of the GNU General Public License, the copyright |
26 |
|
* holders of XVID explicitly forbid distribution in the following |
27 |
/************************************************************************** |
* countries: |
28 |
|
* |
29 |
|
* - Japan |
30 |
|
* - United States of America |
31 |
* |
* |
32 |
* History: |
* Linking XviD statically or dynamically with other modules is making a |
33 |
|
* combined work based on XviD. Thus, the terms and conditions of the |
34 |
|
* GNU General Public License cover the whole combination. |
35 |
* |
* |
36 |
* 22.06.2002 added primative N_VOP support |
* As a special exception, the copyright holders of XviD give you |
37 |
* #define BFRAMES_DEC now enables Minchenm's bframe decoder |
* permission to link XviD with independent modules that communicate with |
38 |
* 08.05.2002 add low_delay support for B_VOP decode |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
39 |
* MinChen <chenm001@163.com> |
* license terms of these independent modules, and to copy and distribute |
40 |
* 05.05.2002 fix some B-frame decode problem |
* the resulting combined work under terms of your choice, provided that |
41 |
* 02.05.2002 add B-frame decode support(have some problem); |
* every copy of the combined work is accompanied by a complete copy of |
42 |
* MinChen <chenm001@163.com> |
* the source code of XviD (the version of XviD used to produce the |
43 |
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
* combined work), being distributed under the terms of the GNU General |
44 |
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
* Public License plus this exception. An independent module is a module |
45 |
* reconstructing blocks, thus artifacts |
* which is not derived from or based on XviD. |
|
* interlacing speedup - used transfers to re-interlace |
|
|
* interlaced decoding should be as fast as progressive now |
|
|
* 26.03.2002 interlacing support - moved transfers outside decode loop |
|
|
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
|
|
* 22.12.2001 lock based interpolation |
|
|
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
|
46 |
* |
* |
47 |
* $Id: decoder.c,v 1.25 2002-07-09 01:09:33 chenm001 Exp $ |
* Note that people who make modified versions of XviD are not obligated |
48 |
|
* to grant this special exception for their modified versions; it is |
49 |
|
* their choice whether to do so. The GNU General Public License gives |
50 |
|
* permission to release a modified version without this exception; this |
51 |
|
* exception also makes it possible to release a modified version which |
52 |
|
* carries forward this exception. |
53 |
|
* |
54 |
|
* $Id: decoder.c,v 1.43 2002-11-16 23:38:15 edgomez Exp $ |
55 |
* |
* |
56 |
*************************************************************************/ |
*************************************************************************/ |
57 |
|
|
58 |
#include <stdlib.h> |
#include <stdlib.h> |
59 |
#include <string.h> |
#include <string.h> |
60 |
|
|
61 |
|
#ifdef BFRAMES_DEC_DEBUG |
62 |
|
#define BFRAMES_DEC |
63 |
|
#endif |
64 |
|
|
65 |
#include "xvid.h" |
#include "xvid.h" |
66 |
#include "portab.h" |
#include "portab.h" |
67 |
|
|
174 |
} |
} |
175 |
|
|
176 |
|
|
|
|
|
|
/* **************************************************************** |
|
|
NIC 28.06.2002 |
|
|
riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla |
|
|
usare nella dll caricata dall'IM1 player |
|
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
|
|
**************************************************************** */ |
|
|
//XVID_DEC_FRAME * frame |
|
|
|
|
|
int |
|
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
|
|
{ |
|
|
DECODER *dec; |
|
|
Bitstream bs; |
|
|
uint32_t rounding; |
|
|
uint32_t quant; |
|
|
uint32_t fcode_forward; |
|
|
uint32_t fcode_backward; |
|
|
uint32_t intra_dc_threshold; |
|
|
|
|
|
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
|
|
if (dec == NULL) { |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
param->handle = dec; |
|
|
|
|
|
|
|
|
BitstreamInit(&bs, frame->bitstream, frame->length);//NIC |
|
|
|
|
|
//NIC |
|
|
BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, |
|
|
&fcode_backward, &intra_dc_threshold); |
|
|
|
|
|
|
|
|
param->width = dec->width; //NIC added |
|
|
param->height = dec->height; //NIC added |
|
|
//dec->width = param->width; //NIC commentate |
|
|
//dec->height = param->height; //NIC commentate |
|
|
|
|
|
dec->mb_width = (dec->width + 15) / 16; |
|
|
dec->mb_height = (dec->height + 15) / 16; |
|
|
|
|
|
dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; |
|
|
dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; |
|
|
dec->low_delay = 0; |
|
|
|
|
|
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for support B-frame to reference last 2 frame |
|
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
dec->mbs = |
|
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
|
|
CACHE_LINE); |
|
|
if (dec->mbs == NULL) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
|
|
|
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for skip MB flag |
|
|
dec->last_mbs = |
|
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
|
|
CACHE_LINE); |
|
|
if (dec->last_mbs == NULL) { |
|
|
xvid_free(dec->mbs); |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
|
|
|
|
|
init_timer(); |
|
|
|
|
|
// add by chenm001 <chenm001@163.com> |
|
|
// for support B-frame to save reference frame's time |
|
|
dec->frames = -1; |
|
|
dec->time = dec->time_base = dec->last_time_base = 0; |
|
|
|
|
|
return XVID_ERR_OK; |
|
|
} |
|
|
/* **************************************************************** |
|
|
END NIC |
|
|
**************************************************************** */ |
|
|
|
|
177 |
int |
int |
178 |
decoder_destroy(DECODER * dec) |
decoder_destroy(DECODER * dec) |
179 |
{ |
{ |
209 |
const uint32_t cbp, |
const uint32_t cbp, |
210 |
Bitstream * bs, |
Bitstream * bs, |
211 |
const uint32_t quant, |
const uint32_t quant, |
212 |
const uint32_t intra_dc_threshold) |
const uint32_t intra_dc_threshold, |
213 |
|
const unsigned int bound) |
214 |
{ |
{ |
215 |
|
|
216 |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
236 |
|
|
237 |
start_timer(); |
start_timer(); |
238 |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
239 |
iQuant, iDcScaler, predictors); |
iQuant, iDcScaler, predictors, bound); |
240 |
if (!acpred_flag) { |
if (!acpred_flag) { |
241 |
pMB->acpred_directions[i] = 0; |
pMB->acpred_directions[i] = 0; |
242 |
} |
} |
255 |
|
|
256 |
block[i * 64 + 0] = dc_dif; |
block[i * 64 + 0] = dc_dif; |
257 |
start_coeff = 1; |
start_coeff = 1; |
258 |
|
|
259 |
|
DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); |
260 |
} else { |
} else { |
261 |
start_coeff = 0; |
start_coeff = 0; |
262 |
} |
} |
344 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
345 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
346 |
|
|
347 |
|
if (dec->quarterpel) |
348 |
|
{ |
349 |
|
uv_dx = (uv_dx >> 1) | (uv_dx & 1); |
350 |
|
uv_dy = (uv_dy >> 1) | (uv_dy & 1); |
351 |
|
} |
352 |
|
|
353 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
354 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
355 |
} else { |
} else { |
356 |
int sum; |
int sum; |
|
|
|
357 |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
358 |
uv_dx = |
|
359 |
(sum == |
if (dec->quarterpel) |
360 |
0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + |
{ |
361 |
(ABS(sum) / 16) * 2)); |
sum /= 2; |
362 |
|
} |
363 |
|
|
364 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
365 |
|
|
366 |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
367 |
uv_dy = |
|
368 |
(sum == |
if (dec->quarterpel) |
369 |
0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + |
{ |
370 |
(ABS(sum) / 16) * 2)); |
sum /= 2; |
371 |
|
} |
372 |
|
|
373 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
374 |
} |
} |
375 |
|
|
376 |
start_timer(); |
start_timer(); |
377 |
|
if(dec->quarterpel) { |
378 |
|
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n"); |
379 |
|
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, |
380 |
|
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
381 |
|
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
382 |
|
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
383 |
|
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
384 |
|
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
385 |
|
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
386 |
|
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
387 |
|
} |
388 |
|
else { |
389 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos, |
390 |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
391 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
392 |
16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
393 |
rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
394 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
395 |
16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
396 |
rounding); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
397 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, |
} |
398 |
16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, |
|
|
rounding); |
|
399 |
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
400 |
uv_dx, uv_dy, stride2, rounding); |
uv_dx, uv_dy, stride2, rounding); |
401 |
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
453 |
int quant, |
int quant, |
454 |
int intra_dc_threshold) |
int intra_dc_threshold) |
455 |
{ |
{ |
456 |
|
uint32_t bound; |
457 |
uint32_t x, y; |
uint32_t x, y; |
458 |
|
|
459 |
|
bound = 0; |
460 |
|
|
461 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
462 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
463 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb; |
|
|
|
464 |
uint32_t mcbpc; |
uint32_t mcbpc; |
465 |
uint32_t cbpc; |
uint32_t cbpc; |
466 |
uint32_t acpred_flag; |
uint32_t acpred_flag; |
467 |
uint32_t cbpy; |
uint32_t cbpy; |
468 |
uint32_t cbp; |
uint32_t cbp; |
469 |
|
|
470 |
|
while (BitstreamShowBits(bs, 9) == 1) |
471 |
|
BitstreamSkip(bs, 9); |
472 |
|
|
473 |
|
if (check_resync_marker(bs, 0)) |
474 |
|
{ |
475 |
|
bound = read_video_packet_header(bs, 0, &quant); |
476 |
|
x = bound % dec->mb_width; |
477 |
|
y = bound / dec->mb_width; |
478 |
|
} |
479 |
|
mb = &dec->mbs[y * dec->mb_width + x]; |
480 |
|
|
481 |
|
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
482 |
|
|
483 |
mcbpc = get_mcbpc_intra(bs); |
mcbpc = get_mcbpc_intra(bs); |
484 |
mb->mode = mcbpc & 7; |
mb->mode = mcbpc & 7; |
485 |
cbpc = (mcbpc >> 4); |
cbpc = (mcbpc >> 4); |
486 |
|
|
487 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
488 |
|
|
|
if (mb->mode == MODE_STUFFING) { |
|
|
DEBUG("-- STUFFING ?"); |
|
|
continue; |
|
|
} |
|
|
|
|
489 |
cbpy = get_cbpy(bs, 1); |
cbpy = get_cbpy(bs, 1); |
490 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
491 |
|
|
498 |
} |
} |
499 |
} |
} |
500 |
mb->quant = quant; |
mb->quant = quant; |
501 |
|
mb->mvs[0].x = mb->mvs[0].y = |
502 |
|
mb->mvs[1].x = mb->mvs[1].y = |
503 |
|
mb->mvs[2].x = mb->mvs[2].y = |
504 |
|
mb->mvs[3].x = mb->mvs[3].y =0; |
505 |
|
|
506 |
if (dec->interlacing) { |
if (dec->interlacing) { |
507 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
508 |
DEBUG1("deci: field_dct: ", mb->field_dct); |
DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct); |
509 |
} |
} |
510 |
|
|
511 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
512 |
intra_dc_threshold); |
intra_dc_threshold, bound); |
513 |
} |
} |
514 |
|
if(dec->out_frm) |
515 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width); |
516 |
|
|
517 |
} |
} |
518 |
|
|
519 |
} |
} |
526 |
int y, |
int y, |
527 |
int k, |
int k, |
528 |
VECTOR * mv, |
VECTOR * mv, |
529 |
int fcode) |
int fcode, |
530 |
|
const int bound) |
531 |
{ |
{ |
532 |
|
|
533 |
int scale_fac = 1 << (fcode - 1); |
int scale_fac = 1 << (fcode - 1); |
535 |
int low = ((-32) * scale_fac); |
int low = ((-32) * scale_fac); |
536 |
int range = (64 * scale_fac); |
int range = (64 * scale_fac); |
537 |
|
|
538 |
VECTOR pmv[4]; |
VECTOR pmv; |
|
int32_t psad[4]; |
|
|
|
|
539 |
int mv_x, mv_y; |
int mv_x, mv_y; |
|
int pmv_x, pmv_y; |
|
540 |
|
|
541 |
|
pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); |
|
get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); |
|
|
|
|
|
pmv_x = pmv[0].x; |
|
|
pmv_y = pmv[0].y; |
|
542 |
|
|
543 |
mv_x = get_mv(bs, fcode); |
mv_x = get_mv(bs, fcode); |
544 |
mv_y = get_mv(bs, fcode); |
mv_y = get_mv(bs, fcode); |
545 |
|
|
546 |
mv_x += pmv_x; |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y); |
547 |
mv_y += pmv_y; |
|
548 |
|
mv_x += pmv.x; |
549 |
|
mv_y += pmv.y; |
550 |
|
|
551 |
if (mv_x < low) { |
if (mv_x < low) { |
552 |
mv_x += range; |
mv_x += range; |
576 |
{ |
{ |
577 |
|
|
578 |
uint32_t x, y; |
uint32_t x, y; |
579 |
|
uint32_t bound; |
580 |
|
int cp_mb, st_mb; |
581 |
|
|
582 |
start_timer(); |
start_timer(); |
583 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
584 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
585 |
stop_edges_timer(); |
stop_edges_timer(); |
586 |
|
|
587 |
|
bound = 0; |
588 |
|
|
589 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
590 |
|
cp_mb = st_mb = 0; |
591 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
592 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb; |
593 |
|
|
594 |
|
// skip stuffing |
595 |
|
while (BitstreamShowBits(bs, 10) == 1) |
596 |
|
BitstreamSkip(bs, 10); |
597 |
|
|
598 |
|
if (check_resync_marker(bs, fcode - 1)) |
599 |
|
{ |
600 |
|
bound = read_video_packet_header(bs, fcode - 1, &quant); |
601 |
|
x = bound % dec->mb_width; |
602 |
|
y = bound / dec->mb_width; |
603 |
|
} |
604 |
|
mb = &dec->mbs[y * dec->mb_width + x]; |
605 |
|
|
606 |
|
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
607 |
|
|
608 |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
609 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) // not_coded |
615 |
uint32_t cbp; |
uint32_t cbp; |
616 |
uint32_t intra; |
uint32_t intra; |
617 |
|
|
618 |
|
cp_mb++; |
619 |
mcbpc = get_mcbpc_inter(bs); |
mcbpc = get_mcbpc_inter(bs); |
620 |
mb->mode = mcbpc & 7; |
mb->mode = mcbpc & 7; |
621 |
cbpc = (mcbpc >> 4); |
cbpc = (mcbpc >> 4); |
622 |
|
|
623 |
|
DPRINTF(DPRINTF_MB, "mode %i", mb->mode); |
624 |
|
DPRINTF(DPRINTF_MB, "cbpc %i", cbpc); |
625 |
acpred_flag = 0; |
acpred_flag = 0; |
626 |
|
|
627 |
intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); |
intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); |
630 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
631 |
} |
} |
632 |
|
|
|
if (mb->mode == MODE_STUFFING) { |
|
|
DEBUG("-- STUFFING ?"); |
|
|
continue; |
|
|
} |
|
|
|
|
633 |
cbpy = get_cbpy(bs, intra); |
cbpy = get_cbpy(bs, intra); |
634 |
|
DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); |
635 |
|
|
636 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
637 |
|
|
638 |
if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
639 |
quant += dquant_table[BitstreamGetBits(bs, 2)]; |
int dquant = dquant_table[BitstreamGetBits(bs, 2)]; |
640 |
|
DPRINTF(DPRINTF_MB, "dquant %i", dquant); |
641 |
|
quant += dquant; |
642 |
if (quant > 31) { |
if (quant > 31) { |
643 |
quant = 31; |
quant = 31; |
644 |
} else if (mb->quant < 1) { |
} else if (quant < 1) { |
645 |
quant = 1; |
quant = 1; |
646 |
} |
} |
647 |
|
DPRINTF(DPRINTF_MB, "quant %i", quant); |
648 |
} |
} |
649 |
mb->quant = quant; |
mb->quant = quant; |
650 |
|
|
651 |
if (dec->interlacing) { |
if (dec->interlacing) { |
652 |
|
if (cbp || intra) { |
653 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
654 |
DEBUG1("decp: field_dct: ", mb->field_dct); |
DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct); |
655 |
|
} |
656 |
|
|
657 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
658 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
659 |
DEBUG1("decp: field_pred: ", mb->field_pred); |
DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred); |
660 |
|
|
661 |
if (mb->field_pred) { |
if (mb->field_pred) { |
662 |
mb->field_for_top = BitstreamGetBit(bs); |
mb->field_for_top = BitstreamGetBit(bs); |
663 |
DEBUG1("decp: field_for_top: ", mb->field_for_top); |
DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top); |
664 |
mb->field_for_bot = BitstreamGetBit(bs); |
mb->field_for_bot = BitstreamGetBit(bs); |
665 |
DEBUG1("decp: field_for_bot: ", mb->field_for_bot); |
DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot); |
666 |
} |
} |
667 |
} |
} |
668 |
} |
} |
670 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
671 |
if (dec->interlacing && mb->field_pred) { |
if (dec->interlacing && mb->field_pred) { |
672 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
673 |
fcode); |
fcode, bound); |
674 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
675 |
fcode); |
fcode, bound); |
676 |
} else { |
} else { |
677 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
678 |
fcode); |
fcode, bound); |
679 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
680 |
mb->mvs[0].x; |
mb->mvs[0].x; |
681 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
682 |
mb->mvs[0].y; |
mb->mvs[0].y; |
683 |
} |
} |
684 |
} else if (mb->mode == |
} else if (mb->mode == MODE_INTER4V ) { |
685 |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
|
686 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); |
687 |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); |
688 |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); |
689 |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); |
690 |
} else // MODE_INTRA, MODE_INTRA_Q |
} else // MODE_INTRA, MODE_INTRA_Q |
691 |
{ |
{ |
692 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
694 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
695 |
0; |
0; |
696 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
697 |
intra_dc_threshold); |
intra_dc_threshold, bound); |
698 |
continue; |
continue; |
699 |
} |
} |
700 |
|
|
702 |
rounding); |
rounding); |
703 |
} else // not coded |
} else // not coded |
704 |
{ |
{ |
705 |
//DEBUG2("P-frame MB at (X,Y)=",x,y); |
DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y); |
706 |
|
|
707 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
708 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
709 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
745 |
dec->refn[0].v + |
dec->refn[0].v + |
746 |
(8 * y) * dec->edged_width / 2 + (8 * x), |
(8 * y) * dec->edged_width / 2 + (8 * x), |
747 |
dec->edged_width / 2); |
dec->edged_width / 2); |
|
|
|
748 |
stop_transfer_timer(); |
stop_transfer_timer(); |
749 |
|
if(dec->out_frm && cp_mb > 0) { |
750 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
751 |
|
cp_mb = 0; |
752 |
|
} |
753 |
|
st_mb = x+1; |
754 |
} |
} |
755 |
} |
} |
756 |
|
if(dec->out_frm && cp_mb > 0) |
757 |
|
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
758 |
} |
} |
759 |
} |
} |
760 |
|
|
832 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
833 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
834 |
|
|
835 |
|
|
836 |
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
837 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
838 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
926 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
927 |
const uint32_t x_pos, |
const uint32_t x_pos, |
928 |
const uint32_t y_pos, |
const uint32_t y_pos, |
|
const uint32_t cbp, |
|
929 |
Bitstream * bs) |
Bitstream * bs) |
930 |
{ |
{ |
931 |
|
|
940 |
int b_uv_dx, b_uv_dy; |
int b_uv_dx, b_uv_dy; |
941 |
uint32_t i; |
uint32_t i; |
942 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
943 |
|
const uint32_t cbp = pMB->cbp; |
944 |
|
|
945 |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
946 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
947 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
948 |
|
|
949 |
|
|
950 |
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
951 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
952 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
1036 |
stride2); |
stride2); |
1037 |
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, |
1038 |
stride2); |
stride2); |
|
|
|
1039 |
stop_comp_timer(); |
stop_comp_timer(); |
1040 |
|
|
1041 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
1126 |
int fcode_forward, |
int fcode_forward, |
1127 |
int fcode_backward) |
int fcode_backward) |
1128 |
{ |
{ |
|
|
|
1129 |
uint32_t x, y; |
uint32_t x, y; |
1130 |
VECTOR mv, zeromv; |
VECTOR mv; |
1131 |
|
const VECTOR zeromv = {0,0}; |
1132 |
|
#ifdef BFRAMES_DEC_DEBUG |
1133 |
|
FILE *fp; |
1134 |
|
static char first=0; |
1135 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1136 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \ |
1137 |
|
} |
1138 |
|
#endif |
1139 |
|
|
1140 |
start_timer(); |
start_timer(); |
1141 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1142 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1143 |
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1144 |
|
dec->width, dec->height); |
1145 |
stop_edges_timer(); |
stop_edges_timer(); |
1146 |
|
|
1147 |
|
#ifdef BFRAMES_DEC_DEBUG |
1148 |
|
if (!first){ |
1149 |
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
1150 |
|
} |
1151 |
|
#endif |
1152 |
|
|
1153 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1154 |
// Initialize Pred Motion Vector |
// Initialize Pred Motion Vector |
1155 |
dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; |
dec->p_fmv = dec->p_bmv = zeromv; |
1156 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1157 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1158 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1159 |
|
|
1160 |
mb->mvs[0].x = mb->mvs[0].y = zeromv.x = zeromv.y = mv.x = mv.y = |
mv = |
1161 |
0; |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
1162 |
|
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
1163 |
|
|
1164 |
// the last P_VOP is skip macroblock ? |
// the last P_VOP is skip macroblock ? |
1165 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1166 |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
|
mb->mb_type = MODE_FORWARD; |
|
1167 |
mb->cbp = 0; |
mb->cbp = 0; |
1168 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
#ifdef BFRAMES_DEC_DEBUG |
1169 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
mb->mb_type = MODE_NOT_CODED; |
1170 |
mb->quant = 8; |
BFRAME_DEBUG |
1171 |
|
#endif |
1172 |
|
mb->mb_type = MODE_FORWARD; |
1173 |
|
mb->quant = last_mb->quant; |
1174 |
|
//mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1175 |
|
//mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1176 |
|
|
1177 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
1178 |
continue; |
continue; |
1179 |
} |
} |
|
//t=BitstreamShowBits(bs,32); |
|
1180 |
|
|
1181 |
if (!BitstreamGetBit(bs)) { // modb=='0' |
if (!BitstreamGetBit(bs)) { // modb=='0' |
1182 |
const uint8_t modb2 = BitstreamGetBit(bs); |
const uint8_t modb2 = BitstreamGetBit(bs); |
1193 |
|
|
1194 |
if (quant > 31) { |
if (quant > 31) { |
1195 |
quant = 31; |
quant = 31; |
1196 |
} else if (mb->quant < 1) { |
} else if (quant < 1) { |
1197 |
quant = 1; |
quant = 1; |
1198 |
} |
} |
|
} else { |
|
|
quant = 8; |
|
1199 |
} |
} |
|
mb->quant = quant; |
|
1200 |
} else { |
} else { |
1201 |
mb->mb_type = MODE_DIRECT_NONE_MV; |
mb->mb_type = MODE_DIRECT_NONE_MV; |
1202 |
mb->cbp = 0; |
mb->cbp = 0; |
1203 |
} |
} |
1204 |
|
|
1205 |
mb->mode = MODE_INTER; |
mb->quant = quant; |
1206 |
|
mb->mode = MODE_INTER4V; |
1207 |
//DEBUG1("Switch bm_type=",mb->mb_type); |
//DEBUG1("Switch bm_type=",mb->mb_type); |
1208 |
|
|
1209 |
|
#ifdef BFRAMES_DEC_DEBUG |
1210 |
|
BFRAME_DEBUG |
1211 |
|
#endif |
1212 |
|
|
1213 |
switch (mb->mb_type) { |
switch (mb->mb_type) { |
1214 |
case MODE_DIRECT: |
case MODE_DIRECT: |
1215 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); |
1216 |
|
|
1217 |
case MODE_DIRECT_NONE_MV: |
case MODE_DIRECT_NONE_MV: |
1218 |
{ // Because this file is a C file not C++ so I use '{' to define var |
{ |
1219 |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
|
dec->time_pp; |
|
1220 |
int i; |
int i; |
1221 |
|
|
1222 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1223 |
mb->mvs[i].x = |
mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) |
1224 |
(int32_t) ((TRB * last_mb->mvs[i].x) / TRD + |
/ TRD + mv.x); |
1225 |
mb->mvs[0].x); |
mb->b_mvs[i].x = (int32_t) ((mv.x == 0) |
1226 |
mb->b_mvs[i].x = |
? ((TRB - TRD) * last_mb->mvs[i].x) |
1227 |
(int32_t) ((mb->mvs[0].x == |
/ TRD |
1228 |
0) ? ((TRB - |
: mb->mvs[i].x - last_mb->mvs[i].x); |
1229 |
TRD) * last_mb->mvs[i].x) / |
mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) |
1230 |
TRD : mb->mvs[i].x - last_mb->mvs[i].x); |
/ TRD + mv.y); |
1231 |
mb->mvs[i].y = |
mb->b_mvs[i].y = (int32_t) ((mv.y == 0) |
1232 |
(int32_t) ((TRB * last_mb->mvs[i].y) / TRD + |
? ((TRB - TRD) * last_mb->mvs[i].y) |
1233 |
mb->mvs[0].y); |
/ TRD |
1234 |
mb->b_mvs[i].y = |
: mb->mvs[i].y - last_mb->mvs[i].y); |
|
(int32_t) ((mb->mvs[0].y == |
|
|
0) ? ((TRB - |
|
|
TRD) * last_mb->mvs[i].y) / |
|
|
TRD : mb->mvs[i].y - last_mb->mvs[i].y); |
|
1235 |
} |
} |
1236 |
//DEBUG("B-frame Direct!\n"); |
//DEBUG("B-frame Direct!\n"); |
1237 |
} |
} |
|
mb->mode = MODE_INTER4V; |
|
1238 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1239 |
mb, x, y, mb->cbp, bs); |
mb, x, y, bs); |
1240 |
break; |
break; |
1241 |
|
|
1242 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1243 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1244 |
dec->p_fmv); |
dec->p_fmv); |
1245 |
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1246 |
|
|
1247 |
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], |
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], |
1248 |
fcode_backward, dec->p_bmv); |
fcode_backward, dec->p_bmv); |
1249 |
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = |
dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = |
1250 |
mb->b_mvs[3].x = mb->b_mvs[0].x; |
mb->b_mvs[3] = mb->b_mvs[0]; |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = |
|
|
mb->b_mvs[3].y = mb->b_mvs[0].y; |
|
1251 |
|
|
1252 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1253 |
mb, x, y, mb->cbp, bs); |
mb, x, y, bs); |
1254 |
//DEBUG("B-frame Bidir!\n"); |
//DEBUG("B-frame Bidir!\n"); |
1255 |
break; |
break; |
1256 |
|
|
1257 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1258 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
1259 |
dec->p_bmv); |
dec->p_bmv); |
1260 |
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1261 |
|
|
1262 |
|
mb->mode = MODE_INTER; |
1263 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1264 |
//DEBUG("B-frame Backward!\n"); |
//DEBUG("B-frame Backward!\n"); |
1265 |
break; |
break; |
1267 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1268 |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1269 |
dec->p_fmv); |
dec->p_fmv); |
1270 |
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
|
mb->mvs[0].x; |
|
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
|
|
mb->mvs[0].y; |
|
1271 |
|
|
1272 |
|
mb->mode = MODE_INTER; |
1273 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1274 |
//DEBUG("B-frame Forward!\n"); |
//DEBUG("B-frame Forward!\n"); |
1275 |
break; |
break; |
1276 |
|
|
1277 |
default: |
default: |
1278 |
DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type); |
1279 |
} |
} |
1280 |
|
|
1281 |
} // end of FOR |
} // end of FOR |
1282 |
} |
} |
1283 |
|
#ifdef BFRAMES_DEC_DEBUG |
1284 |
|
if (!first){ |
1285 |
|
first=1; |
1286 |
|
if (fp) |
1287 |
|
fclose(fp); |
1288 |
|
} |
1289 |
|
#endif |
1290 |
} |
} |
1291 |
|
|
1292 |
// swap two MACROBLOCK array |
// swap two MACROBLOCK array |
1315 |
|
|
1316 |
start_global_timer(); |
start_global_timer(); |
1317 |
|
|
1318 |
|
dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
1319 |
|
|
1320 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1321 |
|
|
1322 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1332 |
case P_VOP: |
case P_VOP: |
1333 |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
1334 |
intra_dc_threshold); |
intra_dc_threshold); |
1335 |
printf("P_VOP Time=%ld\n",dec->time); |
#ifdef BFRAMES_DEC |
1336 |
DEBUG1("P_VOP Time=", dec->time); |
DPRINTF(DPRINTF_DEBUG, "P_VOP Time=%d", dec->time); |
1337 |
|
#endif |
1338 |
break; |
break; |
1339 |
|
|
1340 |
case I_VOP: |
case I_VOP: |
1341 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1342 |
printf("I_VOP Time=%ld\n",dec->time); |
#ifdef BFRAMES_DEC |
1343 |
DEBUG1("I_VOP Time=", dec->time); |
DPRINTF(DPRINTF_DEBUG, "I_VOP Time=%d", dec->time); |
1344 |
|
#endif |
1345 |
break; |
break; |
1346 |
|
|
1347 |
case B_VOP: |
case B_VOP: |
1348 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1349 |
if (dec->time_pp > dec->time_bp) { |
if (dec->time_pp > dec->time_bp) { |
1350 |
printf("I_VOP Time=%ld\n",dec->time); |
DPRINTF(DPRINTF_DEBUG, "B_VOP Time=%d", dec->time); |
|
DEBUG1("B_VOP Time=", dec->time); |
|
1351 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1352 |
} else { |
} else { |
1353 |
DEBUG("broken B-frame!"); |
DPRINTF(DPRINTF_DEBUG, "Broken B_VOP"); |
1354 |
} |
} |
1355 |
#else |
#else |
|
printf("Che minchia ne so\n"); |
|
1356 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1357 |
#endif |
#endif |
1358 |
break; |
break; |
1360 |
case N_VOP: // vop not coded |
case N_VOP: // vop not coded |
1361 |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
1362 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
|
printf("N_VOP vop not coded\n"); |
|
1363 |
break; |
break; |
1364 |
|
|
1365 |
default: |
default: |
1366 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
1367 |
} |
} |
1368 |
|
|
1369 |
|
#ifdef BFRAMES_DEC_DEBUG |
1370 |
|
if (frame->length != BitstreamPos(&bs) / 8){ |
1371 |
|
DPRINTF(DPRINTF_DEBUG, "InLen: %d / UseLen: %d", frame->length, BitstreamPos(&bs) / 8); |
1372 |
|
} |
1373 |
|
#endif |
1374 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1375 |
|
|
1376 |
|
|
1377 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1378 |
// test if no B_VOP |
// test if no B_VOP |
1379 |
if (dec->low_delay) { |
if (dec->low_delay || dec->frames == 0) { |
1380 |
#endif |
#endif |
1381 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1382 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1383 |
|
|
1384 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1385 |
} else { |
} else { |
1386 |
if (dec->frames >= 1) { |
if (dec->frames >= 1) { |
1402 |
if (vop_type == I_VOP || vop_type == P_VOP) { |
if (vop_type == I_VOP || vop_type == P_VOP) { |
1403 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1404 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1405 |
|
|
1406 |
// swap MACROBLOCK |
// swap MACROBLOCK |
1407 |
if (dec->low_delay && vop_type == P_VOP) |
// the Divx will not set the low_delay flage some times |
1408 |
|
// so follow code will wrong to not swap at that time |
1409 |
|
// this will broken bitstream! so I'm change it, |
1410 |
|
// But that is not the best way! can anyone tell me how |
1411 |
|
// to do another way? |
1412 |
|
// 18-07-2002 MinChen<chenm001@163.com> |
1413 |
|
//if (!dec->low_delay && vop_type == P_VOP) |
1414 |
|
if (vop_type == P_VOP) |
1415 |
mb_swap(&dec->mbs, &dec->last_mbs); |
mb_swap(&dec->mbs, &dec->last_mbs); |
1416 |
} |
} |
1417 |
|
|