117 |
Encoder *pEnc; |
Encoder *pEnc; |
118 |
int n; |
int n; |
119 |
|
|
120 |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
121 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
122 |
|
|
123 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
226 |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
227 |
|
|
228 |
/* max keyframe interval */ |
/* max keyframe interval */ |
229 |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval; |
|
(10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval; |
|
230 |
|
|
231 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
232 |
|
|
740 |
|
|
741 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
742 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
743 |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant; |
744 |
} |
} |
745 |
} |
} |
746 |
|
|
770 |
} |
} |
771 |
|
|
772 |
/* call plugins */ |
/* call plugins */ |
773 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
774 |
emms(); |
emms(); |
775 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
776 |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
866 |
int type; |
int type; |
867 |
Bitstream bs; |
Bitstream bs; |
868 |
|
|
869 |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ |
870 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
871 |
|
|
872 |
xFrame->out_flags = 0; |
xFrame->out_flags = 0; |
903 |
|
|
904 |
if (xFrame->quant_intra_matrix) |
if (xFrame->quant_intra_matrix) |
905 |
{ |
{ |
906 |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix)); |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char)); |
907 |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
908 |
} |
} |
909 |
|
|
910 |
if (xFrame->quant_inter_matrix) |
if (xFrame->quant_inter_matrix) |
911 |
{ |
{ |
912 |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix)); |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char)); |
913 |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
914 |
} |
} |
915 |
|
|
1252 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1253 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1254 |
|
|
1255 |
done_flush: |
/*done_flush:*/ |
1256 |
|
|
1257 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1258 |
|
|
1284 |
{ |
{ |
1285 |
unsigned int i,j; |
unsigned int i,j; |
1286 |
int quant = frame->quant; |
int quant = frame->quant; |
1287 |
|
if (quant > 31) |
1288 |
|
frame->quant = quant = 31; |
1289 |
|
else if (quant < 1) |
1290 |
|
frame->quant = quant = 1; |
1291 |
|
|
1292 |
for (j=0; j<pParam->mb_height; j++) |
for (j=0; j<pParam->mb_height; j++) |
1293 |
for (i=0; i<pParam->mb_width; i++) { |
for (i=0; i<pParam->mb_width; i++) { |
1295 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1296 |
if (quant > 31) |
if (quant > 31) |
1297 |
quant = 31; |
quant = 31; |
1298 |
if (quant < 1) |
else if (quant < 1) |
1299 |
quant = 1; |
quant = 1; |
1300 |
pMB->quant = quant; |
pMB->quant = quant; |
1301 |
} |
} |
1359 |
|
|
1360 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1361 |
|
|
1362 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1363 |
|
|
1364 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1365 |
|
|
1366 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1399 |
} |
} |
1400 |
emms(); |
emms(); |
1401 |
|
|
1402 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1403 |
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1404 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1405 |
|
|
1406 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1483 |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1484 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1485 |
|
|
1486 |
if (current->motion_flags & XVID_GME_REFINE) { |
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1487 |
gmcval = GlobalMotionEstRefine(¤t->warp, |
gmcval = GlobalMotionEstRefine(¤t->warp, |
1488 |
current->mbs, pParam, |
current->mbs, pParam, |
1489 |
current, reference, |
current, reference, |
1541 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1542 |
if (vol_header) |
if (vol_header) |
1543 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1544 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1545 |
} |
} |
1546 |
|
|
1547 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
1555 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1556 |
¤t->mbs[x + y * pParam->mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
1557 |
|
|
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
|
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
|
|
|
|
1558 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1559 |
|
|
1560 |
if (bIntra) { |
if (bIntra) { |
1568 |
|
|
1569 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1570 |
|
|
1571 |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1572 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1573 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1574 |
|
qcoeff[5*64+0]=0; |
1575 |
|
} |
1576 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1577 |
stop_coding_timer(); |
stop_coding_timer(); |
1578 |
continue; |
continue; |
1579 |
} |
} |
1580 |
|
|
|
if (current->coding_type == S_VOP) { |
|
|
|
|
|
int32_t iSAD = sad16(current->image.y + 16*y*pParam->edged_width + 16*x, |
|
|
pEnc->vGMC.y + 16*y*pParam->edged_width + 16*x, |
|
|
pParam->edged_width, 65536); |
|
|
|
|
|
if (current->motion_flags & XVID_ME_CHROMA16) { |
|
|
iSAD += sad8(current->image.u + 8*y*(pParam->edged_width/2) + 8*x, |
|
|
pEnc->vGMC.u + 8*y*(pParam->edged_width/2) + 8*x, pParam->edged_width/2); |
|
|
|
|
|
iSAD += sad8(current->image.v + 8*y*(pParam->edged_width/2) + 8*x, |
|
|
pEnc->vGMC.v + 8*y*(pParam->edged_width/2) + 8*x, pParam->edged_width/2); |
|
|
} |
|
|
|
|
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
|
|
|
|
|
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
|
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
|
|
else |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
|
|
|
|
|
pMB->mode = MODE_INTER; |
|
|
pMB->mcsel = 1; |
|
|
pMB->sad16 = iSAD; |
|
|
} else { |
|
|
pMB->mcsel = 0; |
|
|
} |
|
|
} else { |
|
|
pMB->mcsel = 0; /* just a precaution */ |
|
|
} |
|
|
|
|
1581 |
start_timer(); |
start_timer(); |
1582 |
MBMotionCompensation(pMB, x, y, &reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1583 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1792 |
} |
} |
1793 |
*/ |
*/ |
1794 |
|
|
1795 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
|
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pParam->global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1796 |
|
|
1797 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1798 |
|
|
1899 |
} |
} |
1900 |
} |
} |
1901 |
|
|
1902 |
#ifdef BFRAMES_DEC_DEBUG |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
1903 |
BFRAME_DEBUG |
* coding function for BFrames, that's why we don't zero teh DC |
1904 |
#endif |
* coeffs */ |
1905 |
|
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
1906 |
|
mb->cbp &= 0x3C; |
1907 |
|
|
1908 |
start_timer(); |
start_timer(); |
1909 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1910 |
&frame->sStat, direction); |
&frame->sStat, direction); |
1916 |
|
|
1917 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1918 |
|
|
1919 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1920 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1921 |
|
|
1922 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |