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.84 2010-11-12 10:10:40 Isibaar 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 |
|
if (create->fourcc == ((int)('X')|((int)('V')<<8)| |
201 |
|
((int)('I')<<16)|((int)('D')<<24))) { /* XVID */ |
202 |
|
dec->bs_version = 0; /* Initially assume oldest xvid version */ |
203 |
|
} |
204 |
|
else { |
205 |
|
dec->bs_version = 0xffff; /* Initialize to very high value -> assume bugfree stream */ |
206 |
|
} |
207 |
|
|
208 |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
209 |
|
|
210 |
if (dec->fixed_dimensions) |
if (dec->fixed_dimensions) { |
211 |
return decoder_resize(dec); |
int ret = decoder_resize(dec); |
212 |
|
if (ret == XVID_ERR_MEMORY) create->handle = NULL; |
213 |
|
return ret; |
214 |
|
} |
215 |
else |
else |
216 |
return 0; |
return 0; |
217 |
} |
} |
328 |
stop_iquant_timer(); |
stop_iquant_timer(); |
329 |
|
|
330 |
start_timer(); |
start_timer(); |
331 |
idct(&data[i * 64]); |
idct((short * const)&data[i * 64]); |
332 |
stop_idct_timer(); |
stop_idct_timer(); |
333 |
|
|
334 |
} |
} |
418 |
|
|
419 |
/* iDCT */ |
/* iDCT */ |
420 |
start_timer(); |
start_timer(); |
421 |
idct(&data[0]); |
idct((short * const)&data[0]); |
422 |
stop_idct_timer(); |
stop_idct_timer(); |
423 |
|
|
424 |
/* Add this residual to the predicted block */ |
/* Add this residual to the predicted block */ |
478 |
const uint32_t cbp, |
const uint32_t cbp, |
479 |
Bitstream * bs, |
Bitstream * bs, |
480 |
const uint32_t rounding, |
const uint32_t rounding, |
481 |
const int ref) |
const int ref, |
482 |
|
const int bvop) |
483 |
{ |
{ |
484 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
485 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
500 |
|
|
501 |
start_timer(); |
start_timer(); |
502 |
|
|
503 |
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 */ |
504 |
|
|
505 |
uv_dx = mv[0].x; |
uv_dx = mv[0].x; |
506 |
uv_dy = mv[0].y; |
uv_dy = mv[0].y; |
594 |
const uint32_t cbp, |
const uint32_t cbp, |
595 |
Bitstream * bs, |
Bitstream * bs, |
596 |
const uint32_t rounding, |
const uint32_t rounding, |
597 |
const int ref) |
const int ref, |
598 |
|
const int bvop) |
599 |
{ |
{ |
600 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
601 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
619 |
|
|
620 |
start_timer(); |
start_timer(); |
621 |
|
|
622 |
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 */ |
623 |
{ |
{ |
624 |
/* Prepare top field vector */ |
/* Prepare top field vector */ |
625 |
uvtop_dx = DIV2ROUND(mv[0].x); |
uvtop_dx = DIV2ROUND(mv[0].x); |
757 |
bound = read_video_packet_header(bs, dec, 0, |
bound = read_video_packet_header(bs, dec, 0, |
758 |
&quant, NULL, NULL, &intra_dc_threshold); |
&quant, NULL, NULL, &intra_dc_threshold); |
759 |
x = bound % mb_width; |
x = bound % mb_width; |
760 |
y = bound / mb_width; |
y = MIN((bound / mb_width), (mb_height-1)); |
761 |
} |
} |
762 |
mb = &dec->mbs[y * dec->mb_width + x]; |
mb = &dec->mbs[y * dec->mb_width + x]; |
763 |
|
|
984 |
bound = read_video_packet_header(bs, dec, fcode - 1, |
bound = read_video_packet_header(bs, dec, fcode - 1, |
985 |
&quant, &fcode, NULL, &intra_dc_threshold); |
&quant, &fcode, NULL, &intra_dc_threshold); |
986 |
x = bound % mb_width; |
x = bound % mb_width; |
987 |
y = bound / mb_width; |
y = MIN((bound / mb_width), (mb_height-1)); |
988 |
} |
} |
989 |
mb = &dec->mbs[y * dec->mb_width + x]; |
mb = &dec->mbs[y * dec->mb_width + x]; |
990 |
|
|
1077 |
|
|
1078 |
/* See how to decode */ |
/* See how to decode */ |
1079 |
if(!mb->field_pred) |
if(!mb->field_pred) |
1080 |
decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0); |
decoder_mbinter(dec, mb, x, y, cbp, bs, rounding, 0, 0); |
1081 |
else |
else |
1082 |
decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0); |
decoder_mbinter_field(dec, mb, x, y, cbp, bs, rounding, 0, 0); |
1083 |
|
|
1084 |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
1085 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
1100 |
mb->field_pred=0; /* (!) */ |
mb->field_pred=0; /* (!) */ |
1101 |
|
|
1102 |
decoder_mbinter(dec, mb, x, y, 0, bs, |
decoder_mbinter(dec, mb, x, y, 0, bs, |
1103 |
rounding, 0); |
rounding, 0, 0); |
1104 |
|
|
1105 |
if(dec->out_frm && cp_mb > 0) { |
if(dec->out_frm && cp_mb > 0) { |
1106 |
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); |
1202 |
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]; |
1203 |
|
|
1204 |
} else { |
} else { |
1205 |
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. */ |
1206 |
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) { |
|
1207 |
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
1208 |
uv_dx = (uv_dx>>1) | (uv_dx&1); |
int z; |
1209 |
uv_dy = (uv_dy>>1) | (uv_dy&1); |
uv_dx = 0; uv_dy = 0; |
1210 |
b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1); |
b_uv_dx = 0; b_uv_dy = 0; |
1211 |
b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1); |
for (z = 0; z < 4; z++) { |
1212 |
|
uv_dx += ((pMB->mvs[z].x>>1) | (pMB->mvs[z].x&1)); |
1213 |
|
uv_dy += ((pMB->mvs[z].y>>1) | (pMB->mvs[z].y&1)); |
1214 |
|
b_uv_dx += ((pMB->b_mvs[z].x>>1) | (pMB->b_mvs[z].x&1)); |
1215 |
|
b_uv_dy += ((pMB->b_mvs[z].y>>1) | (pMB->b_mvs[z].y&1)); |
1216 |
|
} |
1217 |
} |
} |
1218 |
else { |
else { |
1219 |
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); |
1220 |
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); |
1221 |
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); |
1222 |
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); |
1223 |
} |
} |
1224 |
|
} else { |
1225 |
|
uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1226 |
|
uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1227 |
|
b_uv_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
1228 |
|
b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
1229 |
} |
} |
1230 |
|
|
1231 |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
1344 |
return -1; |
return -1; |
1345 |
} |
} |
1346 |
|
|
1347 |
|
static int __inline get_resync_len_b(const int fcode_backward, |
1348 |
|
const int fcode_forward) { |
1349 |
|
int resync_len = ((fcode_forward>fcode_backward) ? fcode_forward : fcode_backward) - 1; |
1350 |
|
if (resync_len < 1) resync_len = 1; |
1351 |
|
return resync_len; |
1352 |
|
} |
1353 |
|
|
1354 |
static void |
static void |
1355 |
decoder_bframe(DECODER * dec, |
decoder_bframe(DECODER * dec, |
1356 |
Bitstream * bs, |
Bitstream * bs, |
1362 |
VECTOR mv; |
VECTOR mv; |
1363 |
const VECTOR zeromv = {0,0}; |
const VECTOR zeromv = {0,0}; |
1364 |
int i; |
int i; |
1365 |
|
int resync_len; |
1366 |
|
|
1367 |
if (!dec->is_edged[0]) { |
if (!dec->is_edged[0]) { |
1368 |
start_timer(); |
start_timer(); |
1380 |
stop_edges_timer(); |
stop_edges_timer(); |
1381 |
} |
} |
1382 |
|
|
1383 |
|
resync_len = get_resync_len_b(fcode_backward, fcode_forward); |
1384 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1385 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1386 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1387 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1388 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1389 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1390 |
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 */ |
|
1391 |
|
|
1392 |
if (check_resync_marker(bs, fcode_max - 1)) { |
if (check_resync_marker(bs, resync_len)) { |
1393 |
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
int bound = read_video_packet_header(bs, dec, resync_len, &quant, |
1394 |
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1395 |
x = bound % dec->mb_width; |
x = bound % dec->mb_width; |
1396 |
y = bound / dec->mb_width; |
y = MIN((bound / dec->mb_width), (dec->mb_height-1)); |
1397 |
/* reset predicted macroblocks */ |
/* reset predicted macroblocks */ |
1398 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1399 |
|
/* update resync len with new fcodes */ |
1400 |
|
resync_len = get_resync_len_b(fcode_backward, fcode_forward); |
1401 |
} |
} |
1402 |
|
|
1403 |
mv = |
mv = |
1414 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1415 |
mb->cbp = 0; |
mb->cbp = 0; |
1416 |
mb->mode = MODE_FORWARD; |
mb->mode = MODE_FORWARD; |
1417 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); |
1418 |
continue; |
continue; |
1419 |
} |
} |
1420 |
|
|
1497 |
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); |
1498 |
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]; |
1499 |
|
|
1500 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
1501 |
break; |
break; |
1502 |
|
|
1503 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1504 |
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); |
1505 |
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]; |
1506 |
|
|
1507 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 1, 1); |
1508 |
break; |
break; |
1509 |
|
|
1510 |
default: |
default: |
1515 |
} |
} |
1516 |
|
|
1517 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1518 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
static void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1519 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1520 |
int coding_type, int quant) |
int coding_type, int quant) |
1521 |
{ |
{ |
1637 |
if (coding_type == -2 || coding_type == -3) { /* vol and/or resize */ |
if (coding_type == -2 || coding_type == -3) { /* vol and/or resize */ |
1638 |
|
|
1639 |
if (coding_type == -3) |
if (coding_type == -3) |
1640 |
decoder_resize(dec); |
if (decoder_resize(dec)) return XVID_ERR_MEMORY; |
1641 |
|
|
1642 |
if(stats) { |
if(stats) { |
1643 |
stats->type = XVID_TYPE_VOL; |
stats->type = XVID_TYPE_VOL; |
1660 |
goto repeat; |
goto repeat; |
1661 |
} |
} |
1662 |
|
|
1663 |
dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; /* init pred vector to 0 */ |
dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.x = dec->p_fmv.y = 0; /* init pred vector to 0 */ |
1664 |
|
|
1665 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1666 |
if (dec->packed_mode && coding_type == N_VOP) { |
if (dec->packed_mode && coding_type == N_VOP) { |