20 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: decoder.c,v 1.49.2.18 2003-10-22 09:47:52 edgomez Exp $ |
* $Id: decoder.c,v 1.49.2.31 2004-02-03 02:56:57 syskin Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
41 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
42 |
|
|
43 |
#include "quant/quant.h" |
#include "quant/quant.h" |
44 |
|
#include "quant/quant_matrix.h" |
45 |
#include "dct/idct.h" |
#include "dct/idct.h" |
46 |
#include "dct/fdct.h" |
#include "dct/fdct.h" |
47 |
#include "utils/mem_transfer.h" |
#include "utils/mem_transfer.h" |
58 |
|
|
59 |
#include "image/image.h" |
#include "image/image.h" |
60 |
#include "image/colorspace.h" |
#include "image/colorspace.h" |
61 |
|
#include "image/postprocessing.h" |
62 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
63 |
|
|
64 |
static int |
static int |
177 |
if (dec == NULL) { |
if (dec == NULL) { |
178 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
179 |
} |
} |
180 |
|
|
181 |
memset(dec, 0, sizeof(DECODER)); |
memset(dec, 0, sizeof(DECODER)); |
182 |
|
|
183 |
|
dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
184 |
|
if (dec->mpeg_quant_matrices == NULL) { |
185 |
|
xvid_free(dec); |
186 |
|
return XVID_ERR_MEMORY; |
187 |
|
} |
188 |
|
|
189 |
create->handle = dec; |
create->handle = dec; |
190 |
|
|
191 |
dec->width = create->width; |
dec->width = create->width; |
205 |
dec->last_mbs = NULL; |
dec->last_mbs = NULL; |
206 |
|
|
207 |
init_timer(); |
init_timer(); |
208 |
|
init_postproc(&dec->postproc); |
209 |
|
init_mpeg_matrix(dec->mpeg_quant_matrices); |
210 |
|
|
211 |
/* For B-frame support (used to save reference frame's time */ |
/* For B-frame support (used to save reference frame's time */ |
212 |
dec->frames = 0; |
dec->frames = 0; |
237 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
238 |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
239 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
240 |
|
xvid_free(dec->mpeg_quant_matrices); |
241 |
xvid_free(dec); |
xvid_free(dec); |
242 |
|
|
243 |
write_timer(); |
write_timer(); |
333 |
|
|
334 |
start_timer(); |
start_timer(); |
335 |
if (dec->quant_type == 0) { |
if (dec->quant_type == 0) { |
336 |
dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); |
dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices); |
337 |
} else { |
} else { |
338 |
dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); |
dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices); |
339 |
} |
} |
340 |
stop_iquant_timer(); |
stop_iquant_timer(); |
341 |
|
|
404 |
stop_coding_timer(); |
stop_coding_timer(); |
405 |
|
|
406 |
start_timer(); |
start_timer(); |
407 |
dequant(&data[i * 64], block, iQuant); |
dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices); |
408 |
stop_iquant_timer(); |
stop_iquant_timer(); |
409 |
|
|
410 |
start_timer(); |
start_timer(); |
851 |
mb->quant = quant; |
mb->quant = quant; |
852 |
|
|
853 |
if (dec->interlacing) { |
if (dec->interlacing) { |
854 |
if ((cbp || intra) && !mcsel) { |
if (cbp || intra) { |
855 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
856 |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
857 |
} |
} |
858 |
|
|
859 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) { |
860 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
861 |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
862 |
|
|
900 |
|
|
901 |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
902 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
903 |
|
mb->quant = quant; |
904 |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
905 |
|
|
906 |
if(dec->out_frm && cp_mb > 0) { |
if(dec->out_frm && cp_mb > 0) { |
910 |
st_mb = x+1; |
st_mb = x+1; |
911 |
} else { /* not coded P_VOP macroblock */ |
} else { /* not coded P_VOP macroblock */ |
912 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
913 |
|
mb->quant = quant; |
914 |
|
|
915 |
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; |
916 |
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; |
1178 |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
1179 |
int i; |
int i; |
1180 |
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
FILE *fp; |
|
|
static char first=0; |
|
|
#define BFRAME_DEBUG |
|
|
if (!first && fp) { \ |
|
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
|
|
} |
|
|
#endif |
|
|
|
|
1181 |
start_timer(); |
start_timer(); |
1182 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1183 |
dec->width, dec->height); |
dec->width, dec->height); |
1185 |
dec->width, dec->height); |
dec->width, dec->height); |
1186 |
stop_edges_timer(); |
stop_edges_timer(); |
1187 |
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
|
|
} |
|
|
#endif |
|
|
|
|
1188 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1189 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1190 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1191 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1192 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1193 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1194 |
|
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
1195 |
|
uint32_t intra_dc_threshold; /* fake variable */ |
1196 |
|
|
1197 |
|
if (check_resync_marker(bs, fcode_max - 1)) { |
1198 |
|
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
1199 |
|
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1200 |
|
x = bound % dec->mb_width; |
1201 |
|
y = bound / dec->mb_width; |
1202 |
|
/* reset predicted macroblocks */ |
1203 |
|
dec->p_fmv = dec->p_bmv = zeromv; |
1204 |
|
} |
1205 |
|
|
1206 |
mv = |
mv = |
1207 |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
1314 |
} |
} |
1315 |
} /* End of for */ |
} /* End of for */ |
1316 |
} |
} |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
first=1; |
|
|
if (fp) |
|
|
fclose(fp); |
|
|
} |
|
|
#endif |
|
1317 |
} |
} |
1318 |
|
|
1319 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1320 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1321 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1322 |
|
int coding_type, int quant) |
1323 |
|
{ |
1324 |
|
if (dec->cartoon_mode) |
1325 |
|
frame->general &= ~XVID_FILMEFFECT; |
1326 |
|
|
1327 |
|
if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL) /* post process */ |
1328 |
{ |
{ |
1329 |
|
/* note: image is stored to tmp */ |
1330 |
|
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
1331 |
|
image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, |
1332 |
|
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
1333 |
|
frame->general, dec->frames, (coding_type == B_VOP)); |
1334 |
|
img = &dec->tmp; |
1335 |
|
} |
1336 |
|
|
1337 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1338 |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1339 |
frame->output.csp, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1377 |
/* if not decoding "low_delay/packed", and this isn't low_delay and |
/* if not decoding "low_delay/packed", and this isn't low_delay and |
1378 |
we have a reference frame, then outout the reference frame */ |
we have a reference frame, then outout the reference frame */ |
1379 |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) { |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) { |
1380 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1381 |
dec->frames = 0; |
dec->frames = 0; |
1382 |
ret = 0; |
ret = 0; |
1383 |
} else { |
} else { |
1447 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1448 |
if (dec->packed_mode && coding_type == N_VOP) { |
if (dec->packed_mode && coding_type == N_VOP) { |
1449 |
if (dec->low_delay_default && dec->frames > 0) { |
if (dec->low_delay_default && dec->frames > 0) { |
1450 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1451 |
output = 1; |
output = 1; |
1452 |
} |
} |
1453 |
/* ignore otherwise */ |
/* ignore otherwise */ |
1468 |
/* XXX: not_coded vops are not used for forward prediction */ |
/* XXX: not_coded vops are not used for forward prediction */ |
1469 |
/* we should not swap(last_mbs,mbs) */ |
/* we should not swap(last_mbs,mbs) */ |
1470 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1471 |
|
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */ |
1472 |
break; |
break; |
1473 |
} |
} |
1474 |
|
|
1481 |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
1482 |
if (!(dec->low_delay_default && dec->packed_mode)) { |
if (!(dec->low_delay_default && dec->packed_mode)) { |
1483 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1484 |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1485 |
output = 1; |
output = 1; |
1486 |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
1487 |
/* output the reference frame */ |
/* output the reference frame */ |
1488 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1489 |
output = 1; |
output = 1; |
1490 |
} |
} |
1491 |
} |
} |
1510 |
/* attemping to decode a bvop without atleast 2 reference frames */ |
/* attemping to decode a bvop without atleast 2 reference frames */ |
1511 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1512 |
"broken b-frame, mising ref frames"); |
"broken b-frame, mising ref frames"); |
1513 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1514 |
} else if (dec->time_pp <= dec->time_bp) { |
} else if (dec->time_pp <= dec->time_bp) { |
1515 |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
1516 |
decoded in vfw. */ |
decoded in vfw. */ |
1517 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1518 |
"broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); |
"broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); |
1519 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1520 |
} else { |
} else { |
1521 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1522 |
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1523 |
} |
} |
1524 |
|
|
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
|
1525 |
output = 1; |
output = 1; |
1526 |
dec->frames++; |
dec->frames++; |
1527 |
} |
} |
1541 |
if (dec->low_delay_default && output == 0) { |
if (dec->low_delay_default && output == 0) { |
1542 |
if (dec->packed_mode && seen_something) { |
if (dec->packed_mode && seen_something) { |
1543 |
/* output the recently decoded frame */ |
/* output the recently decoded frame */ |
1544 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1545 |
} else { |
} else { |
1546 |
image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128); |
image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128); |
1547 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1549 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1550 |
"bframe decoder lag"); |
"bframe decoder lag"); |
1551 |
|
|
1552 |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant); |
1553 |
if (stats) stats->type = XVID_TYPE_NOTHING; |
if (stats) stats->type = XVID_TYPE_NOTHING; |
1554 |
} |
} |
1555 |
} |
} |