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.73 2005-09-13 12:12:15 suxen_drol Exp $ |
* $Id: decoder.c,v 1.80 2007-04-16 19:01:28 Skal Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
195 |
dec->low_delay = 0; |
dec->low_delay = 0; |
196 |
dec->packed_mode = 0; |
dec->packed_mode = 0; |
197 |
dec->time_inc_resolution = 1; /* until VOL header says otherwise */ |
dec->time_inc_resolution = 1; /* until VOL header says otherwise */ |
198 |
|
dec->ver_id = 1; |
199 |
|
|
200 |
|
dec->bs_version = 0xffff; /* Initialize to very high value -> assume bugfree stream */ |
201 |
|
|
202 |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
203 |
|
|
319 |
stop_iquant_timer(); |
stop_iquant_timer(); |
320 |
|
|
321 |
start_timer(); |
start_timer(); |
322 |
idct(&data[i * 64]); |
idct((short * const)&data[i * 64]); |
323 |
stop_idct_timer(); |
stop_idct_timer(); |
324 |
|
|
325 |
} |
} |
409 |
|
|
410 |
/* iDCT */ |
/* iDCT */ |
411 |
start_timer(); |
start_timer(); |
412 |
idct(&data[0]); |
idct((short * const)&data[0]); |
413 |
stop_idct_timer(); |
stop_idct_timer(); |
414 |
|
|
415 |
/* Add this residual to the predicted block */ |
/* Add this residual to the predicted block */ |
469 |
const uint32_t cbp, |
const uint32_t cbp, |
470 |
Bitstream * bs, |
Bitstream * bs, |
471 |
const uint32_t rounding, |
const uint32_t rounding, |
472 |
const int ref) |
const int ref, |
473 |
|
const int bvop) |
474 |
{ |
{ |
475 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
476 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
491 |
|
|
492 |
start_timer(); |
start_timer(); |
493 |
|
|
494 |
if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
if ((pMB->mode != MODE_INTER4V) || (bvop)) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
495 |
|
|
496 |
uv_dx = mv[0].x; |
uv_dx = mv[0].x; |
497 |
uv_dy = mv[0].y; |
uv_dy = mv[0].y; |
585 |
const uint32_t cbp, |
const uint32_t cbp, |
586 |
Bitstream * bs, |
Bitstream * bs, |
587 |
const uint32_t rounding, |
const uint32_t rounding, |
588 |
const int ref) |
const int ref, |
589 |
|
const int bvop) |
590 |
{ |
{ |
591 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
592 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
610 |
|
|
611 |
start_timer(); |
start_timer(); |
612 |
|
|
613 |
if(pMB->mode!=MODE_INTER4V) /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
if((pMB->mode!=MODE_INTER4V) || (bvop)) /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
614 |
{ |
{ |
615 |
/* Prepare top field vector */ |
/* Prepare top field vector */ |
616 |
uvtop_dx = DIV2ROUND(mv[0].x); |
uvtop_dx = DIV2ROUND(mv[0].x); |
1068 |
|
|
1069 |
/* See how to decode */ |
/* See how to decode */ |
1070 |
if(!mb->field_pred) |
if(!mb->field_pred) |
1071 |
decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0); |
decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0, 0); |
1072 |
else |
else |
1073 |
decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0); |
decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0, 0); |
1074 |
|
|
1075 |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
1076 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
1091 |
mb->field_pred=0; /* (!) */ |
mb->field_pred=0; /* (!) */ |
1092 |
|
|
1093 |
decoder_mbinter(dec, mb, x, y, 0, bs, |
decoder_mbinter(dec, mb, x, y, 0, bs, |
1094 |
rounding, 0); |
rounding, 0, 0); |
1095 |
|
|
1096 |
if(dec->out_frm && cp_mb > 0) { |
if(dec->out_frm && cp_mb > 0) { |
1097 |
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); |
1193 |
b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3]; |
b_uv_dy = (b_uv_dy >> 1) + roundtab_79[b_uv_dy & 0x3]; |
1194 |
|
|
1195 |
} else { |
} else { |
1196 |
uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
if (dec->quarterpel) { /* for qpel the /2 shall be done before summation. We've done it right in the encoder in the past. */ |
1197 |
uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
/* TODO: figure out if we ever did it wrong on the encoder side. If yes, add some workaround */ |
|
b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
|
|
b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
|
|
|
|
|
if (dec->quarterpel) { |
|
1198 |
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
1199 |
uv_dx = (uv_dx>>1) | (uv_dx&1); |
int z; |
1200 |
uv_dy = (uv_dy>>1) | (uv_dy&1); |
uv_dx = 0; uv_dy = 0; |
1201 |
b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1); |
b_uv_dx = 0; b_uv_dy = 0; |
1202 |
b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1); |
for (z = 0; z < 4; z++) { |
1203 |
|
uv_dx += ((pMB->mvs[z].x>>1) | (pMB->mvs[z].x&1)); |
1204 |
|
uv_dy += ((pMB->mvs[z].y>>1) | (pMB->mvs[z].y&1)); |
1205 |
|
b_uv_dx += ((pMB->b_mvs[z].x>>1) | (pMB->b_mvs[z].x&1)); |
1206 |
|
b_uv_dy += ((pMB->b_mvs[z].y>>1) | (pMB->b_mvs[z].y&1)); |
1207 |
|
} |
1208 |
} |
} |
1209 |
else { |
else { |
1210 |
uv_dx /= 2; |
uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2); |
1211 |
uv_dy /= 2; |
uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2); |
1212 |
b_uv_dx /= 2; |
b_uv_dx = (pMB->b_mvs[0].x / 2) + (pMB->b_mvs[1].x / 2) + (pMB->b_mvs[2].x / 2) + (pMB->b_mvs[3].x / 2); |
1213 |
b_uv_dy /= 2; |
b_uv_dy = (pMB->b_mvs[0].y / 2) + (pMB->b_mvs[1].y / 2) + (pMB->b_mvs[2].y / 2) + (pMB->b_mvs[3].y / 2); |
1214 |
} |
} |
1215 |
|
} else { |
1216 |
|
uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1217 |
|
uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1218 |
|
b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
1219 |
|
b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
1220 |
} |
} |
1221 |
|
|
1222 |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
1335 |
return -1; |
return -1; |
1336 |
} |
} |
1337 |
|
|
1338 |
|
static int __inline get_resync_len_b(const int fcode_backward, |
1339 |
|
const int fcode_forward) { |
1340 |
|
int resync_len = ((fcode_forward>fcode_backward) ? fcode_forward : fcode_backward) - 1; |
1341 |
|
if (resync_len < 1) resync_len = 1; |
1342 |
|
return resync_len; |
1343 |
|
} |
1344 |
|
|
1345 |
static void |
static void |
1346 |
decoder_bframe(DECODER * dec, |
decoder_bframe(DECODER * dec, |
1347 |
Bitstream * bs, |
Bitstream * bs, |
1353 |
VECTOR mv; |
VECTOR mv; |
1354 |
const VECTOR zeromv = {0,0}; |
const VECTOR zeromv = {0,0}; |
1355 |
int i; |
int i; |
1356 |
|
int resync_len; |
1357 |
|
|
1358 |
if (!dec->is_edged[0]) { |
if (!dec->is_edged[0]) { |
1359 |
start_timer(); |
start_timer(); |
1371 |
stop_edges_timer(); |
stop_edges_timer(); |
1372 |
} |
} |
1373 |
|
|
1374 |
|
resync_len = get_resync_len_b(fcode_backward, fcode_forward); |
1375 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1376 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1377 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1378 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1379 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1380 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1381 |
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
int intra_dc_threshold; /* fake variable */ |
|
uint32_t intra_dc_threshold; /* fake variable */ |
|
1382 |
|
|
1383 |
if (check_resync_marker(bs, fcode_max - 1)) { |
if (check_resync_marker(bs, resync_len)) { |
1384 |
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
int bound = read_video_packet_header(bs, dec, resync_len, &quant, |
1385 |
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1386 |
x = bound % dec->mb_width; |
x = bound % dec->mb_width; |
1387 |
y = bound / dec->mb_width; |
y = bound / dec->mb_width; |
1388 |
/* reset predicted macroblocks */ |
/* reset predicted macroblocks */ |
1389 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1390 |
|
/* update resync len with new fcodes */ |
1391 |
|
resync_len = get_resync_len_b(fcode_backward, fcode_forward); |
1392 |
} |
} |
1393 |
|
|
1394 |
mv = |
mv = |
1405 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1406 |
mb->cbp = 0; |
mb->cbp = 0; |
1407 |
mb->mode = MODE_FORWARD; |
mb->mode = MODE_FORWARD; |
1408 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); |
1409 |
continue; |
continue; |
1410 |
} |
} |
1411 |
|
|
1488 |
get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y); |
get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y); |
1489 |
dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1490 |
|
|
1491 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
1492 |
break; |
break; |
1493 |
|
|
1494 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1495 |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); |
1496 |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1497 |
|
|
1498 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); |
1499 |
break; |
break; |
1500 |
|
|
1501 |
default: |
default: |
1506 |
} |
} |
1507 |
|
|
1508 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1509 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
static void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1510 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1511 |
int coding_type, int quant) |
int coding_type, int quant) |
1512 |
{ |
{ |
1628 |
if (coding_type == -2 || coding_type == -3) { /* vol and/or resize */ |
if (coding_type == -2 || coding_type == -3) { /* vol and/or resize */ |
1629 |
|
|
1630 |
if (coding_type == -3) |
if (coding_type == -3) |
1631 |
decoder_resize(dec); |
if (decoder_resize(dec)) return XVID_ERR_MEMORY; |
1632 |
|
|
1633 |
if(stats) { |
if(stats) { |
1634 |
stats->type = XVID_TYPE_VOL; |
stats->type = XVID_TYPE_VOL; |