73 |
|
|
74 |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
75 |
|
|
76 |
if (dec->last_mbs) |
image_null(&dec->cur); |
77 |
|
image_null(&dec->refn[0]); |
78 |
|
image_null(&dec->refn[1]); |
79 |
|
image_null(&dec->tmp); |
80 |
|
image_null(&dec->qtmp); |
81 |
|
image_null(&dec->gmc); |
82 |
|
|
83 |
|
|
84 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
|
if (dec->mbs) |
|
85 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
|
if (dec->qscale) |
|
86 |
xvid_free(dec->qscale); |
xvid_free(dec->qscale); |
87 |
|
dec->last_mbs = NULL; |
88 |
|
dec->mbs = NULL; |
89 |
|
dec->qscale = NULL; |
90 |
|
|
91 |
/* realloc */ |
/* realloc */ |
92 |
dec->mb_width = (dec->width + 15) / 16; |
dec->mb_width = (dec->width + 15) / 16; |
95 |
dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; |
dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; |
96 |
dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; |
dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; |
97 |
|
|
98 |
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { |
if ( image_create(&dec->cur, dec->edged_width, dec->edged_height) |
99 |
xvid_free(dec); |
|| image_create(&dec->refn[0], dec->edged_width, dec->edged_height) |
100 |
return XVID_ERR_MEMORY; |
|| image_create(&dec->refn[1], dec->edged_width, dec->edged_height) /* Support B-frame to reference last 2 frame */ |
101 |
} |
|| image_create(&dec->tmp, dec->edged_width, dec->edged_height) |
102 |
|
|| image_create(&dec->qtmp, dec->edged_width, dec->edged_height) |
103 |
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
|| image_create(&dec->gmc, dec->edged_width, dec->edged_height) ) |
104 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
goto memory_error; |
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
/* Support B-frame to reference last 2 frame */ |
|
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
if (image_create(&dec->tmp, dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&dec->qtmp, dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&dec->gmc, dec->edged_width, dec->edged_height)) { |
|
|
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
105 |
|
|
106 |
dec->mbs = |
dec->mbs = |
107 |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
108 |
CACHE_LINE); |
CACHE_LINE); |
109 |
if (dec->mbs == NULL) { |
if (dec->mbs == NULL) |
110 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
goto memory_error; |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
111 |
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
112 |
|
|
113 |
/* For skip MB flag */ |
/* For skip MB flag */ |
114 |
dec->last_mbs = |
dec->last_mbs = |
115 |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
116 |
CACHE_LINE); |
CACHE_LINE); |
117 |
if (dec->last_mbs == NULL) { |
if (dec->last_mbs == NULL) |
118 |
xvid_free(dec->mbs); |
goto memory_error; |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
|
|
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
|
|
xvid_free(dec); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
119 |
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
120 |
|
|
121 |
/* nothing happens if that fails */ |
/* nothing happens if that fails */ |
126 |
memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height); |
memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height); |
127 |
|
|
128 |
return 0; |
return 0; |
129 |
|
|
130 |
|
memory_error: |
131 |
|
/* Most structures were deallocated / nullifieded, so it should be safe */ |
132 |
|
/* decoder_destroy(dec) minus the write_timer */ |
133 |
|
xvid_free(dec->mbs); |
134 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
135 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
136 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
137 |
|
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
138 |
|
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
139 |
|
|
140 |
|
xvid_free(dec); |
141 |
|
return XVID_ERR_MEMORY; |
142 |
} |
} |
143 |
|
|
144 |
|
|
443 |
CHECK_MV(mv[3]); |
CHECK_MV(mv[3]); |
444 |
} |
} |
445 |
|
|
446 |
|
/* Up to this version, chroma rounding was wrong with qpel. |
447 |
|
* So we try to be backward compatible to avoid artifacts */ |
448 |
|
#define BS_VERSION_BUGGY_CHROMA_ROUNDING 1 |
449 |
|
|
450 |
/* decode an inter macroblock */ |
/* decode an inter macroblock */ |
451 |
static void |
static void |
452 |
decoder_mbinter(DECODER * dec, |
decoder_mbinter(DECODER * dec, |
482 |
uv_dx = mv[0].x; |
uv_dx = mv[0].x; |
483 |
uv_dy = mv[0].y; |
uv_dy = mv[0].y; |
484 |
if (dec->quarterpel) { |
if (dec->quarterpel) { |
485 |
|
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
486 |
|
uv_dx = (uv_dx>>1) | (uv_dx&1); |
487 |
|
uv_dy = (uv_dy>>1) | (uv_dy&1); |
488 |
|
} |
489 |
|
else { |
490 |
uv_dx /= 2; |
uv_dx /= 2; |
491 |
uv_dy /= 2; |
uv_dy /= 2; |
492 |
} |
} |
493 |
|
} |
494 |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
495 |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
496 |
|
|
505 |
} else { /* MODE_INTER4V */ |
} else { /* MODE_INTER4V */ |
506 |
|
|
507 |
if(dec->quarterpel) { |
if(dec->quarterpel) { |
508 |
|
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
509 |
|
int z; |
510 |
|
uv_dx = 0; uv_dy = 0; |
511 |
|
for (z = 0; z < 4; z++) { |
512 |
|
uv_dx += ((mv[z].x>>1) | (mv[z].x&1)); |
513 |
|
uv_dy += ((mv[z].y>>1) | (mv[z].y&1)); |
514 |
|
} |
515 |
|
} |
516 |
|
else { |
517 |
uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2); |
uv_dx = (mv[0].x / 2) + (mv[1].x / 2) + (mv[2].x / 2) + (mv[3].x / 2); |
518 |
uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2); |
uv_dy = (mv[0].y / 2) + (mv[1].y / 2) + (mv[2].y / 2) + (mv[3].y / 2); |
519 |
|
} |
520 |
} else { |
} else { |
521 |
uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x; |
uv_dx = mv[0].x + mv[1].x + mv[2].x + mv[3].x; |
522 |
uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y; |
uv_dy = mv[0].y + mv[1].y + mv[2].y + mv[3].y; |
968 |
b_uv_dy = pMB->b_mvs[0].y; |
b_uv_dy = pMB->b_mvs[0].y; |
969 |
|
|
970 |
if (dec->quarterpel) { |
if (dec->quarterpel) { |
971 |
|
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
972 |
|
uv_dx = (uv_dx>>1) | (uv_dx&1); |
973 |
|
uv_dy = (uv_dy>>1) | (uv_dy&1); |
974 |
|
b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1); |
975 |
|
b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1); |
976 |
|
} |
977 |
|
else { |
978 |
uv_dx /= 2; |
uv_dx /= 2; |
979 |
uv_dy /= 2; |
uv_dy /= 2; |
980 |
b_uv_dx /= 2; |
b_uv_dx /= 2; |
981 |
b_uv_dy /= 2; |
b_uv_dy /= 2; |
982 |
} |
} |
983 |
|
} |
984 |
|
|
985 |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
986 |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
994 |
b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
b_uv_dy = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
995 |
|
|
996 |
if (dec->quarterpel) { |
if (dec->quarterpel) { |
997 |
|
if (dec->bs_version <= BS_VERSION_BUGGY_CHROMA_ROUNDING) { |
998 |
|
uv_dx = (uv_dx>>1) | (uv_dx&1); |
999 |
|
uv_dy = (uv_dy>>1) | (uv_dy&1); |
1000 |
|
b_uv_dx = (b_uv_dx>>1) | (b_uv_dx&1); |
1001 |
|
b_uv_dy = (b_uv_dy>>1) | (b_uv_dy&1); |
1002 |
|
} |
1003 |
|
else { |
1004 |
uv_dx /= 2; |
uv_dx /= 2; |
1005 |
uv_dy /= 2; |
uv_dy /= 2; |
1006 |
b_uv_dx /= 2; |
b_uv_dx /= 2; |
1007 |
b_uv_dy /= 2; |
b_uv_dy /= 2; |
1008 |
} |
} |
1009 |
|
} |
1010 |
|
|
1011 |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
1012 |
uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf]; |
uv_dy = (uv_dy >> 3) + roundtab_76[uv_dy & 0xf]; |
1159 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1160 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1161 |
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
1162 |
uint32_t intra_dc_threshold; /* fake variable */ |
int32_t intra_dc_threshold; /* fake variable */ |
1163 |
|
|
1164 |
if (check_resync_marker(bs, fcode_max - 1)) { |
if (check_resync_marker(bs, fcode_max - 1)) { |
1165 |
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |