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.17 2003-10-16 12:16:00 syskin Exp $ |
* $Id: decoder.c,v 1.49.2.22 2003-12-10 01:01:07 Isibaar 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" |
176 |
if (dec == NULL) { |
if (dec == NULL) { |
177 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
178 |
} |
} |
179 |
|
|
180 |
memset(dec, 0, sizeof(DECODER)); |
memset(dec, 0, sizeof(DECODER)); |
181 |
|
|
182 |
|
dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
183 |
|
if (dec->mpeg_quant_matrices == NULL) { |
184 |
|
xvid_free(dec); |
185 |
|
return XVID_ERR_MEMORY; |
186 |
|
} |
187 |
|
|
188 |
create->handle = dec; |
create->handle = dec; |
189 |
|
|
190 |
dec->width = create->width; |
dec->width = create->width; |
204 |
dec->last_mbs = NULL; |
dec->last_mbs = NULL; |
205 |
|
|
206 |
init_timer(); |
init_timer(); |
207 |
|
init_mpeg_matrix(dec->mpeg_quant_matrices); |
208 |
|
|
209 |
/* For B-frame support (used to save reference frame's time */ |
/* For B-frame support (used to save reference frame's time */ |
210 |
dec->frames = 0; |
dec->frames = 0; |
235 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
236 |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
237 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
238 |
|
xvid_free(dec->mpeg_quant_matrices); |
239 |
xvid_free(dec); |
xvid_free(dec); |
240 |
|
|
241 |
write_timer(); |
write_timer(); |
331 |
|
|
332 |
start_timer(); |
start_timer(); |
333 |
if (dec->quant_type == 0) { |
if (dec->quant_type == 0) { |
334 |
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); |
335 |
} else { |
} else { |
336 |
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); |
337 |
} |
} |
338 |
stop_iquant_timer(); |
stop_iquant_timer(); |
339 |
|
|
383 |
DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE); |
384 |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
385 |
|
|
386 |
int stride = dec->edged_width, next_block = stride * (reduced_resolution ? 16 : 8); |
int stride = dec->edged_width; |
387 |
|
int next_block = stride * (reduced_resolution ? 16 : 8); |
388 |
const int stride2 = stride/2; |
const int stride2 = stride/2; |
389 |
int i; |
int i; |
390 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
402 |
stop_coding_timer(); |
stop_coding_timer(); |
403 |
|
|
404 |
start_timer(); |
start_timer(); |
405 |
dequant(&data[i * 64], block, iQuant); |
dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices); |
406 |
stop_iquant_timer(); |
stop_iquant_timer(); |
407 |
|
|
408 |
start_timer(); |
start_timer(); |
461 |
{ |
{ |
462 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
463 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
|
uint32_t next_block = stride * (reduced_resolution ? 16 : 8); |
|
464 |
uint32_t i; |
uint32_t i; |
465 |
|
|
466 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
973 |
{ |
{ |
974 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
975 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
|
uint32_t next_block = stride * 8; |
|
976 |
int uv_dx, uv_dy; |
int uv_dx, uv_dy; |
977 |
int b_uv_dx, b_uv_dy; |
int b_uv_dx, b_uv_dy; |
978 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
1174 |
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; |
1175 |
int i; |
int i; |
1176 |
|
|
|
#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 |
|
|
|
|
1177 |
start_timer(); |
start_timer(); |
1178 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1179 |
dec->width, dec->height); |
dec->width, dec->height); |
1181 |
dec->width, dec->height); |
dec->width, dec->height); |
1182 |
stop_edges_timer(); |
stop_edges_timer(); |
1183 |
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
|
|
} |
|
|
#endif |
|
|
|
|
1184 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1185 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1186 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1187 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1188 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1189 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1190 |
|
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
1191 |
|
uint32_t intra_dc_threshold; /* fake variable */ |
1192 |
|
|
1193 |
|
if (check_resync_marker(bs, fcode_max - 1)) { |
1194 |
|
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
1195 |
|
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1196 |
|
x = bound % dec->mb_width; |
1197 |
|
y = bound / dec->mb_width; |
1198 |
|
/* reset predicted macroblocks */ |
1199 |
|
dec->p_fmv = dec->p_bmv = zeromv; |
1200 |
|
} |
1201 |
|
|
1202 |
mv = |
mv = |
1203 |
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] = |
1310 |
} |
} |
1311 |
} /* End of for */ |
} /* End of for */ |
1312 |
} |
} |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
first=1; |
|
|
if (fp) |
|
|
fclose(fp); |
|
|
} |
|
|
#endif |
|
1313 |
} |
} |
1314 |
|
|
1315 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1316 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1317 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
1318 |
{ |
{ |
1319 |
|
if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV)) /* post process */ |
1320 |
|
{ |
1321 |
|
/* note: image is stored to tmp */ |
1322 |
|
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
1323 |
|
image_deblock(&dec->tmp, dec->edged_width, |
1324 |
|
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
1325 |
|
frame->general); |
1326 |
|
img = &dec->tmp; |
1327 |
|
} |
1328 |
|
|
1329 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1330 |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1331 |
frame->output.csp, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1501 |
/* attemping to decode a bvop without atleast 2 reference frames */ |
/* attemping to decode a bvop without atleast 2 reference frames */ |
1502 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1503 |
"broken b-frame, mising ref frames"); |
"broken b-frame, mising ref frames"); |
1504 |
|
stats->type = XVID_TYPE_NOTHING; |
1505 |
} else if (dec->time_pp <= dec->time_bp) { |
} else if (dec->time_pp <= dec->time_bp) { |
1506 |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
1507 |
decoded in vfw. */ |
decoded in vfw. */ |
1508 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1509 |
"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); |
1510 |
|
stats->type = XVID_TYPE_NOTHING; |
1511 |
} else { |
} else { |
1512 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1513 |
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
1514 |
} |
} |
1515 |
|
|
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
|
1516 |
output = 1; |
output = 1; |
1517 |
dec->frames++; |
dec->frames++; |
1518 |
} |
} |