32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
|
* 28.06.2002 added basic resync support to iframe/pframe_decode() |
|
35 |
* 22.06.2002 added primative N_VOP support |
* 22.06.2002 added primative N_VOP support |
36 |
* #define BFRAMES_DEC now enables Minchenm's bframe decoder |
* #define BFRAMES_DEC now enables Minchenm's bframe decoder |
37 |
* 08.05.2002 add low_delay support for B_VOP decode |
* 08.05.2002 add low_delay support for B_VOP decode |
49 |
* 22.12.2001 lock based interpolation |
* 22.12.2001 lock based interpolation |
50 |
* 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> |
51 |
* |
* |
52 |
* $Id: decoder.c,v 1.22 2002-06-28 15:14:40 suxen_drol Exp $ |
* $Id: decoder.c,v 1.25 2002-07-09 01:09:33 chenm001 Exp $ |
53 |
* |
* |
54 |
*************************************************************************/ |
*************************************************************************/ |
55 |
|
|
168 |
} |
} |
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
/* **************************************************************** |
173 |
|
NIC 28.06.2002 |
174 |
|
riscritta la 'decoder_create' per cambiarne l'interfaccia e poterla |
175 |
|
usare nella dll caricata dall'IM1 player |
176 |
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
177 |
|
**************************************************************** */ |
178 |
|
//XVID_DEC_FRAME * frame |
179 |
|
|
180 |
|
int |
181 |
|
IM1_decoder_create(XVID_DEC_PARAM * param,XVID_DEC_FRAME * frame) |
182 |
|
{ |
183 |
|
DECODER *dec; |
184 |
|
Bitstream bs; |
185 |
|
uint32_t rounding; |
186 |
|
uint32_t quant; |
187 |
|
uint32_t fcode_forward; |
188 |
|
uint32_t fcode_backward; |
189 |
|
uint32_t intra_dc_threshold; |
190 |
|
|
191 |
|
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
192 |
|
if (dec == NULL) { |
193 |
|
return XVID_ERR_MEMORY; |
194 |
|
} |
195 |
|
param->handle = dec; |
196 |
|
|
197 |
|
|
198 |
|
BitstreamInit(&bs, frame->bitstream, frame->length);//NIC |
199 |
|
|
200 |
|
//NIC |
201 |
|
BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, |
202 |
|
&fcode_backward, &intra_dc_threshold); |
203 |
|
|
204 |
|
|
205 |
|
param->width = dec->width; //NIC added |
206 |
|
param->height = dec->height; //NIC added |
207 |
|
//dec->width = param->width; //NIC commentate |
208 |
|
//dec->height = param->height; //NIC commentate |
209 |
|
|
210 |
|
dec->mb_width = (dec->width + 15) / 16; |
211 |
|
dec->mb_height = (dec->height + 15) / 16; |
212 |
|
|
213 |
|
dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; |
214 |
|
dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; |
215 |
|
dec->low_delay = 0; |
216 |
|
|
217 |
|
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { |
218 |
|
xvid_free(dec); |
219 |
|
return XVID_ERR_MEMORY; |
220 |
|
} |
221 |
|
|
222 |
|
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
223 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
224 |
|
xvid_free(dec); |
225 |
|
return XVID_ERR_MEMORY; |
226 |
|
} |
227 |
|
// add by chenm001 <chenm001@163.com> |
228 |
|
// for support B-frame to reference last 2 frame |
229 |
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
230 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
231 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
232 |
|
xvid_free(dec); |
233 |
|
return XVID_ERR_MEMORY; |
234 |
|
} |
235 |
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
236 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
237 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
238 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
239 |
|
xvid_free(dec); |
240 |
|
return XVID_ERR_MEMORY; |
241 |
|
} |
242 |
|
|
243 |
|
dec->mbs = |
244 |
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
245 |
|
CACHE_LINE); |
246 |
|
if (dec->mbs == NULL) { |
247 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
248 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
249 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
250 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
251 |
|
xvid_free(dec); |
252 |
|
return XVID_ERR_MEMORY; |
253 |
|
} |
254 |
|
|
255 |
|
memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
256 |
|
|
257 |
|
// add by chenm001 <chenm001@163.com> |
258 |
|
// for skip MB flag |
259 |
|
dec->last_mbs = |
260 |
|
xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, |
261 |
|
CACHE_LINE); |
262 |
|
if (dec->last_mbs == NULL) { |
263 |
|
xvid_free(dec->mbs); |
264 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
265 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
266 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
267 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
268 |
|
xvid_free(dec); |
269 |
|
return XVID_ERR_MEMORY; |
270 |
|
} |
271 |
|
|
272 |
|
memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
273 |
|
|
274 |
|
init_timer(); |
275 |
|
|
276 |
|
// add by chenm001 <chenm001@163.com> |
277 |
|
// for support B-frame to save reference frame's time |
278 |
|
dec->frames = -1; |
279 |
|
dec->time = dec->time_base = dec->last_time_base = 0; |
280 |
|
|
281 |
|
return XVID_ERR_OK; |
282 |
|
} |
283 |
|
/* **************************************************************** |
284 |
|
END NIC |
285 |
|
**************************************************************** */ |
286 |
|
|
287 |
int |
int |
288 |
decoder_destroy(DECODER * dec) |
decoder_destroy(DECODER * dec) |
289 |
{ |
{ |
319 |
const uint32_t cbp, |
const uint32_t cbp, |
320 |
Bitstream * bs, |
Bitstream * bs, |
321 |
const uint32_t quant, |
const uint32_t quant, |
322 |
const uint32_t intra_dc_threshold, |
const uint32_t intra_dc_threshold) |
|
const unsigned int bound_x, |
|
|
const unsigned int bound_y) |
|
323 |
{ |
{ |
324 |
|
|
325 |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
345 |
|
|
346 |
start_timer(); |
start_timer(); |
347 |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
348 |
iQuant, iDcScaler, predictors, bound_x, bound_y); |
iQuant, iDcScaler, predictors); |
349 |
if (!acpred_flag) { |
if (!acpred_flag) { |
350 |
pMB->acpred_directions[i] = 0; |
pMB->acpred_directions[i] = 0; |
351 |
} |
} |
538 |
int quant, |
int quant, |
539 |
int intra_dc_threshold) |
int intra_dc_threshold) |
540 |
{ |
{ |
|
uint32_t bound_x, bound_y; |
|
|
uint32_t x, y; |
|
541 |
|
|
542 |
bound_x = bound_y = 0; |
uint32_t x, y; |
543 |
|
|
544 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
545 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
546 |
MACROBLOCK *mb; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
547 |
|
|
548 |
uint32_t mcbpc; |
uint32_t mcbpc; |
549 |
uint32_t cbpc; |
uint32_t cbpc; |
551 |
uint32_t cbpy; |
uint32_t cbpy; |
552 |
uint32_t cbp; |
uint32_t cbp; |
553 |
|
|
|
skip_stuffing(bs); |
|
|
if (check_resync_marker(bs, 0)) |
|
|
{ |
|
|
int mbnum = read_video_packet_header(bs, 0); |
|
|
x = bound_x = mbnum % dec->mb_width; |
|
|
y = bound_y = mbnum / dec->mb_width; |
|
|
} |
|
|
|
|
|
mb = &dec->mbs[y * dec->mb_width + x]; |
|
|
|
|
554 |
mcbpc = get_mcbpc_intra(bs); |
mcbpc = get_mcbpc_intra(bs); |
555 |
mb->mode = mcbpc & 7; |
mb->mode = mcbpc & 7; |
556 |
cbpc = (mcbpc >> 4); |
cbpc = (mcbpc >> 4); |
557 |
|
|
558 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
559 |
|
|
560 |
|
if (mb->mode == MODE_STUFFING) { |
561 |
|
DEBUG("-- STUFFING ?"); |
562 |
|
continue; |
563 |
|
} |
564 |
|
|
565 |
cbpy = get_cbpy(bs, 1); |
cbpy = get_cbpy(bs, 1); |
566 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
567 |
|
|
581 |
} |
} |
582 |
|
|
583 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
584 |
intra_dc_threshold, bound_x, bound_y); |
intra_dc_threshold); |
585 |
} |
} |
586 |
} |
} |
587 |
|
|
595 |
int y, |
int y, |
596 |
int k, |
int k, |
597 |
VECTOR * mv, |
VECTOR * mv, |
598 |
int fcode, |
int fcode) |
|
const int bound_x, |
|
|
const int bound_y) |
|
599 |
{ |
{ |
600 |
|
|
601 |
int scale_fac = 1 << (fcode - 1); |
int scale_fac = 1 << (fcode - 1); |
610 |
int pmv_x, pmv_y; |
int pmv_x, pmv_y; |
611 |
|
|
612 |
|
|
613 |
get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad, bound_x, bound_y); |
get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); |
614 |
|
|
615 |
pmv_x = pmv[0].x; |
pmv_x = pmv[0].x; |
616 |
pmv_y = pmv[0].y; |
pmv_y = pmv[0].y; |
649 |
{ |
{ |
650 |
|
|
651 |
uint32_t x, y; |
uint32_t x, y; |
|
uint32_t bound_x, bound_y; |
|
652 |
|
|
653 |
start_timer(); |
start_timer(); |
654 |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
655 |
dec->width, dec->height, dec->interlacing); |
dec->width, dec->height, dec->interlacing); |
656 |
stop_edges_timer(); |
stop_edges_timer(); |
657 |
|
|
|
bound_x = bound_y = 0; |
|
|
|
|
658 |
for (y = 0; y < dec->mb_height; y++) { |
for (y = 0; y < dec->mb_height; y++) { |
659 |
for (x = 0; x < dec->mb_width; x++) { |
for (x = 0; x < dec->mb_width; x++) { |
660 |
MACROBLOCK *mb; |
MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; |
|
|
|
|
skip_stuffing(bs); |
|
|
if (check_resync_marker(bs, 0)) |
|
|
{ |
|
|
int mbnum = read_video_packet_header(bs, 0); |
|
|
x = bound_x = mbnum % dec->mb_width; |
|
|
y = bound_y = mbnum / dec->mb_width; |
|
|
} |
|
|
mb = &dec->mbs[y * dec->mb_width + x]; |
|
661 |
|
|
662 |
//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 |
663 |
if (!(BitstreamGetBit(bs))) // not_coded |
if (!(BitstreamGetBit(bs))) // not_coded |
680 |
acpred_flag = BitstreamGetBit(bs); |
acpred_flag = BitstreamGetBit(bs); |
681 |
} |
} |
682 |
|
|
683 |
|
if (mb->mode == MODE_STUFFING) { |
684 |
|
DEBUG("-- STUFFING ?"); |
685 |
|
continue; |
686 |
|
} |
687 |
|
|
688 |
cbpy = get_cbpy(bs, intra); |
cbpy = get_cbpy(bs, intra); |
689 |
cbp = (cbpy << 2) | cbpc; |
cbp = (cbpy << 2) | cbpc; |
690 |
|
|
718 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
719 |
if (dec->interlacing && mb->field_pred) { |
if (dec->interlacing && mb->field_pred) { |
720 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
721 |
fcode, bound_x, bound_y); |
fcode); |
722 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
723 |
fcode, bound_x, bound_y); |
fcode); |
724 |
} else { |
} else { |
725 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], |
726 |
fcode, bound_x, bound_y); |
fcode); |
727 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
728 |
mb->mvs[0].x; |
mb->mvs[0].x; |
729 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = |
731 |
} |
} |
732 |
} else if (mb->mode == |
} else if (mb->mode == |
733 |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */ ) { |
734 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound_x, bound_y); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
735 |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound_x, bound_y); |
get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); |
736 |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound_x, bound_y); |
get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); |
737 |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound_x, bound_y); |
get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); |
738 |
} else // MODE_INTRA, MODE_INTRA_Q |
} else // MODE_INTRA, MODE_INTRA_Q |
739 |
{ |
{ |
740 |
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 = |
742 |
mb->mvs[0].y = 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 = |
743 |
0; |
0; |
744 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
745 |
intra_dc_threshold, bound_x, bound_y); |
intra_dc_threshold); |
746 |
continue; |
continue; |
747 |
} |
} |
748 |
|
|
1359 |
case P_VOP: |
case P_VOP: |
1360 |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, |
1361 |
intra_dc_threshold); |
intra_dc_threshold); |
1362 |
|
printf("P_VOP Time=%ld\n",dec->time); |
1363 |
DEBUG1("P_VOP Time=", dec->time); |
DEBUG1("P_VOP Time=", dec->time); |
1364 |
break; |
break; |
1365 |
|
|
1366 |
case I_VOP: |
case I_VOP: |
1367 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1368 |
|
printf("I_VOP Time=%ld\n",dec->time); |
1369 |
DEBUG1("I_VOP Time=", dec->time); |
DEBUG1("I_VOP Time=", dec->time); |
1370 |
break; |
break; |
1371 |
|
|
1372 |
case B_VOP: |
case B_VOP: |
1373 |
#ifdef BFRAMES_DEC |
#ifdef BFRAMES_DEC |
1374 |
if (dec->time_pp > dec->time_bp) { |
if (dec->time_pp > dec->time_bp) { |
1375 |
|
printf("I_VOP Time=%ld\n",dec->time); |
1376 |
DEBUG1("B_VOP Time=", dec->time); |
DEBUG1("B_VOP Time=", dec->time); |
1377 |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1378 |
} else { |
} else { |
1379 |
DEBUG("broken B-frame!"); |
DEBUG("broken B-frame!"); |
1380 |
} |
} |
1381 |
#else |
#else |
1382 |
|
printf("Che minchia ne so\n"); |
1383 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1384 |
#endif |
#endif |
1385 |
break; |
break; |
1387 |
case N_VOP: // vop not coded |
case N_VOP: // vop not coded |
1388 |
// 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 |
1389 |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); |
1390 |
|
printf("N_VOP vop not coded\n"); |
1391 |
break; |
break; |
1392 |
|
|
1393 |
default: |
default: |