116 |
{ |
{ |
117 |
Encoder *pEnc; |
Encoder *pEnc; |
118 |
int i; |
int i; |
|
|
|
119 |
pParam->handle = NULL; |
pParam->handle = NULL; |
120 |
|
|
121 |
ENC_CHECK(pParam); |
ENC_CHECK(pParam); |
183 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
184 |
|
|
185 |
if (pParam->max_key_interval <= 0) |
if (pParam->max_key_interval <= 0) |
186 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fbase / pParam->fincr; |
187 |
|
|
188 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
189 |
if (pEnc == NULL) |
if (pEnc == NULL) |
365 |
|
|
366 |
pEnc->queue = NULL; |
pEnc->queue = NULL; |
367 |
|
|
|
|
|
368 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
369 |
int n; |
int n; |
370 |
|
|
417 |
|
|
418 |
xvid_err_memory5: |
xvid_err_memory5: |
419 |
|
|
|
|
|
420 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
421 |
|
|
422 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
524 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
525 |
} |
} |
526 |
|
|
|
|
|
527 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
528 |
|
|
529 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
673 |
uint16_t x, y; |
uint16_t x, y; |
674 |
Bitstream bs; |
Bitstream bs; |
675 |
uint32_t bits; |
uint32_t bits; |
676 |
int mode; |
int mode = -1; /* Just to shut up compiler warning */ |
677 |
|
|
678 |
int input_valid = 1; |
int input_valid = 1; |
679 |
int bframes_count = 0; |
int bframes_count = 0; |
752 |
pResult->ublks = pEnc->current->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
753 |
} |
} |
754 |
|
|
755 |
|
emms(); |
756 |
|
|
757 |
|
if (pFrame->quant == 0) { |
758 |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
759 |
|
pFrame->length, pFrame->intra); |
760 |
|
} |
761 |
|
|
762 |
if (input_valid) |
if (input_valid) |
763 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
764 |
|
|
771 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
772 |
|
|
773 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
|
|
|
774 |
for divx5 decoder compatibility, this marker must consist |
for divx5 decoder compatibility, this marker must consist |
775 |
of a not-coded p-vop, with a time_base of zero, and time_increment |
of a not-coded p-vop, with a time_base of zero, and time_increment |
776 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
783 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
784 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
785 |
|
|
|
|
|
786 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
787 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
788 |
|
|
801 |
pResult->ublks = pEnc->current->sStat.ublks; |
pResult->ublks = pEnc->current->sStat.ublks; |
802 |
} |
} |
803 |
|
|
804 |
|
emms(); |
805 |
|
|
806 |
|
if (pFrame->quant == 0) { |
807 |
|
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
808 |
|
pFrame->length, pFrame->intra); |
809 |
|
} |
810 |
|
|
811 |
if (input_valid) |
if (input_valid) |
812 |
queue_image(pEnc, pFrame); |
queue_image(pEnc, pFrame); |
813 |
|
|
889 |
/* That disabled line of code was supposed to inform VirtualDub |
/* That disabled line of code was supposed to inform VirtualDub |
890 |
* that the frame was a dummy delay frame - now disabled (thx god :-) |
* that the frame was a dummy delay frame - now disabled (thx god :-) |
891 |
*/ |
*/ |
892 |
/* BitstreamPutBits(&bs, 0x7f, 8); */ |
//BitstreamPutBits(&bs, 0x7f, 8); |
893 |
pFrame->intra = 5; |
pFrame->intra = 5; |
894 |
|
|
895 |
if (pResult) { |
if (pResult) { |
911 |
pResult->mblks = 0; |
pResult->mblks = 0; |
912 |
pResult->ublks = 0; |
pResult->ublks = 0; |
913 |
} |
} |
|
|
|
914 |
} else { |
} else { |
915 |
|
|
916 |
if (pResult) { |
if (pResult) { |
997 |
|
|
998 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
999 |
} |
} |
|
|
|
1000 |
#undef OFFSET |
#undef OFFSET |
1001 |
} |
} |
|
|
|
1002 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
1003 |
} |
} |
|
|
|
1004 |
} |
} |
1005 |
|
|
1006 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1010 |
|
|
1011 |
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
if (pEnc->iFrameNum == 0 || pEnc->bframenum_dx50bvop >= 0 || |
1012 |
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
(mode < 0 && pEnc->mbParam.iMaxKeyInterval > 0 && |
1013 |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) { |
|
{ |
|
1014 |
mode = I_VOP; |
mode = I_VOP; |
1015 |
}else{ |
}else{ |
1016 |
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
mode = MEanalysis(&pEnc->reference->image, pEnc->current, |
1017 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1018 |
(mode < 0) ? pEnc->iFrameNum : 0, |
(/*mode < 0*/1/*hack*/) ? pEnc->iFrameNum : 0, |
1019 |
bframes_count++); |
bframes_count++, pFrame->bframe_threshold); |
1020 |
} |
} |
1021 |
|
|
1022 |
if (mode == I_VOP) { |
if (mode == I_VOP) { |
1210 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1211 |
|
|
1212 |
float psnr; |
float psnr; |
1213 |
uint8_t temp[128]; |
char temp[128]; |
1214 |
|
|
1215 |
start_global_timer(); |
start_global_timer(); |
1216 |
|
|
1437 |
return; |
return; |
1438 |
} |
} |
1439 |
|
|
1440 |
pEnc->current->fcode = |
pEnc->current->fcode = (hint->rawhints) ? |
1441 |
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
(uint32_t)hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
|
FCODEBITS); |
|
1442 |
|
|
1443 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1444 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1453 |
VECTOR tmp; |
VECTOR tmp; |
1454 |
int vec; |
int vec; |
1455 |
|
|
1456 |
pMB->mode = |
pMB->mode = (hint->rawhints) ? |
1457 |
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
(uint32_t)bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
|
MODEBITS); |
|
1458 |
|
|
1459 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1460 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1461 |
|
|
1462 |
if (pMB->mode == MODE_INTER) { |
if (pMB->mode == MODE_INTER) { |
1463 |
tmp.x = |
tmp.x = (hint->rawhints) ? |
1464 |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
bhint->mvs[0].x : (int)BitstreamGetBits(&bs, length); |
1465 |
length); |
tmp.y = (hint->rawhints) ? |
1466 |
tmp.y = |
bhint->mvs[0].y : (int)BitstreamGetBits(&bs, length); |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
|
|
length); |
|
1467 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1468 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1469 |
|
|
1477 |
} |
} |
1478 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1479 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1480 |
tmp.x = |
tmp.x = (hint->rawhints) ? |
1481 |
(hint->rawhints) ? bhint->mvs[vec]. |
bhint->mvs[vec].x : (int)BitstreamGetBits(&bs, length); |
1482 |
x : BitstreamGetBits(&bs, length); |
tmp.y = (hint->rawhints) ? |
1483 |
tmp.y = |
bhint->mvs[vec].y : (int)BitstreamGetBits(&bs, length); |
|
(hint->rawhints) ? bhint->mvs[vec]. |
|
|
y : BitstreamGetBits(&bs, length); |
|
1484 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1485 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1486 |
|
|
1628 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height); |
1629 |
stop_edges_timer(); |
stop_edges_timer(); |
1630 |
} |
} |
|
|
|
1631 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1632 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1633 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1725 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1726 |
} |
} |
1727 |
|
|
|
|
|
1728 |
start_timer(); |
start_timer(); |
1729 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1730 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height); |
2146 |
|
|
2147 |
|
|
2148 |
stop_motion_timer(); |
stop_motion_timer(); |
|
|
|
2149 |
/* |
/* |
2150 |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) { |
if (test_quant_type(&pEnc->mbParam, pEnc->current)) { |
2151 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |