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 |
41 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
42 |
|
|
43 |
#include "quant/quant.h" |
#include "quant/quant.h" |
44 |
|
#include "quant/quant_matrix.h" |
45 |
#include "dct/idct.h" |
#include "dct/idct.h" |
46 |
#include "dct/fdct.h" |
#include "dct/fdct.h" |
47 |
#include "utils/mem_transfer.h" |
#include "utils/mem_transfer.h" |
48 |
#include "image/interpolate8x8.h" |
#include "image/interpolate8x8.h" |
49 |
#include "image/reduced.h" |
#include "image/reduced.h" |
50 |
#include "image/font.h" |
#include "image/font.h" |
51 |
|
#include "image/qpel.h" |
52 |
|
|
53 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
54 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
59 |
|
|
60 |
#include "image/image.h" |
#include "image/image.h" |
61 |
#include "image/colorspace.h" |
#include "image/colorspace.h" |
62 |
|
#include "image/postprocessing.h" |
63 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
64 |
|
|
65 |
|
#ifdef ARCH_IS_IA32 |
66 |
|
#define interpolate16x16_quarterpel new_interpolate16x16_quarterpel |
67 |
|
#define interpolate8x8_quarterpel new_interpolate8x8_quarterpel |
68 |
|
#endif |
69 |
|
|
70 |
static int |
static int |
71 |
decoder_resize(DECODER * dec) |
decoder_resize(DECODER * dec) |
72 |
{ |
{ |
83 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
84 |
if (dec->mbs) |
if (dec->mbs) |
85 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
86 |
|
if (dec->qscale) |
87 |
|
xvid_free(dec->qscale); |
88 |
|
|
89 |
/* realloc */ |
/* realloc */ |
90 |
dec->mb_width = (dec->width + 15) / 16; |
dec->mb_width = (dec->width + 15) / 16; |
169 |
|
|
170 |
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); |
171 |
|
|
172 |
|
/* nothing happens if that fails */ |
173 |
|
dec->qscale = |
174 |
|
xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE); |
175 |
|
|
176 |
|
if (dec->qscale) |
177 |
|
memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height); |
178 |
|
|
179 |
return 0; |
return 0; |
180 |
} |
} |
181 |
|
|
192 |
if (dec == NULL) { |
if (dec == NULL) { |
193 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
194 |
} |
} |
195 |
|
|
196 |
memset(dec, 0, sizeof(DECODER)); |
memset(dec, 0, sizeof(DECODER)); |
197 |
|
|
198 |
|
dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
199 |
|
if (dec->mpeg_quant_matrices == NULL) { |
200 |
|
xvid_free(dec); |
201 |
|
return XVID_ERR_MEMORY; |
202 |
|
} |
203 |
|
|
204 |
create->handle = dec; |
create->handle = dec; |
205 |
|
|
206 |
dec->width = create->width; |
dec->width = create->width; |
215 |
/* image based GMC */ |
/* image based GMC */ |
216 |
image_null(&dec->gmc); |
image_null(&dec->gmc); |
217 |
|
|
|
|
|
218 |
dec->mbs = NULL; |
dec->mbs = NULL; |
219 |
dec->last_mbs = NULL; |
dec->last_mbs = NULL; |
220 |
|
dec->qscale = NULL; |
221 |
|
|
222 |
init_timer(); |
init_timer(); |
223 |
|
init_postproc(&dec->postproc); |
224 |
|
init_mpeg_matrix(dec->mpeg_quant_matrices); |
225 |
|
|
226 |
/* For B-frame support (used to save reference frame's time */ |
/* For B-frame support (used to save reference frame's time */ |
227 |
dec->frames = 0; |
dec->frames = 0; |
228 |
dec->time = dec->time_base = dec->last_time_base = 0; |
dec->time = dec->time_base = dec->last_time_base = 0; |
229 |
dec->low_delay = 0; |
dec->low_delay = 0; |
230 |
dec->packed_mode = 0; |
dec->packed_mode = 0; |
231 |
|
dec->time_inc_resolution = 1; /* until VOL header says otherwise */ |
232 |
|
|
233 |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
234 |
|
|
244 |
{ |
{ |
245 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
246 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
247 |
|
xvid_free(dec->qscale); |
248 |
|
|
249 |
/* image based GMC */ |
/* image based GMC */ |
250 |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
254 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
255 |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
256 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
257 |
|
xvid_free(dec->mpeg_quant_matrices); |
258 |
xvid_free(dec); |
xvid_free(dec); |
259 |
|
|
260 |
write_timer(); |
write_timer(); |
345 |
stop_coding_timer(); |
stop_coding_timer(); |
346 |
|
|
347 |
start_timer(); |
start_timer(); |
348 |
add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); |
add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version); |
349 |
stop_prediction_timer(); |
stop_prediction_timer(); |
350 |
|
|
351 |
start_timer(); |
start_timer(); |
352 |
if (dec->quant_type == 0) { |
if (dec->quant_type == 0) { |
353 |
dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); |
dequant_h263_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices); |
354 |
} else { |
} else { |
355 |
dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); |
dequant_mpeg_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler, dec->mpeg_quant_matrices); |
356 |
} |
} |
357 |
stop_iquant_timer(); |
stop_iquant_timer(); |
358 |
|
|
399 |
const int reduced_resolution, |
const int reduced_resolution, |
400 |
const MACROBLOCK * pMB) |
const MACROBLOCK * pMB) |
401 |
{ |
{ |
|
DECLARE_ALIGNED_MATRIX(block, 1, 64, int16_t, CACHE_LINE); |
|
402 |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
403 |
|
|
404 |
int stride = dec->edged_width, next_block = stride * (reduced_resolution ? 16 : 8); |
int stride = dec->edged_width; |
405 |
|
int next_block = stride * (reduced_resolution ? 16 : 8); |
406 |
const int stride2 = stride/2; |
const int stride2 = stride/2; |
407 |
int i; |
int i; |
408 |
const uint32_t iQuant = pMB->quant; |
const uint32_t iQuant = pMB->quant; |
409 |
const int direction = dec->alternate_vertical_scan ? 2 : 0; |
const int direction = dec->alternate_vertical_scan ? 2 : 0; |
410 |
const quant_interFuncPtr dequant = dec->quant_type == 0 ? dequant_h263_inter : dequant_mpeg_inter; |
typedef void (*get_inter_block_function_t)( |
411 |
|
Bitstream * bs, |
412 |
|
int16_t * block, |
413 |
|
int direction, |
414 |
|
const int quant, |
415 |
|
const uint16_t *matrix); |
416 |
|
|
417 |
|
const get_inter_block_function_t get_inter_block = (dec->quant_type == 0) |
418 |
|
? get_inter_block_h263 |
419 |
|
: get_inter_block_mpeg; |
420 |
|
|
421 |
|
memset(&data[0], 0, 6*64*sizeof(int16_t)); /* clear */ |
422 |
|
|
423 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
424 |
|
|
425 |
if (cbp & (1 << (5 - i))) { /* coded */ |
if (cbp & (1 << (5 - i))) { /* coded */ |
426 |
|
|
|
memset(block, 0, 64 * sizeof(int16_t)); /* clear */ |
|
427 |
|
|
428 |
|
/* Decode coeffs and dequantize on the fly */ |
429 |
start_timer(); |
start_timer(); |
430 |
get_inter_block(bs, block, direction); |
get_inter_block(bs, &data[i*64], direction, iQuant, get_inter_matrix(dec->mpeg_quant_matrices)); |
431 |
stop_coding_timer(); |
stop_coding_timer(); |
432 |
|
|
433 |
start_timer(); |
start_timer(); |
|
dequant(&data[i * 64], block, iQuant); |
|
|
stop_iquant_timer(); |
|
|
|
|
|
start_timer(); |
|
434 |
idct(&data[i * 64]); |
idct(&data[i * 64]); |
435 |
stop_idct_timer(); |
stop_idct_timer(); |
436 |
} |
} |
486 |
{ |
{ |
487 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
488 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
|
uint32_t next_block = stride * (reduced_resolution ? 16 : 8); |
|
489 |
uint32_t i; |
uint32_t i; |
490 |
|
|
491 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
509 |
mv[i] = pMB->mvs[i]; |
mv[i] = pMB->mvs[i]; |
510 |
} |
} |
511 |
|
|
512 |
|
for (i = 0; i < 4; i++) { |
513 |
|
/* clip to valid range */ |
514 |
|
int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel); |
515 |
|
if (mv[i].x > border) { |
516 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
517 |
|
mv[i].x = border; |
518 |
|
} else { |
519 |
|
border = (-(int)x_pos-1) << (5 + dec->quarterpel); |
520 |
|
if (mv[i].x < border) { |
521 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
522 |
|
mv[i].x = border; |
523 |
|
} |
524 |
|
} |
525 |
|
|
526 |
|
border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel); |
527 |
|
if (mv[i].y > border) { |
528 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
529 |
|
mv[i].y = border; |
530 |
|
} else { |
531 |
|
border = (-(int)y_pos-1) << (5 + dec->quarterpel); |
532 |
|
if (mv[i].y < border) { |
533 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
534 |
|
mv[i].y = border; |
535 |
|
} |
536 |
|
} |
537 |
|
} |
538 |
|
|
539 |
start_timer(); |
start_timer(); |
540 |
|
|
541 |
if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
827 |
mb_height = (dec->height + 31) / 32; |
mb_height = (dec->height + 31) / 32; |
828 |
} |
} |
829 |
|
|
830 |
|
if (!dec->is_edged[0]) { |
831 |
start_timer(); |
start_timer(); |
832 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
833 |
dec->width, dec->height); |
dec->width, dec->height, dec->bs_version); |
834 |
|
dec->is_edged[0] = 1; |
835 |
stop_edges_timer(); |
stop_edges_timer(); |
836 |
|
} |
837 |
|
|
838 |
if (gmc_warp) { |
if (gmc_warp) { |
839 |
/* 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 */ |
904 |
mb->quant = quant; |
mb->quant = quant; |
905 |
|
|
906 |
if (dec->interlacing) { |
if (dec->interlacing) { |
907 |
if ((cbp || intra) && !mcsel) { |
if (cbp || intra) { |
908 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
909 |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
910 |
} |
} |
911 |
|
|
912 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) { |
913 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
914 |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
915 |
|
|
953 |
|
|
954 |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
955 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
956 |
|
mb->quant = quant; |
957 |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
958 |
|
|
959 |
if(dec->out_frm && cp_mb > 0) { |
if(dec->out_frm && cp_mb > 0) { |
963 |
st_mb = x+1; |
st_mb = x+1; |
964 |
} else { /* not coded P_VOP macroblock */ |
} else { /* not coded P_VOP macroblock */ |
965 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
966 |
|
mb->quant = quant; |
967 |
|
|
968 |
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; |
969 |
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; |
990 |
get_b_motion_vector(Bitstream * bs, |
get_b_motion_vector(Bitstream * bs, |
991 |
VECTOR * mv, |
VECTOR * mv, |
992 |
int fcode, |
int fcode, |
993 |
const VECTOR pmv) |
const VECTOR pmv, |
994 |
|
const DECODER * const dec, |
995 |
|
const int x, const int y) |
996 |
{ |
{ |
997 |
const int scale_fac = 1 << (fcode - 1); |
const int scale_fac = 1 << (fcode - 1); |
998 |
const int high = (32 * scale_fac) - 1; |
const int high = (32 * scale_fac) - 1; |
1032 |
{ |
{ |
1033 |
uint32_t stride = dec->edged_width; |
uint32_t stride = dec->edged_width; |
1034 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
|
uint32_t next_block = stride * 8; |
|
1035 |
int uv_dx, uv_dy; |
int uv_dx, uv_dy; |
1036 |
int b_uv_dx, b_uv_dy; |
int b_uv_dx, b_uv_dy; |
1037 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
1044 |
if (!direct) { |
if (!direct) { |
1045 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
1046 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
|
|
|
1047 |
b_uv_dx = pMB->b_mvs[0].x; |
b_uv_dx = pMB->b_mvs[0].x; |
1048 |
b_uv_dy = pMB->b_mvs[0].y; |
b_uv_dy = pMB->b_mvs[0].y; |
1049 |
|
|
1056 |
|
|
1057 |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
uv_dx = (uv_dx >> 1) + roundtab_79[uv_dx & 0x3]; |
1058 |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
uv_dy = (uv_dy >> 1) + roundtab_79[uv_dy & 0x3]; |
|
|
|
1059 |
b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3]; |
b_uv_dx = (b_uv_dx >> 1) + roundtab_79[b_uv_dx & 0x3]; |
1060 |
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]; |
1061 |
|
|
1062 |
} else { |
} else { |
|
if(dec->quarterpel) { |
|
|
uv_dx = (pMB->mvs[0].x / 2) + (pMB->mvs[1].x / 2) + (pMB->mvs[2].x / 2) + (pMB->mvs[3].x / 2); |
|
|
uv_dy = (pMB->mvs[0].y / 2) + (pMB->mvs[1].y / 2) + (pMB->mvs[2].y / 2) + (pMB->mvs[3].y / 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); |
|
|
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); |
|
|
} else { |
|
1063 |
uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
uv_dx = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
1064 |
uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
uv_dy = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
1065 |
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_dx = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
1066 |
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; |
1067 |
|
|
1068 |
|
if (dec->quarterpel) { |
1069 |
|
uv_dx /= 2; |
1070 |
|
uv_dy /= 2; |
1071 |
|
b_uv_dx /= 2; |
1072 |
|
b_uv_dy /= 2; |
1073 |
} |
} |
1074 |
|
|
1075 |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
uv_dx = (uv_dx >> 3) + roundtab_76[uv_dx & 0xf]; |
1105 |
pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
1106 |
interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, |
interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, |
1107 |
pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
1108 |
interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, |
interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos + 8, |
1109 |
16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
1110 |
} |
} |
1111 |
|
|
1112 |
interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, |
interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, |
1228 |
uint32_t x, y; |
uint32_t x, y; |
1229 |
VECTOR mv; |
VECTOR mv; |
1230 |
const VECTOR zeromv = {0,0}; |
const VECTOR zeromv = {0,0}; |
|
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
|
1231 |
int i; |
int i; |
1232 |
|
|
1233 |
#ifdef BFRAMES_DEC_DEBUG |
if (!dec->is_edged[0]) { |
1234 |
FILE *fp; |
start_timer(); |
1235 |
static char first=0; |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1236 |
#define BFRAME_DEBUG |
dec->width, dec->height, dec->bs_version); |
1237 |
if (!first && fp) { \ |
dec->is_edged[0] = 1; |
1238 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
stop_edges_timer(); |
1239 |
} |
} |
|
#endif |
|
1240 |
|
|
1241 |
|
if (!dec->is_edged[1]) { |
1242 |
start_timer(); |
start_timer(); |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
|
|
dec->width, dec->height); |
|
1243 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1244 |
dec->width, dec->height); |
dec->width, dec->height, dec->bs_version); |
1245 |
|
dec->is_edged[1] = 1; |
1246 |
stop_edges_timer(); |
stop_edges_timer(); |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
|
1247 |
} |
} |
|
#endif |
|
1248 |
|
|
1249 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1250 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1252 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1253 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1254 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1255 |
|
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
1256 |
|
uint32_t intra_dc_threshold; /* fake variable */ |
1257 |
|
|
1258 |
|
if (check_resync_marker(bs, fcode_max - 1)) { |
1259 |
|
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
1260 |
|
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1261 |
|
x = bound % dec->mb_width; |
1262 |
|
y = bound / dec->mb_width; |
1263 |
|
/* reset predicted macroblocks */ |
1264 |
|
dec->p_fmv = dec->p_bmv = zeromv; |
1265 |
|
} |
1266 |
|
|
1267 |
mv = |
mv = |
1268 |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = |
1327 |
|
|
1328 |
switch (mb->mode) { |
switch (mb->mode) { |
1329 |
case MODE_DIRECT: |
case MODE_DIRECT: |
1330 |
get_b_motion_vector(bs, &mv, 1, zeromv); |
get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y); |
1331 |
|
|
1332 |
case MODE_DIRECT_NONE_MV: |
case MODE_DIRECT_NONE_MV: |
1333 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1334 |
mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) / TRD + mv.x); |
mb->mvs[i].x = last_mb->mvs[i].x*dec->time_bp/dec->time_pp + mv.x; |
1335 |
mb->b_mvs[i].x = (int32_t) ((mv.x == 0) |
mb->mvs[i].y = last_mb->mvs[i].y*dec->time_bp/dec->time_pp + mv.y; |
1336 |
? ((TRB - TRD) * last_mb->mvs[i].x) / TRD |
|
1337 |
: mb->mvs[i].x - last_mb->mvs[i].x); |
mb->b_mvs[i].x = (mv.x) |
1338 |
mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y); |
? mb->mvs[i].x - last_mb->mvs[i].x |
1339 |
mb->b_mvs[i].y = (int32_t) ((mv.y == 0) |
: last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp; |
1340 |
? ((TRB - TRD) * last_mb->mvs[i].y) / TRD |
mb->b_mvs[i].y = (mv.y) |
1341 |
: mb->mvs[i].y - last_mb->mvs[i].y); |
? mb->mvs[i].y - last_mb->mvs[i].y |
1342 |
|
: last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp; |
1343 |
} |
} |
1344 |
|
|
1345 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1347 |
break; |
break; |
1348 |
|
|
1349 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1350 |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv); |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); |
1351 |
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]; |
1352 |
|
|
1353 |
get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv); |
get_b_motion_vector(bs, &mb->b_mvs[0], fcode_backward, dec->p_bmv, dec, x, y); |
1354 |
dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0]; |
dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = mb->b_mvs[0]; |
1355 |
|
|
1356 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1358 |
break; |
break; |
1359 |
|
|
1360 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1361 |
get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv); |
get_b_motion_vector(bs, &mb->mvs[0], fcode_backward, dec->p_bmv, dec, x, y); |
1362 |
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]; |
1363 |
|
|
1364 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0); |
1365 |
break; |
break; |
1366 |
|
|
1367 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1368 |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv); |
get_b_motion_vector(bs, &mb->mvs[0], fcode_forward, dec->p_fmv, dec, x, y); |
1369 |
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]; |
1370 |
|
|
1371 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
1376 |
} |
} |
1377 |
} /* End of for */ |
} /* End of for */ |
1378 |
} |
} |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
first=1; |
|
|
if (fp) |
|
|
fclose(fp); |
|
|
} |
|
|
#endif |
|
1379 |
} |
} |
1380 |
|
|
1381 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1382 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1383 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1384 |
|
int coding_type, int quant) |
1385 |
{ |
{ |
1386 |
|
const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0; |
1387 |
|
|
1388 |
|
if (dec->cartoon_mode) |
1389 |
|
frame->general &= ~XVID_FILMEFFECT; |
1390 |
|
|
1391 |
|
if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0) |
1392 |
|
&& mbs != NULL) /* post process */ |
1393 |
|
{ |
1394 |
|
/* note: image is stored to tmp */ |
1395 |
|
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
1396 |
|
image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, |
1397 |
|
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
1398 |
|
frame->general, brightness, dec->frames, (coding_type == B_VOP)); |
1399 |
|
img = &dec->tmp; |
1400 |
|
} |
1401 |
|
|
1402 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1403 |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1404 |
frame->output.csp, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1407 |
stats->type = coding2type(coding_type); |
stats->type = coding2type(coding_type); |
1408 |
stats->data.vop.time_base = (int)dec->time_base; |
stats->data.vop.time_base = (int)dec->time_base; |
1409 |
stats->data.vop.time_increment = 0; /* XXX: todo */ |
stats->data.vop.time_increment = 0; /* XXX: todo */ |
1410 |
|
stats->data.vop.qscale_stride = dec->mb_width; |
1411 |
|
stats->data.vop.qscale = dec->qscale; |
1412 |
|
if (stats->data.vop.qscale != NULL && mbs != NULL) { |
1413 |
|
int i; |
1414 |
|
for (i = 0; i < dec->mb_width*dec->mb_height; i++) |
1415 |
|
stats->data.vop.qscale[i] = mbs[i].quant; |
1416 |
|
} else |
1417 |
|
stats->data.vop.qscale = NULL; |
1418 |
} |
} |
1419 |
} |
} |
1420 |
|
|
1427 |
Bitstream bs; |
Bitstream bs; |
1428 |
uint32_t rounding; |
uint32_t rounding; |
1429 |
uint32_t reduced_resolution; |
uint32_t reduced_resolution; |
1430 |
uint32_t quant; |
uint32_t quant = 2; |
1431 |
uint32_t fcode_forward; |
uint32_t fcode_forward; |
1432 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1433 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1450 |
/* if not decoding "low_delay/packed", and this isn't low_delay and |
/* if not decoding "low_delay/packed", and this isn't low_delay and |
1451 |
we have a reference frame, then outout the reference frame */ |
we have a reference frame, then outout the reference frame */ |
1452 |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) { |
if (!(dec->low_delay_default && dec->packed_mode) && !dec->low_delay && dec->frames>0) { |
1453 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1454 |
dec->frames = 0; |
dec->frames = 0; |
1455 |
ret = 0; |
ret = 0; |
1456 |
} else { |
} else { |
1515 |
goto repeat; |
goto repeat; |
1516 |
} |
} |
1517 |
|
|
1518 |
|
if(dec->frames == 0 && coding_type != I_VOP) { |
1519 |
|
/* 1st frame is not an i-vop */ |
1520 |
|
goto repeat; |
1521 |
|
} |
1522 |
|
|
1523 |
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.y = dec->p_fmv.y = 0; /* init pred vector to 0 */ |
1524 |
|
|
1525 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1526 |
if (dec->packed_mode && coding_type == N_VOP) { |
if (dec->packed_mode && coding_type == N_VOP) { |
1527 |
if (dec->low_delay_default && dec->frames > 0) { |
if (dec->low_delay_default && dec->frames > 0) { |
1528 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1529 |
output = 1; |
output = 1; |
1530 |
} |
} |
1531 |
/* ignore otherwise */ |
/* ignore otherwise */ |
1546 |
/* XXX: not_coded vops are not used for forward prediction */ |
/* XXX: not_coded vops are not used for forward prediction */ |
1547 |
/* we should not swap(last_mbs,mbs) */ |
/* we should not swap(last_mbs,mbs) */ |
1548 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1549 |
|
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */ |
1550 |
break; |
break; |
1551 |
} |
} |
1552 |
|
|
1559 |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
/* note: for packed_mode, output is performed when the special-N_VOP is decoded */ |
1560 |
if (!(dec->low_delay_default && dec->packed_mode)) { |
if (!(dec->low_delay_default && dec->packed_mode)) { |
1561 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1562 |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1563 |
output = 1; |
output = 1; |
1564 |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
1565 |
/* output the reference frame */ |
/* output the reference frame */ |
1566 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1567 |
output = 1; |
output = 1; |
1568 |
} |
} |
1569 |
} |
} |
1570 |
|
|
1571 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1572 |
|
dec->is_edged[1] = dec->is_edged[0]; |
1573 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1574 |
|
dec->is_edged[0] = 0; |
1575 |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
1576 |
dec->last_reduced_resolution = reduced_resolution; |
dec->last_reduced_resolution = reduced_resolution; |
1577 |
dec->last_coding_type = coding_type; |
dec->last_coding_type = coding_type; |
1583 |
|
|
1584 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1585 |
DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n"); |
DPRINTF(XVID_DEBUG_ERROR, "warning: bvop found in low_delay==1 stream\n"); |
1586 |
dec->low_delay = 1; |
dec->low_delay = 0; |
1587 |
} |
} |
1588 |
|
|
1589 |
if (dec->frames < 2) { |
if (dec->frames < 2) { |
1590 |
/* attemping to decode a bvop without atleast 2 reference frames */ |
/* attemping to decode a bvop without atleast 2 reference frames */ |
1591 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1592 |
"broken b-frame, mising ref frames"); |
"broken b-frame, mising ref frames"); |
1593 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1594 |
} else if (dec->time_pp <= dec->time_bp) { |
} else if (dec->time_pp <= dec->time_bp) { |
1595 |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
1596 |
decoded in vfw. */ |
decoded in vfw. */ |
1597 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1598 |
"broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); |
"broken b-frame, tpp=%i tbp=%i", dec->time_pp, dec->time_bp); |
1599 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1600 |
} else { |
} else { |
1601 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1602 |
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1603 |
} |
} |
1604 |
|
|
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
|
1605 |
output = 1; |
output = 1; |
1606 |
dec->frames++; |
dec->frames++; |
1607 |
} |
} |
1608 |
|
|
1609 |
|
#if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */ |
1610 |
BitstreamByteAlign(&bs); |
BitstreamByteAlign(&bs); |
1611 |
|
#endif |
1612 |
|
|
1613 |
/* low_delay_default mode: repeat in packed_mode */ |
/* low_delay_default mode: repeat in packed_mode */ |
1614 |
if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { |
if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { |
1623 |
if (dec->low_delay_default && output == 0) { |
if (dec->low_delay_default && output == 0) { |
1624 |
if (dec->packed_mode && seen_something) { |
if (dec->packed_mode && seen_something) { |
1625 |
/* output the recently decoded frame */ |
/* output the recently decoded frame */ |
1626 |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type); |
decoder_output(dec, &dec->refn[0], dec->last_mbs, frame, stats, dec->last_coding_type, quant); |
1627 |
} else { |
} else { |
1628 |
image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128); |
image_clear(&dec->cur, dec->width, dec->height, dec->edged_width, 0, 128, 128); |
1629 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1631 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1632 |
"bframe decoder lag"); |
"bframe decoder lag"); |
1633 |
|
|
1634 |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant); |
1635 |
if (stats) stats->type = XVID_TYPE_NOTHING; |
if (stats) stats->type = XVID_TYPE_NOTHING; |
1636 |
} |
} |
1637 |
} |
} |
1639 |
emms(); |
emms(); |
1640 |
stop_global_timer(); |
stop_global_timer(); |
1641 |
|
|
1642 |
return BitstreamPos(&bs) / 8; /* number of bytes consumed */ |
return (BitstreamPos(&bs) + 7) / 8; /* number of bytes consumed */ |
1643 |
} |
} |