3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Decoder main module - |
* - Decoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 MinChen <chenm001@163.com> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002 Peter Ross <pross@xvid.org> |
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* This file is part of XviD, a free MPEG-4 video encoder/decoder |
10 |
* it under the terms of the GNU General Public License as published by |
* |
11 |
|
* XviD is free software; you can redistribute it and/or modify it |
12 |
|
* under the terms of the GNU General Public License as published by |
13 |
* the Free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
14 |
* (at your option) any later version. |
* (at your option) any later version. |
15 |
* |
* |
22 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
24 |
* |
* |
25 |
*************************************************************************/ |
* Under section 8 of the GNU General Public License, the copyright |
26 |
|
* holders of XVID explicitly forbid distribution in the following |
27 |
/************************************************************************** |
* countries: |
28 |
* |
* |
29 |
* History: |
* - Japan |
30 |
|
* - United States of America |
31 |
* |
* |
32 |
* 15.07.2002 fix a bug in B-frame decode at DIRECT mode |
* Linking XviD statically or dynamically with other modules is making a |
33 |
* MinChen <chenm001@163.com> |
* combined work based on XviD. Thus, the terms and conditions of the |
34 |
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
* GNU General Public License cover the whole combination. |
35 |
* Fix a little bug for low_delay flage |
* |
36 |
* MinChen <chenm001@163.com> |
* As a special exception, the copyright holders of XviD give you |
37 |
* 28.06.2002 added basic resync support to iframe/pframe_decode() |
* permission to link XviD with independent modules that communicate with |
38 |
* 22.06.2002 added primative N_VOP support |
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
39 |
* #define BFRAMES_DEC now enables Minchen's bframe decoder |
* license terms of these independent modules, and to copy and distribute |
40 |
* 08.05.2002 add low_delay support for B_VOP decode |
* the resulting combined work under terms of your choice, provided that |
41 |
* MinChen <chenm001@163.com> |
* every copy of the combined work is accompanied by a complete copy of |
42 |
* 05.05.2002 fix some B-frame decode problem |
* the source code of XviD (the version of XviD used to produce the |
43 |
* 02.05.2002 add B-frame decode support(have some problem); |
* combined work), being distributed under the terms of the GNU General |
44 |
* MinChen <chenm001@163.com> |
* Public License plus this exception. An independent module is a module |
45 |
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
* which is not derived from or based on XviD. |
46 |
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
* |
47 |
* reconstructing blocks, thus artifacts |
* Note that people who make modified versions of XviD are not obligated |
48 |
* interlacing speedup - used transfers to re-interlace |
* to grant this special exception for their modified versions; it is |
49 |
* interlaced decoding should be as fast as progressive now |
* their choice whether to do so. The GNU General Public License gives |
50 |
* 26.03.2002 interlacing support - moved transfers outside decode loop |
* permission to release a modified version without this exception; this |
51 |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
* exception also makes it possible to release a modified version which |
52 |
* 22.12.2001 lock based interpolation |
* carries forward this exception. |
|
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
|
53 |
* |
* |
54 |
* $Id$ |
* $Id$ |
55 |
* |
* |
116 |
xvid_free(dec); |
xvid_free(dec); |
117 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
118 |
} |
} |
119 |
// add by chenm001 <chenm001@163.com> |
/* add by chenm001 <chenm001@163.com> */ |
120 |
// for support B-frame to reference last 2 frame |
/* for support B-frame to reference last 2 frame */ |
121 |
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
122 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
123 |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
146 |
|
|
147 |
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
148 |
|
|
149 |
// add by chenm001 <chenm001@163.com> |
/* add by chenm001 <chenm001@163.com> */ |
150 |
// for skip MB flag |
/* for skip MB flag */ |
151 |
dec->last_mbs = |
dec->last_mbs = |
152 |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
153 |
CACHE_LINE); |
CACHE_LINE); |
165 |
|
|
166 |
init_timer(); |
init_timer(); |
167 |
|
|
168 |
// add by chenm001 <chenm001@163.com> |
/* add by chenm001 <chenm001@163.com> */ |
169 |
// for support B-frame to save reference frame's time |
/* for support B-frame to save reference frame's time */ |
170 |
dec->frames = -1; |
dec->frames = -1; |
171 |
dec->time = dec->time_base = dec->last_time_base = 0; |
dec->time = dec->time_base = dec->last_time_base = 0; |
172 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
// decode an intra macroblock |
/* decode an intra macroblock */ |
202 |
|
|
203 |
void |
void |
204 |
decoder_mbintra(DECODER * dec, |
decoder_mbintra(DECODER * dec, |
227 |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
228 |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
229 |
|
|
230 |
memset(block, 0, 6 * 64 * sizeof(int16_t)); // clear |
memset(block, 0, 6 * 64 * sizeof(int16_t)); /* clear */ |
231 |
|
|
232 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
233 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
250 |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0; |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0; |
251 |
|
|
252 |
if (dc_size > 8) { |
if (dc_size > 8) { |
253 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); /* marker */ |
254 |
} |
} |
255 |
|
|
256 |
block[i * 64 + 0] = dc_dif; |
block[i * 64 + 0] = dc_dif; |
262 |
} |
} |
263 |
|
|
264 |
start_timer(); |
start_timer(); |
265 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) /* coded */ |
266 |
{ |
{ |
267 |
get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
268 |
start_coeff); |
start_coeff); |
311 |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
{ 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
312 |
|
|
313 |
|
|
314 |
// decode an inter macroblock |
/* decode an inter macroblock */ |
315 |
|
|
316 |
void |
void |
317 |
decoder_mbinter(DECODER * dec, |
decoder_mbinter(DECODER * dec, |
344 |
uv_dx = pMB->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
345 |
uv_dy = pMB->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
346 |
|
|
347 |
|
if (dec->quarterpel) |
348 |
|
{ |
349 |
|
uv_dx = (uv_dx >> 1) | (uv_dx & 1); |
350 |
|
uv_dy = (uv_dy >> 1) | (uv_dy & 1); |
351 |
|
} |
352 |
|
|
353 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
354 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
355 |
} else { |
} else { |
356 |
int sum; |
int sum; |
|
|
|
357 |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
358 |
uv_dx = |
|
359 |
(sum == |
if (dec->quarterpel) |
360 |
0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + |
{ |
361 |
(ABS(sum) / 16) * 2)); |
sum /= 2; |
362 |
|
} |
363 |
|
|
364 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
365 |
|
|
366 |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
367 |
uv_dy = |
|
368 |
(sum == |
if (dec->quarterpel) |
369 |
0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + |
{ |
370 |
(ABS(sum) / 16) * 2)); |
sum /= 2; |
371 |
|
} |
372 |
|
|
373 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); |
374 |
} |
} |
375 |
|
|
376 |
start_timer(); |
start_timer(); |
377 |
|
if(dec->quarterpel) { |
378 |
|
DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n"); |
379 |
|
interpolate8x8_quarterpel(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_quarterpel(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_quarterpel(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_quarterpel(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 |
|
else { |
389 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, 16 * y_pos, |
390 |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
391 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, |
392 |
16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, |
pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
393 |
rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, |
394 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos, |
pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
395 |
16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, |
396 |
rounding); |
pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
397 |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16 * x_pos + 8, |
} |
398 |
16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, |
|
|
rounding); |
|
399 |
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
400 |
uv_dx, uv_dy, stride2, rounding); |
uv_dx, uv_dy, stride2, rounding); |
401 |
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, |
403 |
stop_comp_timer(); |
stop_comp_timer(); |
404 |
|
|
405 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
406 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) /* coded */ |
407 |
{ |
{ |
408 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); /* clear */ |
409 |
|
|
410 |
start_timer(); |
start_timer(); |
411 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64]); |
505 |
|
|
506 |
if (dec->interlacing) { |
if (dec->interlacing) { |
507 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
508 |
DEBUG1("deci: field_dct: ", mb->field_dct); |
DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct); |
509 |
} |
} |
510 |
|
|
511 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
581 |
|
|
582 |
start_timer(); |
start_timer(); |
583 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
584 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
585 |
stop_edges_timer(); |
stop_edges_timer(); |
586 |
|
|
587 |
bound = 0; |
bound = 0; |
591 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
592 |
MACROBLOCK *mb; |
MACROBLOCK *mb; |
593 |
|
|
594 |
// skip stuffing |
/* skip stuffing */ |
595 |
while (BitstreamShowBits(bs, 10) == 1) |
while (BitstreamShowBits(bs, 10) == 1) |
596 |
BitstreamSkip(bs, 10); |
BitstreamSkip(bs, 10); |
597 |
|
|
605 |
|
|
606 |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); |
607 |
|
|
608 |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
/*if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) not_coded */ |
609 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) /* not_coded */ |
610 |
{ |
{ |
611 |
uint32_t mcbpc; |
uint32_t mcbpc; |
612 |
uint32_t cbpc; |
uint32_t cbpc; |
649 |
mb->quant = quant; |
mb->quant = quant; |
650 |
|
|
651 |
if (dec->interlacing) { |
if (dec->interlacing) { |
652 |
|
if (cbp || intra) { |
653 |
mb->field_dct = BitstreamGetBit(bs); |
mb->field_dct = BitstreamGetBit(bs); |
654 |
DEBUG1("decp: field_dct: ", mb->field_dct); |
DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct); |
655 |
|
} |
656 |
|
|
657 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
658 |
mb->field_pred = BitstreamGetBit(bs); |
mb->field_pred = BitstreamGetBit(bs); |
659 |
DEBUG1("decp: field_pred: ", mb->field_pred); |
DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred); |
660 |
|
|
661 |
if (mb->field_pred) { |
if (mb->field_pred) { |
662 |
mb->field_for_top = BitstreamGetBit(bs); |
mb->field_for_top = BitstreamGetBit(bs); |
663 |
DEBUG1("decp: field_for_top: ", mb->field_for_top); |
DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top); |
664 |
mb->field_for_bot = BitstreamGetBit(bs); |
mb->field_for_bot = BitstreamGetBit(bs); |
665 |
DEBUG1("decp: field_for_bot: ", mb->field_for_bot); |
DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot); |
666 |
} |
} |
667 |
} |
} |
668 |
} |
} |
681 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
682 |
mb->mvs[0].y; |
mb->mvs[0].y; |
683 |
} |
} |
684 |
} else if (mb->mode == |
} else if (mb->mode == MODE_INTER4V ) { |
685 |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
|
686 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); |
687 |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); |
688 |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); |
689 |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); |
690 |
} else // MODE_INTRA, MODE_INTRA_Q |
} else /* MODE_INTRA, MODE_INTRA_Q */ |
691 |
{ |
{ |
692 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
693 |
0; |
0; |
700 |
|
|
701 |
decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
702 |
rounding); |
rounding); |
703 |
} else // not coded |
} else /* not coded */ |
704 |
{ |
{ |
705 |
//DEBUG2("P-frame MB at (X,Y)=",x,y); |
DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y); |
706 |
|
|
707 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
708 |
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; |
709 |
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; |
710 |
|
|
711 |
// copy macroblock directly from ref to cur |
/* copy macroblock directly from ref to cur */ |
712 |
|
|
713 |
start_timer(); |
start_timer(); |
714 |
|
|
759 |
} |
} |
760 |
|
|
761 |
|
|
762 |
// add by MinChen <chenm001@163.com> |
/* add by MinChen <chenm001@163.com> */ |
763 |
// decode B-frame motion vector |
/* decode B-frame motion vector */ |
764 |
void |
void |
765 |
get_b_motion_vector(DECODER * dec, |
get_b_motion_vector(DECODER * dec, |
766 |
Bitstream * bs, |
Bitstream * bs, |
804 |
} |
} |
805 |
|
|
806 |
|
|
807 |
// add by MinChen <chenm001@163.com> |
/* add by MinChen <chenm001@163.com> */ |
808 |
// decode an B-frame forward & backward inter macroblock |
/* decode an B-frame forward & backward inter macroblock */ |
809 |
void |
void |
810 |
decoder_bf_mbinter(DECODER * dec, |
decoder_bf_mbinter(DECODER * dec, |
811 |
const MACROBLOCK * pMB, |
const MACROBLOCK * pMB, |
873 |
stop_comp_timer(); |
stop_comp_timer(); |
874 |
|
|
875 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
876 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) /* coded */ |
877 |
{ |
{ |
878 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); /* clear */ |
879 |
|
|
880 |
start_timer(); |
start_timer(); |
881 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64]); |
917 |
} |
} |
918 |
|
|
919 |
|
|
920 |
// add by MinChen <chenm001@163.com> |
/* add by MinChen <chenm001@163.com> */ |
921 |
// decode an B-frame direct & inter macroblock |
/* decode an B-frame direct & inter macroblock */ |
922 |
void |
void |
923 |
decoder_bf_interpolate_mbinter(DECODER * dec, |
decoder_bf_interpolate_mbinter(DECODER * dec, |
924 |
IMAGE forward, |
IMAGE forward, |
1039 |
stop_comp_timer(); |
stop_comp_timer(); |
1040 |
|
|
1041 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
1042 |
if (cbp & (1 << (5 - i))) // coded |
if (cbp & (1 << (5 - i))) /* coded */ |
1043 |
{ |
{ |
1044 |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i * 64], 0, 64 * sizeof(int16_t)); /* clear */ |
1045 |
|
|
1046 |
start_timer(); |
start_timer(); |
1047 |
get_inter_block(bs, &block[i * 64]); |
get_inter_block(bs, &block[i * 64]); |
1083 |
} |
} |
1084 |
|
|
1085 |
|
|
1086 |
// add by MinChen <chenm001@163.com> |
/* add by MinChen <chenm001@163.com> */ |
1087 |
// for decode B-frame dbquant |
/* for decode B-frame dbquant */ |
1088 |
int32_t __inline |
int32_t __inline |
1089 |
get_dbquant(Bitstream * bs) |
get_dbquant(Bitstream * bs) |
1090 |
{ |
{ |
1091 |
if (!BitstreamGetBit(bs)) // '0' |
if (!BitstreamGetBit(bs)) /* '0' */ |
1092 |
return (0); |
return (0); |
1093 |
else if (!BitstreamGetBit(bs)) // '10' |
else if (!BitstreamGetBit(bs)) /* '10' */ |
1094 |
return (-2); |
return (-2); |
1095 |
else |
else |
1096 |
return (2); // '11' |
return (2); /* '11' */ |
1097 |
} |
} |
1098 |
|
|
1099 |
// add by MinChen <chenm001@163.com> |
/* add by MinChen <chenm001@163.com> */ |
1100 |
// for decode B-frame mb_type |
/* for decode B-frame mb_type */ |
1101 |
// bit ret_value |
/* bit ret_value */ |
1102 |
// 1 0 |
/* 1 0 */ |
1103 |
// 01 1 |
/* 01 1 */ |
1104 |
// 001 2 |
/* 001 2 */ |
1105 |
// 0001 3 |
/* 0001 3 */ |
1106 |
int32_t __inline |
int32_t __inline |
1107 |
get_mbtype(Bitstream * bs) |
get_mbtype(Bitstream * bs) |
1108 |
{ |
{ |
1139 |
|
|
1140 |
start_timer(); |
start_timer(); |
1141 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1142 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1143 |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1144 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height); |
1145 |
stop_edges_timer(); |
stop_edges_timer(); |
1146 |
|
|
1147 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1151 |
#endif |
#endif |
1152 |
|
|
1153 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
1154 |
// Initialize Pred Motion Vector |
/* Initialize Pred Motion Vector */ |
1155 |
dec->p_fmv = dec->p_bmv = zeromv; |
dec->p_fmv = dec->p_bmv = zeromv; |
1156 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
1157 |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
1161 |
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] = |
1162 |
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; |
1163 |
|
|
1164 |
// the last P_VOP is skip macroblock ? |
/* the last P_VOP is skip macroblock ? */ |
1165 |
if (last_mb->mode == MODE_NOT_CODED) { |
if (last_mb->mode == MODE_NOT_CODED) { |
1166 |
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
/*DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); */ |
1167 |
mb->cbp = 0; |
mb->cbp = 0; |
1168 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1169 |
mb->mb_type = MODE_NOT_CODED; |
mb->mb_type = MODE_NOT_CODED; |
1171 |
#endif |
#endif |
1172 |
mb->mb_type = MODE_FORWARD; |
mb->mb_type = MODE_FORWARD; |
1173 |
mb->quant = last_mb->quant; |
mb->quant = last_mb->quant; |
1174 |
//mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
/*mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; */ |
1175 |
//mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
/*mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; */ |
1176 |
|
|
1177 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
1178 |
continue; |
continue; |
1179 |
} |
} |
1180 |
|
|
1181 |
if (!BitstreamGetBit(bs)) { // modb=='0' |
if (!BitstreamGetBit(bs)) { /* modb=='0' */ |
1182 |
const uint8_t modb2 = BitstreamGetBit(bs); |
const uint8_t modb2 = BitstreamGetBit(bs); |
1183 |
|
|
1184 |
mb->mb_type = get_mbtype(bs); |
mb->mb_type = get_mbtype(bs); |
1185 |
|
|
1186 |
if (!modb2) { // modb=='00' |
if (!modb2) { /* modb=='00' */ |
1187 |
mb->cbp = BitstreamGetBits(bs, 6); |
mb->cbp = BitstreamGetBits(bs, 6); |
1188 |
} else { |
} else { |
1189 |
mb->cbp = 0; |
mb->cbp = 0; |
1204 |
|
|
1205 |
mb->quant = quant; |
mb->quant = quant; |
1206 |
mb->mode = MODE_INTER4V; |
mb->mode = MODE_INTER4V; |
1207 |
//DEBUG1("Switch bm_type=",mb->mb_type); |
/*DEBUG1("Switch bm_type=",mb->mb_type); */ |
1208 |
|
|
1209 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1210 |
BFRAME_DEBUG |
BFRAME_DEBUG |
1233 |
/ TRD |
/ TRD |
1234 |
: mb->mvs[i].y - last_mb->mvs[i].y); |
: mb->mvs[i].y - last_mb->mvs[i].y); |
1235 |
} |
} |
1236 |
//DEBUG("B-frame Direct!\n"); |
/*DEBUG("B-frame Direct!\n"); */ |
1237 |
} |
} |
1238 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1239 |
mb, x, y, bs); |
mb, x, y, bs); |
1251 |
|
|
1252 |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1253 |
mb, x, y, bs); |
mb, x, y, bs); |
1254 |
//DEBUG("B-frame Bidir!\n"); |
/*DEBUG("B-frame Bidir!\n"); */ |
1255 |
break; |
break; |
1256 |
|
|
1257 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
1261 |
|
|
1262 |
mb->mode = MODE_INTER; |
mb->mode = MODE_INTER; |
1263 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1264 |
//DEBUG("B-frame Backward!\n"); |
/*DEBUG("B-frame Backward!\n"); */ |
1265 |
break; |
break; |
1266 |
|
|
1267 |
case MODE_FORWARD: |
case MODE_FORWARD: |
1271 |
|
|
1272 |
mb->mode = MODE_INTER; |
mb->mode = MODE_INTER; |
1273 |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1274 |
//DEBUG("B-frame Forward!\n"); |
/*DEBUG("B-frame Forward!\n"); */ |
1275 |
break; |
break; |
1276 |
|
|
1277 |
default: |
default: |
1278 |
DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type); |
|
; |
|
1279 |
} |
} |
1280 |
|
|
1281 |
if ( (x==19) && (y==8) ) |
} /* end of FOR */ |
|
{ |
|
|
fprintf(stderr,"D %d %d %d %d %d %d \n",0, mb->mb_type, |
|
|
mb->mvs[0].x, mb->mvs[0].y,mb->b_mvs[0].x, mb->b_mvs[0].y ); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} // end of FOR |
|
1282 |
} |
} |
1283 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1284 |
if (!first){ |
if (!first){ |
1289 |
#endif |
#endif |
1290 |
} |
} |
1291 |
|
|
1292 |
// swap two MACROBLOCK array |
/* swap two MACROBLOCK array */ |
1293 |
void |
void |
1294 |
mb_swap(MACROBLOCK ** mb1, |
mb_swap(MACROBLOCK ** mb1, |
1295 |
MACROBLOCK ** mb2) |
MACROBLOCK ** mb2) |
1319 |
|
|
1320 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1321 |
|
|
1322 |
// add by chenm001 <chenm001@163.com> |
/* add by chenm001 <chenm001@163.com> */ |
1323 |
// for support B-frame to reference last 2 frame |
/* for support B-frame to reference last 2 frame */ |
1324 |
dec->frames++; |
dec->frames++; |
1325 |
vop_type = |
vop_type = |
1326 |
BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, |
BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, |
1327 |
&fcode_backward, &intra_dc_threshold); |
&fcode_backward, &intra_dc_threshold); |
1328 |
|
|
1329 |
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 */ |
1330 |
|
|
1331 |
switch (vop_type) { |
switch (vop_type) { |
1332 |
case P_VOP: |
case P_VOP: |
1333 |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
1334 |
intra_dc_threshold); |
intra_dc_threshold); |
1335 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1336 |
DEBUG1("P_VOP Time=", dec->time); |
DPRINTF(DPRINTF_DEBUG, "P_VOP Time=%d", dec->time); |
1337 |
#endif |
#endif |
1338 |
break; |
break; |
1339 |
|
|
1340 |
case I_VOP: |
case I_VOP: |
1341 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1342 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1343 |
DEBUG1("I_VOP Time=", dec->time); |
DPRINTF(DPRINTF_DEBUG, "I_VOP Time=%d", dec->time); |
1344 |
#endif |
#endif |
1345 |
break; |
break; |
1346 |
|
|
1347 |
case B_VOP: |
case B_VOP: |
1348 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1349 |
if (dec->time_pp > dec->time_bp) { |
if (dec->time_pp > dec->time_bp) { |
1350 |
DEBUG1("B_VOP Time=", dec->time); |
DPRINTF(DPRINTF_DEBUG, "B_VOP Time=%d", dec->time); |
1351 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1352 |
} else { |
} else { |
1353 |
DEBUG("broken B-frame!"); |
DPRINTF(DPRINTF_DEBUG, "Broken B_VOP"); |
1354 |
} |
} |
1355 |
#else |
#else |
1356 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1357 |
#endif |
#endif |
1358 |
break; |
break; |
1359 |
|
|
1360 |
case N_VOP: // vop not coded |
case N_VOP: /* vop not coded */ |
1361 |
// 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 */ |
1362 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1363 |
break; |
break; |
1364 |
|
|
1368 |
|
|
1369 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |
1370 |
if (frame->length != BitstreamPos(&bs) / 8){ |
if (frame->length != BitstreamPos(&bs) / 8){ |
1371 |
DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8); |
DPRINTF(DPRINTF_DEBUG, "InLen: %d / UseLen: %d", frame->length, BitstreamPos(&bs) / 8); |
1372 |
} |
} |
1373 |
#endif |
#endif |
1374 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1375 |
|
|
1376 |
|
|
1377 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1378 |
// test if no B_VOP |
/* test if no B_VOP */ |
1379 |
if (dec->low_delay) { |
if (dec->low_delay || dec->frames == 0) { |
1380 |
#endif |
#endif |
1381 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1382 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1383 |
|
|
1384 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1385 |
} else { |
} else { |
1386 |
if (dec->frames >= 0) { |
if (dec->frames >= 1) { |
1387 |
start_timer(); |
start_timer(); |
1388 |
if ((vop_type == I_VOP || vop_type == P_VOP)) { |
if ((vop_type == I_VOP || vop_type == P_VOP)) { |
1389 |
image_output(&dec->refn[0], dec->width, dec->height, |
image_output(&dec->refn[0], dec->width, dec->height, |
1403 |
image_swap(&dec->refn[0], &dec->refn[1]); |
image_swap(&dec->refn[0], &dec->refn[1]); |
1404 |
image_swap(&dec->cur, &dec->refn[0]); |
image_swap(&dec->cur, &dec->refn[0]); |
1405 |
|
|
1406 |
// swap MACROBLOCK |
/* swap MACROBLOCK */ |
1407 |
// the Divx will not set the low_delay flage some times |
/* the Divx will not set the low_delay flage some times */ |
1408 |
// so follow code will wrong to not swap at that time |
/* so follow code will wrong to not swap at that time */ |
1409 |
// this will broken bitstream! so I'm change it, |
/* this will broken bitstream! so I'm change it, */ |
1410 |
// But that is not the best way! can anyone tell me how |
/* But that is not the best way! can anyone tell me how */ |
1411 |
// to do another way? |
/* to do another way? */ |
1412 |
// 18-07-2002 MinChen<chenm001@163.com> |
/* 18-07-2002 MinChen<chenm001@163.com> */ |
1413 |
//if (!dec->low_delay && vop_type == P_VOP) |
/*if (!dec->low_delay && vop_type == P_VOP) */ |
1414 |
if (vop_type == P_VOP) |
if (vop_type == P_VOP) |
1415 |
mb_swap(&dec->mbs, &dec->last_mbs); |
mb_swap(&dec->mbs, &dec->last_mbs); |
1416 |
} |
} |