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" |
58 |
|
|
59 |
#include "image/image.h" |
#include "image/image.h" |
60 |
#include "image/colorspace.h" |
#include "image/colorspace.h" |
61 |
|
#include "image/postprocessing.h" |
62 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
63 |
|
|
64 |
static int |
static int |
77 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
78 |
if (dec->mbs) |
if (dec->mbs) |
79 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
80 |
|
if (dec->qscale) |
81 |
|
xvid_free(dec->qscale); |
82 |
|
|
83 |
/* realloc */ |
/* realloc */ |
84 |
dec->mb_width = (dec->width + 15) / 16; |
dec->mb_width = (dec->width + 15) / 16; |
163 |
|
|
164 |
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); |
165 |
|
|
166 |
|
/* nothing happens if that fails */ |
167 |
|
dec->qscale = |
168 |
|
xvid_malloc(sizeof(int) * dec->mb_width * dec->mb_height, CACHE_LINE); |
169 |
|
|
170 |
|
if (dec->qscale) |
171 |
|
memset(dec->qscale, 0, sizeof(int) * dec->mb_width * dec->mb_height); |
172 |
|
|
173 |
return 0; |
return 0; |
174 |
} |
} |
175 |
|
|
186 |
if (dec == NULL) { |
if (dec == NULL) { |
187 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
188 |
} |
} |
189 |
|
|
190 |
memset(dec, 0, sizeof(DECODER)); |
memset(dec, 0, sizeof(DECODER)); |
191 |
|
|
192 |
|
dec->mpeg_quant_matrices = xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
193 |
|
if (dec->mpeg_quant_matrices == NULL) { |
194 |
|
xvid_free(dec); |
195 |
|
return XVID_ERR_MEMORY; |
196 |
|
} |
197 |
|
|
198 |
create->handle = dec; |
create->handle = dec; |
199 |
|
|
200 |
dec->width = create->width; |
dec->width = create->width; |
209 |
/* image based GMC */ |
/* image based GMC */ |
210 |
image_null(&dec->gmc); |
image_null(&dec->gmc); |
211 |
|
|
|
|
|
212 |
dec->mbs = NULL; |
dec->mbs = NULL; |
213 |
dec->last_mbs = NULL; |
dec->last_mbs = NULL; |
214 |
|
dec->qscale = NULL; |
215 |
|
|
216 |
init_timer(); |
init_timer(); |
217 |
|
init_postproc(&dec->postproc); |
218 |
|
init_mpeg_matrix(dec->mpeg_quant_matrices); |
219 |
|
|
220 |
/* For B-frame support (used to save reference frame's time */ |
/* For B-frame support (used to save reference frame's time */ |
221 |
dec->frames = 0; |
dec->frames = 0; |
222 |
dec->time = dec->time_base = dec->last_time_base = 0; |
dec->time = dec->time_base = dec->last_time_base = 0; |
223 |
dec->low_delay = 0; |
dec->low_delay = 0; |
224 |
dec->packed_mode = 0; |
dec->packed_mode = 0; |
225 |
|
dec->time_inc_resolution = 1; /* until VOL header says otherwise */ |
226 |
|
|
227 |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
dec->fixed_dimensions = (dec->width > 0 && dec->height > 0); |
228 |
|
|
238 |
{ |
{ |
239 |
xvid_free(dec->last_mbs); |
xvid_free(dec->last_mbs); |
240 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
241 |
|
xvid_free(dec->qscale); |
242 |
|
|
243 |
/* image based GMC */ |
/* image based GMC */ |
244 |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
image_destroy(&dec->gmc, dec->edged_width, dec->edged_height); |
248 |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->tmp, dec->edged_width, dec->edged_height); |
249 |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
image_destroy(&dec->qtmp, dec->edged_width, dec->edged_height); |
250 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
251 |
|
xvid_free(dec->mpeg_quant_matrices); |
252 |
xvid_free(dec); |
xvid_free(dec); |
253 |
|
|
254 |
write_timer(); |
write_timer(); |
339 |
stop_coding_timer(); |
stop_coding_timer(); |
340 |
|
|
341 |
start_timer(); |
start_timer(); |
342 |
add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); |
add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors, dec->bs_version); |
343 |
stop_prediction_timer(); |
stop_prediction_timer(); |
344 |
|
|
345 |
start_timer(); |
start_timer(); |
346 |
if (dec->quant_type == 0) { |
if (dec->quant_type == 0) { |
347 |
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); |
348 |
} else { |
} else { |
349 |
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); |
350 |
} |
} |
351 |
stop_iquant_timer(); |
stop_iquant_timer(); |
352 |
|
|
415 |
stop_coding_timer(); |
stop_coding_timer(); |
416 |
|
|
417 |
start_timer(); |
start_timer(); |
418 |
dequant(&data[i * 64], block, iQuant); |
dequant(&data[i * 64], block, iQuant, dec->mpeg_quant_matrices); |
419 |
stop_iquant_timer(); |
stop_iquant_timer(); |
420 |
|
|
421 |
start_timer(); |
start_timer(); |
497 |
mv[i] = pMB->mvs[i]; |
mv[i] = pMB->mvs[i]; |
498 |
} |
} |
499 |
|
|
500 |
|
for (i = 0; i < 4; i++) { |
501 |
|
/* clip to valid range */ |
502 |
|
int border = (int)(dec->mb_width - x_pos) << (5 + dec->quarterpel); |
503 |
|
if (mv[i].x > border) { |
504 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x > max -- %d > %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
505 |
|
mv[i].x = border; |
506 |
|
} else { |
507 |
|
border = (-(int)x_pos-1) << (5 + dec->quarterpel); |
508 |
|
if (mv[i].x < border) { |
509 |
|
DPRINTF(XVID_DEBUG_MV, "mv.x < min -- %d < %d, MB %d, %d", mv[i].x, border, x_pos, y_pos); |
510 |
|
mv[i].x = border; |
511 |
|
} |
512 |
|
} |
513 |
|
|
514 |
|
border = (int)(dec->mb_height - y_pos) << (5 + dec->quarterpel); |
515 |
|
if (mv[i].y > border) { |
516 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y > max -- %d > %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
517 |
|
mv[i].y = border; |
518 |
|
} else { |
519 |
|
border = (-(int)y_pos-1) << (5 + dec->quarterpel); |
520 |
|
if (mv[i].y < border) { |
521 |
|
DPRINTF(XVID_DEBUG_MV, "mv.y < min -- %d < %d, MB %d, %d", mv[i].y, border, x_pos, y_pos); |
522 |
|
mv[i].y = border; |
523 |
|
} |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
start_timer(); |
start_timer(); |
528 |
|
|
529 |
if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
if (pMB->mode != MODE_INTER4V) { /* INTER, INTER_Q, NOT_CODED, FORWARD, BACKWARD */ |
815 |
mb_height = (dec->height + 31) / 32; |
mb_height = (dec->height + 31) / 32; |
816 |
} |
} |
817 |
|
|
818 |
|
if (!dec->is_edged[0]) { |
819 |
start_timer(); |
start_timer(); |
820 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
821 |
dec->width, dec->height); |
dec->width, dec->height, dec->bs_version); |
822 |
|
dec->is_edged[0] = 1; |
823 |
stop_edges_timer(); |
stop_edges_timer(); |
824 |
|
} |
825 |
|
|
826 |
if (gmc_warp) { |
if (gmc_warp) { |
827 |
/* 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 */ |
892 |
mb->quant = quant; |
mb->quant = quant; |
893 |
|
|
894 |
if (dec->interlacing) { |
if (dec->interlacing) { |
895 |
if ((cbp || intra) && !mcsel) { |
if (cbp || intra) { |
896 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
897 |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
DPRINTF(XVID_DEBUG_MB,"decp: field_dct: %i\n", mb->field_dct); |
898 |
} |
} |
899 |
|
|
900 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if ((mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) && !mcsel) { |
901 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
902 |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
DPRINTF(XVID_DEBUG_MB, "decp: field_pred: %i\n", mb->field_pred); |
903 |
|
|
941 |
|
|
942 |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
} else if (gmc_warp) { /* a not coded S(GMC)-VOP macroblock */ |
943 |
mb->mode = MODE_NOT_CODED_GMC; |
mb->mode = MODE_NOT_CODED_GMC; |
944 |
|
mb->quant = quant; |
945 |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
decoder_mbgmc(dec, mb, x, y, fcode, 0x00, bs, rounding); |
946 |
|
|
947 |
if(dec->out_frm && cp_mb > 0) { |
if(dec->out_frm && cp_mb > 0) { |
951 |
st_mb = x+1; |
st_mb = x+1; |
952 |
} else { /* not coded P_VOP macroblock */ |
} else { /* not coded P_VOP macroblock */ |
953 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
954 |
|
mb->quant = quant; |
955 |
|
|
956 |
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; |
957 |
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; |
978 |
get_b_motion_vector(Bitstream * bs, |
get_b_motion_vector(Bitstream * bs, |
979 |
VECTOR * mv, |
VECTOR * mv, |
980 |
int fcode, |
int fcode, |
981 |
const VECTOR pmv) |
const VECTOR pmv, |
982 |
|
const DECODER * const dec, |
983 |
|
const int x, const int y) |
984 |
{ |
{ |
985 |
const int scale_fac = 1 << (fcode - 1); |
const int scale_fac = 1 << (fcode - 1); |
986 |
const int high = (32 * scale_fac) - 1; |
const int high = (32 * scale_fac) - 1; |
1095 |
pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
1096 |
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, |
1097 |
pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
1098 |
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, |
1099 |
16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
1100 |
} |
} |
1101 |
|
|
1102 |
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, |
1218 |
uint32_t x, y; |
uint32_t x, y; |
1219 |
VECTOR mv; |
VECTOR mv; |
1220 |
const VECTOR zeromv = {0,0}; |
const VECTOR zeromv = {0,0}; |
|
const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
|
1221 |
int i; |
int i; |
1222 |
|
|
1223 |
#ifdef BFRAMES_DEC_DEBUG |
if (!dec->is_edged[0]) { |
1224 |
FILE *fp; |
start_timer(); |
1225 |
static char first=0; |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1226 |
#define BFRAME_DEBUG |
dec->width, dec->height, dec->bs_version); |
1227 |
if (!first && fp) { \ |
dec->is_edged[0] = 1; |
1228 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
stop_edges_timer(); |
1229 |
} |
} |
|
#endif |
|
1230 |
|
|
1231 |
|
if (!dec->is_edged[1]) { |
1232 |
start_timer(); |
start_timer(); |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
|
|
dec->width, dec->height); |
|
1233 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1234 |
dec->width, dec->height); |
dec->width, dec->height, dec->bs_version); |
1235 |
|
dec->is_edged[1] = 1; |
1236 |
stop_edges_timer(); |
stop_edges_timer(); |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
fp=fopen("C:\\XVIDDBG.TXT","w"); |
|
1237 |
} |
} |
|
#endif |
|
1238 |
|
|
1239 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1240 |
/* Initialize Pred Motion Vector */ |
/* Initialize Pred Motion Vector */ |
1242 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1243 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1244 |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; |
1245 |
|
const int fcode_max = (fcode_forward>fcode_backward) ? fcode_forward : fcode_backward; |
1246 |
|
uint32_t intra_dc_threshold; /* fake variable */ |
1247 |
|
|
1248 |
|
if (check_resync_marker(bs, fcode_max - 1)) { |
1249 |
|
int bound = read_video_packet_header(bs, dec, fcode_max - 1, &quant, |
1250 |
|
&fcode_forward, &fcode_backward, &intra_dc_threshold); |
1251 |
|
x = bound % dec->mb_width; |
1252 |
|
y = bound / dec->mb_width; |
1253 |
|
/* reset predicted macroblocks */ |
1254 |
|
dec->p_fmv = dec->p_bmv = zeromv; |
1255 |
|
} |
1256 |
|
|
1257 |
mv = |
mv = |
1258 |
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] = |
1317 |
|
|
1318 |
switch (mb->mode) { |
switch (mb->mode) { |
1319 |
case MODE_DIRECT: |
case MODE_DIRECT: |
1320 |
get_b_motion_vector(bs, &mv, 1, zeromv); |
get_b_motion_vector(bs, &mv, 1, zeromv, dec, x, y); |
1321 |
|
|
1322 |
case MODE_DIRECT_NONE_MV: |
case MODE_DIRECT_NONE_MV: |
1323 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < 4; i++) { |
1324 |
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; |
1325 |
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; |
1326 |
? ((TRB - TRD) * last_mb->mvs[i].x) / TRD |
|
1327 |
: mb->mvs[i].x - last_mb->mvs[i].x); |
mb->b_mvs[i].x = (mv.x) |
1328 |
mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) / TRD + mv.y); |
? mb->mvs[i].x - last_mb->mvs[i].x |
1329 |
mb->b_mvs[i].y = (int32_t) ((mv.y == 0) |
: last_mb->mvs[i].x*(dec->time_bp - dec->time_pp)/dec->time_pp; |
1330 |
? ((TRB - TRD) * last_mb->mvs[i].y) / TRD |
mb->b_mvs[i].y = (mv.y) |
1331 |
: mb->mvs[i].y - last_mb->mvs[i].y); |
? mb->mvs[i].y - last_mb->mvs[i].y |
1332 |
|
: last_mb->mvs[i].y*(dec->time_bp - dec->time_pp)/dec->time_pp; |
1333 |
} |
} |
1334 |
|
|
1335 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1337 |
break; |
break; |
1338 |
|
|
1339 |
case MODE_INTERPOLATE: |
case MODE_INTERPOLATE: |
1340 |
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); |
1341 |
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]; |
1342 |
|
|
1343 |
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); |
1344 |
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]; |
1345 |
|
|
1346 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1348 |
break; |
break; |
1349 |
|
|
1350 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1351 |
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); |
1352 |
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]; |
1353 |
|
|
1354 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 0); |
1355 |
break; |
break; |
1356 |
|
|
1357 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1358 |
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); |
1359 |
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]; |
1360 |
|
|
1361 |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
decoder_mbinter(dec, mb, x, y, mb->cbp, bs, 0, 0, 1); |
1366 |
} |
} |
1367 |
} /* End of for */ |
} /* End of for */ |
1368 |
} |
} |
|
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
if (!first){ |
|
|
first=1; |
|
|
if (fp) |
|
|
fclose(fp); |
|
|
} |
|
|
#endif |
|
1369 |
} |
} |
1370 |
|
|
1371 |
/* perform post processing if necessary, and output the image */ |
/* perform post processing if necessary, and output the image */ |
1372 |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
void decoder_output(DECODER * dec, IMAGE * img, MACROBLOCK * mbs, |
1373 |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, int coding_type) |
xvid_dec_frame_t * frame, xvid_dec_stats_t * stats, |
1374 |
|
int coding_type, int quant) |
1375 |
{ |
{ |
1376 |
|
const int brightness = XVID_VERSION_MINOR(frame->version) >= 1 ? frame->brightness : 0; |
1377 |
|
|
1378 |
|
if (dec->cartoon_mode) |
1379 |
|
frame->general &= ~XVID_FILMEFFECT; |
1380 |
|
|
1381 |
|
if ((frame->general & (XVID_DEBLOCKY|XVID_DEBLOCKUV|XVID_FILMEFFECT) || brightness!=0) |
1382 |
|
&& mbs != NULL) /* post process */ |
1383 |
|
{ |
1384 |
|
/* note: image is stored to tmp */ |
1385 |
|
image_copy(&dec->tmp, img, dec->edged_width, dec->height); |
1386 |
|
image_postproc(&dec->postproc, &dec->tmp, dec->edged_width, |
1387 |
|
mbs, dec->mb_width, dec->mb_height, dec->mb_width, |
1388 |
|
frame->general, brightness, dec->frames, (coding_type == B_VOP)); |
1389 |
|
img = &dec->tmp; |
1390 |
|
} |
1391 |
|
|
1392 |
image_output(img, dec->width, dec->height, |
image_output(img, dec->width, dec->height, |
1393 |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
dec->edged_width, (uint8_t**)frame->output.plane, frame->output.stride, |
1394 |
frame->output.csp, dec->interlacing); |
frame->output.csp, dec->interlacing); |
1397 |
stats->type = coding2type(coding_type); |
stats->type = coding2type(coding_type); |
1398 |
stats->data.vop.time_base = (int)dec->time_base; |
stats->data.vop.time_base = (int)dec->time_base; |
1399 |
stats->data.vop.time_increment = 0; /* XXX: todo */ |
stats->data.vop.time_increment = 0; /* XXX: todo */ |
1400 |
|
stats->data.vop.qscale_stride = dec->mb_width; |
1401 |
|
stats->data.vop.qscale = dec->qscale; |
1402 |
|
if (stats->data.vop.qscale != NULL && mbs != NULL) { |
1403 |
|
int i; |
1404 |
|
for (i = 0; i < dec->mb_width*dec->mb_height; i++) |
1405 |
|
stats->data.vop.qscale[i] = mbs[i].quant; |
1406 |
|
} else |
1407 |
|
stats->data.vop.qscale = NULL; |
1408 |
} |
} |
1409 |
} |
} |
1410 |
|
|
1417 |
Bitstream bs; |
Bitstream bs; |
1418 |
uint32_t rounding; |
uint32_t rounding; |
1419 |
uint32_t reduced_resolution; |
uint32_t reduced_resolution; |
1420 |
uint32_t quant; |
uint32_t quant = 2; |
1421 |
uint32_t fcode_forward; |
uint32_t fcode_forward; |
1422 |
uint32_t fcode_backward; |
uint32_t fcode_backward; |
1423 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1440 |
/* 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 |
1441 |
we have a reference frame, then outout the reference frame */ |
we have a reference frame, then outout the reference frame */ |
1442 |
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) { |
1443 |
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); |
1444 |
dec->frames = 0; |
dec->frames = 0; |
1445 |
ret = 0; |
ret = 0; |
1446 |
} else { |
} else { |
1505 |
goto repeat; |
goto repeat; |
1506 |
} |
} |
1507 |
|
|
1508 |
|
if(dec->frames == 0 && coding_type != I_VOP) { |
1509 |
|
/* 1st frame is not an i-vop */ |
1510 |
|
goto repeat; |
1511 |
|
} |
1512 |
|
|
1513 |
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 */ |
1514 |
|
|
1515 |
/* packed_mode: special-N_VOP treament */ |
/* packed_mode: special-N_VOP treament */ |
1516 |
if (dec->packed_mode && coding_type == N_VOP) { |
if (dec->packed_mode && coding_type == N_VOP) { |
1517 |
if (dec->low_delay_default && dec->frames > 0) { |
if (dec->low_delay_default && dec->frames > 0) { |
1518 |
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); |
1519 |
output = 1; |
output = 1; |
1520 |
} |
} |
1521 |
/* ignore otherwise */ |
/* ignore otherwise */ |
1536 |
/* XXX: not_coded vops are not used for forward prediction */ |
/* XXX: not_coded vops are not used for forward prediction */ |
1537 |
/* we should not swap(last_mbs,mbs) */ |
/* we should not swap(last_mbs,mbs) */ |
1538 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1539 |
|
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); /* it will be swapped back */ |
1540 |
break; |
break; |
1541 |
} |
} |
1542 |
|
|
1549 |
/* 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 */ |
1550 |
if (!(dec->low_delay_default && dec->packed_mode)) { |
if (!(dec->low_delay_default && dec->packed_mode)) { |
1551 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1552 |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1553 |
output = 1; |
output = 1; |
1554 |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
} else if (dec->frames > 0) { /* is the reference frame valid? */ |
1555 |
/* output the reference frame */ |
/* output the reference frame */ |
1556 |
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); |
1557 |
output = 1; |
output = 1; |
1558 |
} |
} |
1559 |
} |
} |
1560 |
|
|
1561 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1562 |
|
dec->is_edged[1] = dec->is_edged[0]; |
1563 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1564 |
|
dec->is_edged[0] = 0; |
1565 |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
SWAP(MACROBLOCK *, dec->mbs, dec->last_mbs); |
1566 |
dec->last_reduced_resolution = reduced_resolution; |
dec->last_reduced_resolution = reduced_resolution; |
1567 |
dec->last_coding_type = coding_type; |
dec->last_coding_type = coding_type; |
1573 |
|
|
1574 |
if (dec->low_delay) { |
if (dec->low_delay) { |
1575 |
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"); |
1576 |
dec->low_delay = 1; |
dec->low_delay = 0; |
1577 |
} |
} |
1578 |
|
|
1579 |
if (dec->frames < 2) { |
if (dec->frames < 2) { |
1580 |
/* attemping to decode a bvop without atleast 2 reference frames */ |
/* attemping to decode a bvop without atleast 2 reference frames */ |
1581 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1582 |
"broken b-frame, mising ref frames"); |
"broken b-frame, mising ref frames"); |
1583 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1584 |
} else if (dec->time_pp <= dec->time_bp) { |
} else if (dec->time_pp <= dec->time_bp) { |
1585 |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
/* this occurs when dx50_bvop_compatibility==0 sequences are |
1586 |
decoded in vfw. */ |
decoded in vfw. */ |
1587 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1588 |
"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); |
1589 |
|
if (stats) stats->type = XVID_TYPE_NOTHING; |
1590 |
} else { |
} else { |
1591 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1592 |
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type, quant); |
1593 |
} |
} |
1594 |
|
|
|
decoder_output(dec, &dec->cur, dec->mbs, frame, stats, coding_type); |
|
1595 |
output = 1; |
output = 1; |
1596 |
dec->frames++; |
dec->frames++; |
1597 |
} |
} |
1598 |
|
|
1599 |
|
#if 0 /* Avoids to read to much data because of 32bit reads in our BS functions */ |
1600 |
BitstreamByteAlign(&bs); |
BitstreamByteAlign(&bs); |
1601 |
|
#endif |
1602 |
|
|
1603 |
/* low_delay_default mode: repeat in packed_mode */ |
/* low_delay_default mode: repeat in packed_mode */ |
1604 |
if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { |
if (dec->low_delay_default && dec->packed_mode && output == 0 && success == 0) { |
1613 |
if (dec->low_delay_default && output == 0) { |
if (dec->low_delay_default && output == 0) { |
1614 |
if (dec->packed_mode && seen_something) { |
if (dec->packed_mode && seen_something) { |
1615 |
/* output the recently decoded frame */ |
/* output the recently decoded frame */ |
1616 |
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); |
1617 |
} else { |
} else { |
1618 |
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); |
1619 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 16, |
1621 |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
image_printf(&dec->cur, dec->edged_width, dec->height, 16, 64, |
1622 |
"bframe decoder lag"); |
"bframe decoder lag"); |
1623 |
|
|
1624 |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP); |
decoder_output(dec, &dec->cur, NULL, frame, stats, P_VOP, quant); |
1625 |
if (stats) stats->type = XVID_TYPE_NOTHING; |
if (stats) stats->type = XVID_TYPE_NOTHING; |
1626 |
} |
} |
1627 |
} |
} |
1629 |
emms(); |
emms(); |
1630 |
stop_global_timer(); |
stop_global_timer(); |
1631 |
|
|
1632 |
return BitstreamPos(&bs) / 8; /* number of bytes consumed */ |
return (BitstreamPos(&bs) + 7) / 8; /* number of bytes consumed */ |
1633 |
} |
} |