21 |
* along with this program ; if not, write to the Free Software |
* along with this program ; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: encoder.c,v 1.95.2.33 2003-07-13 09:57:51 syskin Exp $ |
* $Id: encoder.c,v 1.95.2.40 2003-08-26 14:07:11 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
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 |
|
|
1199 |
|
|
1200 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1201 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1202 |
|
switch(frame->par) { |
1203 |
|
case XVID_PAR_11_VGA: |
1204 |
|
case XVID_PAR_43_PAL: |
1205 |
|
case XVID_PAR_43_NTSC: |
1206 |
|
case XVID_PAR_169_PAL: |
1207 |
|
case XVID_PAR_169_NTSC: |
1208 |
|
case XVID_PAR_EXT: |
1209 |
|
pEnc->mbParam.par = frame->par; |
1210 |
|
break; |
1211 |
|
default: |
1212 |
|
pEnc->mbParam.par = XVID_PAR_EXT; |
1213 |
|
break; |
1214 |
|
} |
1215 |
|
pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; |
1216 |
|
pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; |
1217 |
|
|
1218 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1219 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1267 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1268 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1269 |
|
|
1270 |
done_flush: |
/*done_flush:*/ |
1271 |
|
|
1272 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1273 |
|
|
1299 |
{ |
{ |
1300 |
unsigned int i,j; |
unsigned int i,j; |
1301 |
int quant = frame->quant; |
int quant = frame->quant; |
1302 |
|
if (quant > 31) |
1303 |
|
frame->quant = quant = 31; |
1304 |
|
else if (quant < 1) |
1305 |
|
frame->quant = quant = 1; |
1306 |
|
|
1307 |
for (j=0; j<pParam->mb_height; j++) |
for (j=0; j<pParam->mb_height; j++) |
1308 |
for (i=0; i<pParam->mb_width; i++) { |
for (i=0; i<pParam->mb_width; i++) { |
1310 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1311 |
if (quant > 31) |
if (quant > 31) |
1312 |
quant = 31; |
quant = 31; |
1313 |
if (quant < 1) |
else if (quant < 1) |
1314 |
quant = 1; |
quant = 1; |
1315 |
pMB->quant = quant; |
pMB->quant = quant; |
1316 |
} |
} |
1374 |
|
|
1375 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1376 |
|
|
1377 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1378 |
|
|
1379 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1380 |
|
|
1381 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1414 |
} |
} |
1415 |
emms(); |
emms(); |
1416 |
|
|
1417 |
/* 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() */ |
1418 |
#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 |
|
1419 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1420 |
|
|
1421 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1498 |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1499 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1500 |
|
|
1501 |
if (current->motion_flags & XVID_GME_REFINE) { |
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1502 |
gmcval = GlobalMotionEstRefine(¤t->warp, |
gmcval = GlobalMotionEstRefine(¤t->warp, |
1503 |
current->mbs, pParam, |
current->mbs, pParam, |
1504 |
current, reference, |
current, reference, |
1556 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1557 |
if (vol_header) |
if (vol_header) |
1558 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1559 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1560 |
} |
} |
1561 |
|
|
1562 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
1583 |
|
|
1584 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1585 |
|
|
1586 |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1587 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1588 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1589 |
|
qcoeff[5*64+0]=0; |
1590 |
|
} |
1591 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1592 |
stop_coding_timer(); |
stop_coding_timer(); |
1593 |
continue; |
continue; |
1807 |
} |
} |
1808 |
*/ |
*/ |
1809 |
|
|
1810 |
/* 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 |
|
1811 |
|
|
1812 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1813 |
|
|
1914 |
} |
} |
1915 |
} |
} |
1916 |
|
|
1917 |
#ifdef BFRAMES_DEC_DEBUG |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
1918 |
BFRAME_DEBUG |
* coding function for BFrames, that's why we don't zero teh DC |
1919 |
#endif |
* coeffs */ |
1920 |
|
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
1921 |
|
mb->cbp &= 0x3C; |
1922 |
|
|
1923 |
start_timer(); |
start_timer(); |
1924 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1925 |
&frame->sStat, direction); |
&frame->sStat, direction); |
1931 |
|
|
1932 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1933 |
|
|
1934 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1935 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1936 |
|
|
1937 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |