55 |
* 22.12.2001 lock based interpolation |
* 22.12.2001 lock based interpolation |
56 |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
57 |
* |
* |
58 |
* $Id: decoder.c,v 1.36 2002-08-03 05:36:18 chenm001 Exp $ |
* $Id: decoder.c,v 1.37.2.6 2002-10-30 18:01:48 Isibaar Exp $ |
59 |
* |
* |
60 |
*************************************************************************/ |
*************************************************************************/ |
61 |
|
|
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 = (uv_dx >> 2) + roundtab_78[uv_dx & 0x7]; |
366 |
uv_dy = (uv_dy >> 1) | (uv_dy & 1); |
uv_dy = (uv_dy >> 2) + roundtab_78[uv_dy & 0x7]; |
367 |
|
} |
368 |
|
else { |
369 |
|
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
370 |
|
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
371 |
} |
} |
372 |
|
|
373 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
start_timer(); |
374 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
if(dec->quarterpel) { |
375 |
|
interpolate16x16_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
376 |
|
dec->refh.y + 128, 16*x_pos, 16*y_pos, |
377 |
|
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
378 |
|
} |
379 |
|
else { |
380 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, |
381 |
|
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
382 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
383 |
|
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
384 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
385 |
|
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
386 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
387 |
|
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
388 |
|
} |
389 |
|
|
390 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
391 |
|
uv_dx, uv_dy, stride2, rounding); |
392 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, |
393 |
|
uv_dx, uv_dy, stride2, rounding); |
394 |
|
stop_comp_timer(); |
395 |
|
|
396 |
} else { |
} else { |
397 |
int sum; |
int sum; |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
|
398 |
|
|
399 |
if (dec->quarterpel) |
if (dec->quarterpel) |
400 |
{ |
sum = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2); |
401 |
sum /= 2; |
else |
402 |
} |
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)); |
|
403 |
|
|
404 |
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]; |
405 |
|
|
406 |
if (dec->quarterpel) |
if (dec->quarterpel) |
407 |
{ |
sum = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 2); |
408 |
sum /= 2; |
else |
409 |
} |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
410 |
|
|
411 |
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
uv_dy = (sum >> 3) + roundtab_76[sum & 0xf]; |
|
} |
|
412 |
|
|
413 |
start_timer(); |
start_timer(); |
414 |
if(dec->quarterpel) { |
if(dec->quarterpel) { |
415 |
DEBUG("QUARTERPEL"); |
interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, dec->refh.y, dec->refh.y + 64, |
416 |
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, |
417 |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
418 |
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, |
419 |
|
dec->refh.y + 128, 16*x_pos + 8, 16*y_pos, |
420 |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
421 |
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, |
422 |
|
dec->refh.y + 128, 16*x_pos, 16*y_pos + 8, |
423 |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
424 |
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, |
425 |
|
dec->refh.y + 128, 16*x_pos + 8, 16*y_pos + 8, |
426 |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
427 |
} |
} |
428 |
else { |
else { |
441 |
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, |
442 |
uv_dx, uv_dy, stride2, rounding); |
uv_dx, uv_dy, stride2, rounding); |
443 |
stop_comp_timer(); |
stop_comp_timer(); |
444 |
|
} |
445 |
|
|
446 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
447 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
448 |
|
|
449 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
450 |
{ |
{ |
451 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
452 |
|
|
453 |
start_timer(); |
start_timer(); |
454 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
455 |
stop_coding_timer(); |
stop_coding_timer(); |
456 |
|
|
457 |
start_timer(); |
start_timer(); |
624 |
|
|
625 |
start_timer(); |
start_timer(); |
626 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
627 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
628 |
stop_edges_timer(); |
stop_edges_timer(); |
629 |
|
|
630 |
bound = 0; |
bound = 0; |
692 |
mb->quant = quant; |
mb->quant = quant; |
693 |
|
|
694 |
if (dec->interlacing) { |
if (dec->interlacing) { |
695 |
|
if (cbp || intra) { |
696 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
697 |
DEBUG1("decp: field_dct: ", mb->field_dct); |
DEBUG1("decp: field_dct: ", mb->field_dct); |
698 |
|
} |
699 |
|
|
700 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
701 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
745 |
rounding); |
rounding); |
746 |
} else // not coded |
} else // not coded |
747 |
{ |
{ |
|
DEBUG2("P-frame MB at (X,Y)=",x,y); |
|
748 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
749 |
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; |
750 |
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; |
914 |
stop_comp_timer(); |
stop_comp_timer(); |
915 |
|
|
916 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
917 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
918 |
|
|
919 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
920 |
{ |
{ |
921 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
922 |
|
|
923 |
start_timer(); |
start_timer(); |
924 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
925 |
stop_coding_timer(); |
stop_coding_timer(); |
926 |
|
|
927 |
start_timer(); |
start_timer(); |
959 |
stop_transfer_timer(); |
stop_transfer_timer(); |
960 |
} |
} |
961 |
|
|
|
|
|
962 |
// add by MinChen <chenm001@163.com> |
// add by MinChen <chenm001@163.com> |
963 |
// decode an B-frame direct & inter macroblock |
// decode an B-frame direct & inter macroblock |
964 |
void |
void |
1066 |
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, |
1067 |
b_uv_dx, b_uv_dy, stride2, 0); |
b_uv_dx, b_uv_dy, stride2, 0); |
1068 |
|
|
1069 |
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, |
1070 |
stride); |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos, |
1071 |
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, |
1072 |
stride); |
stride, 0); |
1073 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8, |
|
1074 |
stride); |
interpolate8x8_avg2(dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1075 |
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, |
dec->cur.y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1076 |
16 * y_pos + 8, stride); |
dec->refn[2].y + (16 * y_pos * stride) + 16 * x_pos + 8, |
1077 |
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos, |
stride, 0); |
1078 |
stride2); |
|
1079 |
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, |
1080 |
stride2); |
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1081 |
|
dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos, |
1082 |
|
stride, 0); |
1083 |
|
|
1084 |
|
interpolate8x8_avg2(dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1085 |
|
dec->cur.y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1086 |
|
dec->refn[2].y + ((16 * y_pos + 8) * stride) + 16 * x_pos + 8, |
1087 |
|
stride, 0); |
1088 |
|
|
1089 |
|
interpolate8x8_avg2(dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1090 |
|
dec->cur.u + (8 * y_pos * stride2) + 8 * x_pos, |
1091 |
|
dec->refn[2].u + (8 * y_pos * stride2) + 8 * x_pos, |
1092 |
|
stride2, 0); |
1093 |
|
|
1094 |
|
interpolate8x8_avg2(dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1095 |
|
dec->cur.v + (8 * y_pos * stride2) + 8 * x_pos, |
1096 |
|
dec->refn[2].v + (8 * y_pos * stride2) + 8 * x_pos, |
1097 |
|
stride2, 0); |
1098 |
|
|
1099 |
stop_comp_timer(); |
stop_comp_timer(); |
1100 |
|
|
1101 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
1102 |
|
int direction = dec->alternate_vertical_scan ? 2 : 0; |
1103 |
|
|
1104 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) // coded |
1105 |
{ |
{ |
1106 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
1107 |
|
|
1108 |
start_timer(); |
start_timer(); |
1109 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64], direction); |
1110 |
stop_coding_timer(); |
stop_coding_timer(); |
1111 |
|
|
1112 |
start_timer(); |
start_timer(); |
1201 |
|
|
1202 |
start_timer(); |
start_timer(); |
1203 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1204 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1205 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1206 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1207 |
stop_edges_timer(); |
stop_edges_timer(); |
1208 |
|
|
1209 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1381 |
|
|
1382 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1383 |
|
|
1384 |
|
if(BitstreamShowBits(&bs, 8) == 0x7f) |
1385 |
|
return XVID_ERR_OK; |
1386 |
|
|
1387 |
// add by chenm001 <chenm001@163.com> |
// add by chenm001 <chenm001@163.com> |
1388 |
// for support B-frame to reference last 2 frame |
// for support B-frame to reference last 2 frame |
1389 |
dec->frames++; |
dec->frames++; |
1425 |
case N_VOP: // vop not coded |
case N_VOP: // vop not coded |
1426 |
// 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 |
1427 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1428 |
|
#ifdef BFRAMES_DEC |
1429 |
|
DEBUG1("N_VOP Time=", dec->time); |
1430 |
|
#endif |
1431 |
break; |
break; |
1432 |
|
|
1433 |
default: |
default: |