84 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
85 |
#include "utils/timer.h" |
#include "utils/timer.h" |
86 |
#include "utils/emms.h" |
#include "utils/emms.h" |
87 |
|
#include "motion/motion.h" |
88 |
|
|
89 |
#include "image/image.h" |
#include "image/image.h" |
90 |
#include "image/colorspace.h" |
#include "image/colorspace.h" |
137 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
138 |
} |
} |
139 |
|
|
140 |
|
if (image_create(&dec->refh, dec->edged_width, dec->edged_height)) { |
141 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
142 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
143 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
144 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
145 |
|
xvid_free(dec); |
146 |
|
return XVID_ERR_MEMORY; |
147 |
|
} |
148 |
|
|
149 |
dec->mbs = |
dec->mbs = |
150 |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
151 |
CACHE_LINE); |
CACHE_LINE); |
154 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
155 |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
156 |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
157 |
|
image_destroy(&dec->refh, dec->edged_width, dec->edged_height); |
158 |
xvid_free(dec); |
xvid_free(dec); |
159 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
160 |
} |
} |
172 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
173 |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
174 |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
175 |
|
image_destroy(&dec->refh, dec->edged_width, dec->edged_height); |
176 |
xvid_free(dec); |
xvid_free(dec); |
177 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
178 |
} |
} |
198 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
199 |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
200 |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
201 |
|
image_destroy(&dec->refh, dec->edged_width, dec->edged_height); |
202 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
203 |
xvid_free(dec); |
xvid_free(dec); |
204 |
|
|
281 |
start_timer(); |
start_timer(); |
282 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
283 |
{ |
{ |
284 |
get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
int direction = dec->alternate_vertical_scan ? |
285 |
start_coeff); |
2 : pMB->acpred_directions[i]; |
286 |
|
|
287 |
|
get_intra_block(bs, &block[i * 64], direction, start_coeff); |
288 |
} |
} |
289 |
stop_coding_timer(); |
stop_coding_timer(); |
290 |
|
|
326 |
|
|
327 |
#define SIGN(X) (((X)>0)?1:-1) |
#define SIGN(X) (((X)>0)?1:-1) |
328 |
#define ABS(X) (((X)>0)?(X):-(X)) |
#define ABS(X) (((X)>0)?(X):-(X)) |
|
static const uint32_t roundtab[16] = |
|
|
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
|
|
|
|
329 |
|
|
330 |
// decode an inter macroblock |
// decode an inter macroblock |
331 |
|
|
362 |
|
|
363 |
if (dec->quarterpel) |
if (dec->quarterpel) |
364 |
{ |
{ |
365 |
uv_dx = (uv_dx >> 1) | (uv_dx & 1); |
uv_dx /= 2; |
366 |
uv_dy = (uv_dy >> 1) | (uv_dy & 1); |
uv_dy /= 2; |
367 |
} |
} |
368 |
|
|
369 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
370 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
371 |
|
|
372 |
|
start_timer(); |
373 |
|
if(dec->quarterpel) { |
374 |
|
interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
375 |
|
dec->refh.y + 128, 16*x_pos, 16*y_pos, |
376 |
|
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
377 |
|
} |
378 |
|
else { |
379 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, |
380 |
|
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
381 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
382 |
|
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
383 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
384 |
|
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
385 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
386 |
|
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
387 |
|
} |
388 |
|
|
389 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
390 |
|
uv_dx, uv_dy, stride2, rounding); |
391 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
392 |
|
uv_dx, uv_dy, stride2, rounding); |
393 |
|
stop_comp_timer(); |
394 |
|
|
395 |
} else { |
} else { |
396 |
int sum; |
int sum; |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
397 |
|
|
398 |
if (dec->quarterpel) |
if (dec->quarterpel) |
399 |
{ |
sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2); |
400 |
sum /= 2; |
else |
401 |
} |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
|
|
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
|
402 |
|
|
403 |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
uv_dx = (sum >> 3) + roundtab_76[sum & 0xf]; |
404 |
|
|
405 |
if (dec->quarterpel) |
if (dec->quarterpel) |
406 |
{ |
sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2); |
407 |
sum /= 2; |
else |
408 |
} |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
409 |
|
|
410 |
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
uv_dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
} |
|
411 |
|
|
412 |
start_timer(); |
start_timer(); |
413 |
if(dec->quarterpel) { |
if(dec->quarterpel) { |
414 |
DEBUG("QUARTERPEL"); |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
415 |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, |
dec->refh.y + 128, 16*x_pos, 16*y_pos, |
416 |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
417 |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
418 |
|
dec->refh.y + 128, 16*x_pos + 8, 16*y_pos, |
419 |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
420 |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
421 |
|
dec->refh.y + 128, 16*x_pos, 16*y_pos + 8, |
422 |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
423 |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
424 |
|
dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8, |
425 |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
426 |
} |
} |
427 |
else { |
else { |
440 |
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
441 |
uv_dx, uv_dy, stride2, rounding); |
uv_dx, uv_dy, stride2, rounding); |
442 |
stop_comp_timer(); |
stop_comp_timer(); |
443 |
|
} |
444 |
|
|
445 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
446 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
447 |
|
|
448 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
449 |
{ |
{ |
450 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
451 |
|
|
452 |
start_timer(); |
start_timer(); |
453 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
454 |
stop_coding_timer(); |
stop_coding_timer(); |
455 |
|
|
456 |
start_timer(); |
start_timer(); |
623 |
|
|
624 |
start_timer(); |
start_timer(); |
625 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
626 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
627 |
stop_edges_timer(); |
stop_edges_timer(); |
628 |
|
|
629 |
bound = 0; |
bound = 0; |
744 |
rounding); |
rounding); |
745 |
} else // not coded |
} else // not coded |
746 |
{ |
{ |
|
DEBUG2("P-frame MB at (X,Y)=",x,y); |
|
747 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
748 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
749 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
913 |
stop_comp_timer(); |
stop_comp_timer(); |
914 |
|
|
915 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
916 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
917 |
|
|
918 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
919 |
{ |
{ |
920 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
921 |
|
|
922 |
start_timer(); |
start_timer(); |
923 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
924 |
stop_coding_timer(); |
stop_coding_timer(); |
925 |
|
|
926 |
start_timer(); |
start_timer(); |
958 |
stop_transfer_timer(); |
stop_transfer_timer(); |
959 |
} |
} |
960 |
|
|
|
|
|
961 |
// add by MinChen <chenm001@163.com> |
// add by MinChen <chenm001@163.com> |
962 |
// decode an B-frame direct & inter macroblock |
// decode an B-frame direct & inter macroblock |
963 |
void |
void |
1065 |
interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, |
1066 |
b_uv_dx, b_uv_dy, stride2, 0); |
b_uv_dx, b_uv_dy, stride2, 0); |
1067 |
|
|
1068 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos, |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
1069 |
stride); |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
1070 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos, |
dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos, |
1071 |
stride); |
stride, 0); |
1072 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8, |
|
1073 |
stride); |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1074 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1075 |
16 * y_pos + 8, stride); |
dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1076 |
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos, |
stride, 0); |
1077 |
stride2); |
|
1078 |
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, |
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1079 |
stride2); |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1080 |
|
dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1081 |
|
stride, 0); |
1082 |
|
|
1083 |
|
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1084 |
|
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1085 |
|
dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1086 |
|
stride, 0); |
1087 |
|
|
1088 |
|
interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1089 |
|
dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1090 |
|
dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos, |
1091 |
|
stride2, 0); |
1092 |
|
|
1093 |
|
interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1094 |
|
dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1095 |
|
dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos, |
1096 |
|
stride2, 0); |
1097 |
|
|
1098 |
stop_comp_timer(); |
stop_comp_timer(); |
1099 |
|
|
1100 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
1101 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
1102 |
|
|
1103 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
1104 |
{ |
{ |
1105 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
1106 |
|
|
1107 |
start_timer(); |
start_timer(); |
1108 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
1109 |
stop_coding_timer(); |
stop_coding_timer(); |
1110 |
|
|
1111 |
start_timer(); |
start_timer(); |
1200 |
|
|
1201 |
start_timer(); |
start_timer(); |
1202 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1203 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1204 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1205 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1206 |
stop_edges_timer(); |
stop_edges_timer(); |
1207 |
|
|
1208 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1380 |
|
|
1381 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1382 |
|
|
1383 |
|
if(BitstreamShowBits(&bs, 8) == 0x7f) |
1384 |
|
return XVID_ERR_OK; |
1385 |
|
|
1386 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1387 |
// for support B-frame to reference last 2 frame |
// for support B-frame to reference last 2 frame |
1388 |
dec->frames++; |
dec->frames++; |
1424 |
case N_VOP: // vop not coded |
case N_VOP: // vop not coded |
1425 |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
// when low_delay==0, N_VOP's should interpolate between the past and future frames |
1426 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1427 |
|
#ifdef BFRAMES_DEC |
1428 |
|
DEBUG1("N_VOP Time=", dec->time); |
1429 |
|
#endif |
1430 |
break; |
break; |
1431 |
|
|
1432 |
default: |
default: |