4 |
* - Decoder Module - |
* - Decoder Module - |
5 |
* |
* |
6 |
* Copyright(C) 2002 MinChen <chenm001@163.com> |
* Copyright(C) 2002 MinChen <chenm001@163.com> |
7 |
* 2002-2003 Peter Ross <pross@xvid.org> |
* 2002-2004 Peter Ross <pross@xvid.org> |
8 |
* |
* |
9 |
* This program is free software ; you can redistribute it and/or modify |
* This program is free software ; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
393 |
const int reduced_resolution, |
const int reduced_resolution, |
394 |
const MACROBLOCK * pMB) |
const MACROBLOCK * pMB) |
395 |
{ |
{ |
|
DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE); |
|
396 |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
397 |
|
|
398 |
int stride = dec->edged_width; |
int stride = dec->edged_width; |
401 |
int i; |
int i; |
402 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
403 |
const int direction = dec->alternate_vertical_scan ? 2 : 0; |
const int direction = dec->alternate_vertical_scan ? 2 : 0; |
404 |
const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter; |
typedef void (*get_inter_block_function_t)( |
405 |
|
Bitstream * bs, |
406 |
|
int16_t * block, |
407 |
|
int direction, |
408 |
|
const int quant, |
409 |
|
const uint16_t *matrix); |
410 |
|
|
411 |
|
const get_inter_block_function_t get_inter_block = (dec->quant_type == 0) |
412 |
|
? get_inter_block_h263 |
413 |
|
: get_inter_block_mpeg; |
414 |
|
|
415 |
|
memset(&data[0], 0, 6*64*sizeof(int16_t)); /* clear */ |
416 |
|
|
417 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
418 |
|
|
419 |
if (cbp & (1 << (5 - i))) { /* coded */ |
if (cbp & (1 << (5 - i))) { /* coded */ |
420 |
|
|
|
memset(block, 0, 64 * sizeof(int16_t)); /* clear */ |
|
421 |
|
|
422 |
|
/* Decode coeffs and dequantize on the fly */ |
423 |
start_timer(); |
start_timer(); |
424 |
get_inter_block(bs, block, direction); |
get_inter_block(bs, &data[i*64], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices)); |
425 |
stop_coding_timer(); |
stop_coding_timer(); |
426 |
|
|
427 |
start_timer(); |
start_timer(); |
|
dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices); |
|
|
stop_iquant_timer(); |
|
|
|
|
|
start_timer(); |
|
428 |
idct(&data[i * 64]); |
idct(&data[i * 64]); |
429 |
stop_idct_timer(); |
stop_idct_timer(); |
430 |
} |
} |
466 |
stop_transfer_timer(); |
stop_transfer_timer(); |
467 |
} |
} |
468 |
|
|
|
static void __inline |
|
|
validate_vector(VECTOR * mv, unsigned int x_pos, unsigned int y_pos, const DECODER * dec) |
|
|
{ |
|
|
/* clip a vector to valid range |
|
|
prevents crashes if bitstream is broken |
|
|
*/ |
|
|
int shift = 5 + dec->quarterpel; |
|
|
int xborder_high = (int)(dec->mb_width - x_pos) << shift; |
|
|
int xborder_low = (-(int)x_pos-1) << shift; |
|
|
int yborder_high = (int)(dec->mb_height - y_pos) << shift; |
|
|
int yborder_low = (-(int)y_pos-1) << shift; |
|
|
|
|
|
#define CHECK_MV(mv) \ |
|
|
do { \ |
|
|
if ((mv).x > xborder_high) { \ |
|
|
DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", (mv).x, xborder_high, x_pos, y_pos); \ |
|
|
(mv).x = xborder_high; \ |
|
|
} else if ((mv).x < xborder_low) { \ |
|
|
DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", (mv).x, xborder_low, x_pos, y_pos); \ |
|
|
(mv).x = xborder_low; \ |
|
|
} \ |
|
|
if ((mv).y > yborder_high) { \ |
|
|
DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", (mv).y, yborder_high, x_pos, y_pos); \ |
|
|
(mv).y = yborder_high; \ |
|
|
} else if ((mv).y < yborder_low) { \ |
|
|
DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", (mv).y, yborder_low, x_pos, y_pos); \ |
|
|
(mv).y = yborder_low; \ |
|
|
} \ |
|
|
} while (0) |
|
|
|
|
|
CHECK_MV(mv[0]); |
|
|
CHECK_MV(mv[1]); |
|
|
CHECK_MV(mv[2]); |
|
|
CHECK_MV(mv[3]); |
|
|
} |
|
|
|
|
469 |
/* decode an inter macroblock */ |
/* decode an inter macroblock */ |
470 |
static void |
static void |
471 |
decoder_mbinter(DECODER * dec, |
decoder_mbinter(DECODER * dec, |
503 |
mv[i] = pMB->mvs[i]; |
mv[i] = pMB->mvs[i]; |
504 |
} |
} |
505 |
|
|
506 |
validate_vector(mv, x_pos, y_pos, dec); |
for (i = 0; i < 4; i++) { |
507 |
|
/* clip to valid range */ |
508 |
|
int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel); |
509 |
|
if (mv[i].x > border) { |
510 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
511 |
|
mv[i].x = border; |
512 |
|
} else { |
513 |
|
border = (-(int)x_pos-1) << (5 + dec->quarterpel); |
514 |
|
if (mv[i].x < border) { |
515 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
516 |
|
mv[i].x = border; |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
|
border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel); |
521 |
|
if (mv[i].y > border) { |
522 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
523 |
|
mv[i].y = border; |
524 |
|
} else { |
525 |
|
border = (-(int)y_pos-1) << (5 + dec->quarterpel); |
526 |
|
if (mv[i].y < border) { |
527 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
528 |
|
mv[i].y = border; |
529 |
|
} |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
start_timer(); |
start_timer(); |
534 |
|
|
821 |
mb_height = (dec->height + 31) / 32; |
mb_height = (dec->height + 31) / 32; |
822 |
} |
} |
823 |
|
|
824 |
|
if (!dec->is_edged[0]) { |
825 |
start_timer(); |
start_timer(); |
826 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
827 |
dec->width, dec->height, dec->bs_version); |
dec->width, dec->height, dec->bs_version); |
828 |
|
dec->is_edged[0] = 1; |
829 |
stop_edges_timer(); |
stop_edges_timer(); |
830 |
|
} |
831 |
|
|
832 |
if (gmc_warp) { |
if (gmc_warp) { |
833 |
/* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */ |
/* accuracy: 0==1/2, 1=1/4, 2=1/8, 3=1/16 */ |
1018 |
decoder_bf_interpolate_mbinter(DECODER * dec, |
decoder_bf_interpolate_mbinter(DECODER * dec, |
1019 |
IMAGE forward, |
IMAGE forward, |
1020 |
IMAGE backward, |
IMAGE backward, |
1021 |
MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
1022 |
const uint32_t x_pos, |
const uint32_t x_pos, |
1023 |
const uint32_t y_pos, |
const uint32_t y_pos, |
1024 |
Bitstream * bs, |
Bitstream * bs, |
1035 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
1036 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
1037 |
|
|
|
validate_vector(pMB->mvs, x_pos, y_pos, dec); |
|
|
validate_vector(pMB->b_mvs, x_pos, y_pos, dec); |
|
|
|
|
1038 |
if (!direct) { |
if (!direct) { |
1039 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
1040 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
1149 |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
1150 |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
1151 |
dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos, |
dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos, |
1152 |
stride, 0, 8); |
stride, 1, 8); |
1153 |
|
|
1154 |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1155 |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1156 |
dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
dec->tmp.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1157 |
stride, 0, 8); |
stride, 1, 8); |
1158 |
|
|
1159 |
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1160 |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1161 |
dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1162 |
stride, 0, 8); |
stride, 1, 8); |
1163 |
|
|
1164 |
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1165 |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1166 |
dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
dec->tmp.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1167 |
stride, 0, 8); |
stride, 1, 8); |
1168 |
|
|
1169 |
interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1170 |
dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1171 |
dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos, |
dec->tmp.u + (8 * y_pos * stride2) + 8 * x_pos, |
1172 |
stride2, 0, 8); |
stride2, 1, 8); |
1173 |
|
|
1174 |
interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1175 |
dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1176 |
dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos, |
dec->tmp.v + (8 * y_pos * stride2) + 8 * x_pos, |
1177 |
stride2, 0, 8); |
stride2, 1, 8); |
1178 |
|
|
1179 |
stop_comp_timer(); |
stop_comp_timer(); |
1180 |
|
|
1226 |
const VECTOR zeromv = {0,0}; |
const VECTOR zeromv = {0,0}; |
1227 |
int i; |
int i; |
1228 |
|
|
1229 |
|
if (!dec->is_edged[0]) { |
1230 |
start_timer(); |
start_timer(); |
1231 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1232 |
dec->width, dec->height, dec->bs_version); |
dec->width, dec->height, dec->bs_version); |
1233 |
|
dec->is_edged[0] = 1; |
1234 |
|
stop_edges_timer(); |
1235 |
|
} |
1236 |
|
|
1237 |
|
if (!dec->is_edged[1]) { |
1238 |
|
start_timer(); |
1239 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1240 |
dec->width, dec->height, dec->bs_version); |
dec->width, dec->height, dec->bs_version); |
1241 |
|
dec->is_edged[1] = 1; |
1242 |
stop_edges_timer(); |
stop_edges_timer(); |
1243 |
|
} |
1244 |
|
|
1245 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1246 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1379 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1380 |
int coding_type, int quant) |
int coding_type, int quant) |
1381 |
{ |
{ |
1382 |
|
const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0; |
1383 |
|
|
1384 |
if (dec->cartoon_mode) |
if (dec->cartoon_mode) |
1385 |
frame->general &= ~XVID_FILMEFFECT; |
frame->general &= ~XVID_FILMEFFECT; |
1386 |
|
|
1387 |
if (frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) && mbs != NULL) /* post process */ |
if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0) |
1388 |
|
&& mbs != NULL) /* post process */ |
1389 |
{ |
{ |
1390 |
/* note: image is stored to tmp */ |
/* note: image is stored to tmp */ |
1391 |
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
1392 |
image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, |
image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, |
1393 |
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
1394 |
frame->general, dec->frames, (coding_type == B_VOP)); |
frame->general, brightness, dec->frames, (coding_type == B_VOP)); |
1395 |
img = &dec->tmp; |
img = &dec->tmp; |
1396 |
} |
} |
1397 |
|
|
1414 |
} |
} |
1415 |
} |
} |
1416 |
|
|
1417 |
|
|
1418 |
int |
int |
1419 |
decoder_decode(DECODER * dec, |
decoder_decode(DECODER * dec, |
1420 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats) |
1565 |
} |
} |
1566 |
|
|
1567 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1568 |
|
dec->is_edged[1] = dec->is_edged[0]; |
1569 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1570 |
|
dec->is_edged[0] = 0; |
1571 |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
1572 |
dec->last_reduced_resolution = reduced_resolution; |
dec->last_reduced_resolution = reduced_resolution; |
1573 |
dec->last_coding_type = coding_type; |
dec->last_coding_type = coding_type; |