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.37 2003-08-03 10:20:12 syskin Exp $ |
* $Id: encoder.c,v 1.95.2.47 2003-11-05 16:15:47 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
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 |
|
|
389 |
image_null(&pEnc->queue[n].image); |
image_null(&pEnc->queue[n].image); |
390 |
|
|
391 |
|
|
392 |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
|
{ |
|
393 |
if (image_create |
if (image_create |
394 |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
395 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
396 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
|
|
|
397 |
} |
} |
398 |
|
|
|
|
|
399 |
/* timestamp stuff */ |
/* timestamp stuff */ |
400 |
|
|
401 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
420 |
|
|
421 |
xvid_err_memory5: |
xvid_err_memory5: |
422 |
|
|
423 |
if (pEnc->mbParam.max_bframes > 0) { |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
424 |
int i; |
image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
|
|
|
|
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
|
425 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
426 |
} |
} |
427 |
|
|
428 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
|
} |
|
429 |
|
|
430 |
xvid_err_memory4: |
xvid_err_memory4: |
431 |
|
|
439 |
|
|
440 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
441 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
442 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
443 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
444 |
} |
} |
445 |
|
|
446 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
529 |
int i; |
int i; |
530 |
|
|
531 |
/* B Frames specific */ |
/* B Frames specific */ |
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
|
|
532 |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
|
533 |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
534 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
535 |
} |
} |
|
xvid_free(pEnc->queue); |
|
|
} |
|
536 |
|
|
537 |
|
xvid_free(pEnc->queue); |
538 |
|
|
539 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
540 |
|
|
545 |
|
|
546 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
547 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
548 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
549 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
550 |
} |
} |
551 |
|
|
755 |
} |
} |
756 |
|
|
757 |
/* call plugins */ |
/* call plugins */ |
758 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
759 |
emms(); |
emms(); |
760 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
761 |
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) { |
994 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
995 |
|
|
996 |
/* convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
997 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
998 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
999 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1000 |
|
|
1001 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1002 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1084 |
|
|
1085 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1086 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1087 |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
"%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
1088 |
} |
} |
1089 |
|
|
1090 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1163 |
} |
} |
1164 |
|
|
1165 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1166 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1167 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1168 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1169 |
type = P_VOP; |
type = P_VOP; |
1170 |
} |
} |
1171 |
|
|
1188 |
|
|
1189 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1190 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1191 |
|
switch(frame->par) { |
1192 |
|
case XVID_PAR_11_VGA: |
1193 |
|
case XVID_PAR_43_PAL: |
1194 |
|
case XVID_PAR_43_NTSC: |
1195 |
|
case XVID_PAR_169_PAL: |
1196 |
|
case XVID_PAR_169_NTSC: |
1197 |
|
case XVID_PAR_EXT: |
1198 |
|
pEnc->mbParam.par = frame->par; |
1199 |
|
break; |
1200 |
|
default: |
1201 |
|
pEnc->mbParam.par = XVID_PAR_EXT; |
1202 |
|
break; |
1203 |
|
} |
1204 |
|
pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; |
1205 |
|
pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; |
1206 |
|
|
1207 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1208 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1256 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1257 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1258 |
|
|
1259 |
done_flush: |
/*done_flush:*/ |
1260 |
|
|
1261 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1262 |
|
|
1410 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1411 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1412 |
|
|
1413 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1414 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1415 |
|
|
1416 |
return 1; /* intra */ |
return 1; /* intra */ |
1417 |
} |
} |
1418 |
|
|
1455 |
} |
} |
1456 |
|
|
1457 |
|
|
1458 |
|
if (!reference->is_edged) { |
1459 |
start_timer(); |
start_timer(); |
1460 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1461 |
pParam->width, pParam->height); |
pParam->width, pParam->height); |
1462 |
stop_edges_timer(); |
stop_edges_timer(); |
1463 |
|
reference->is_edged = 1; |
1464 |
|
} |
1465 |
|
|
1466 |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1467 |
current->rounding_type = pParam->m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1473 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1474 |
|
|
1475 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1476 |
|
if (reference->is_interpolated != current->rounding_type) { |
1477 |
start_timer(); |
start_timer(); |
1478 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1479 |
&pEnc->vInterHV, pParam->edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1481 |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1482 |
current->rounding_type); |
current->rounding_type); |
1483 |
stop_inter_timer(); |
stop_inter_timer(); |
1484 |
|
reference->is_interpolated = current->rounding_type; |
1485 |
|
} |
1486 |
} |
} |
1487 |
|
|
1488 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1581 |
|
|
1582 |
current->sStat.kblks++; |
current->sStat.kblks++; |
1583 |
|
|
1584 |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1585 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1586 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1587 |
|
qcoeff[5*64+0]=0; |
1588 |
|
} |
1589 |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1590 |
stop_coding_timer(); |
stop_coding_timer(); |
1591 |
continue; |
continue; |
1790 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1791 |
} |
} |
1792 |
|
|
1793 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1794 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1795 |
|
|
1796 |
|
/* what was this frame's interpolated reference will become |
1797 |
|
forward (past) reference in b-frame coding */ |
1798 |
|
|
1799 |
|
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1800 |
|
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1801 |
|
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1802 |
|
|
1803 |
|
|
1804 |
/* XXX: debug |
/* XXX: debug |
1805 |
{ |
{ |
1806 |
char s[100]; |
char s[100]; |
1852 |
#endif |
#endif |
1853 |
|
|
1854 |
/* forward */ |
/* forward */ |
1855 |
|
if (!pEnc->reference->is_edged) { |
1856 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1857 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1858 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1859 |
|
pEnc->current->is_edged = 1; |
1860 |
|
} |
1861 |
|
|
1862 |
|
if (pEnc->reference->is_interpolated != 0) { |
1863 |
start_timer(); |
start_timer(); |
1864 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1865 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1866 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1867 |
stop_inter_timer(); |
stop_inter_timer(); |
1868 |
|
pEnc->reference->is_interpolated = 0; |
1869 |
|
} |
1870 |
|
|
1871 |
/* backward */ |
/* backward */ |
1872 |
|
if (!pEnc->current->is_edged) { |
1873 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1874 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1875 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
1876 |
|
pEnc->current->is_edged = 1; |
1877 |
|
} |
1878 |
|
|
1879 |
|
if (pEnc->current->is_interpolated != 0) { |
1880 |
start_timer(); |
start_timer(); |
1881 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1882 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1883 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1884 |
stop_inter_timer(); |
stop_inter_timer(); |
1885 |
|
pEnc->current->is_interpolated = 0; |
1886 |
|
} |
1887 |
|
|
1888 |
start_timer(); |
start_timer(); |
1889 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1910 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1911 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1912 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; |
|
1913 |
|
|
1914 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1915 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1936 |
} |
} |
1937 |
} |
} |
1938 |
|
|
1939 |
#ifdef BFRAMES_DEC_DEBUG |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
1940 |
BFRAME_DEBUG |
* coding function for BFrames, that's why we don't zero teh DC |
1941 |
#endif |
* coeffs */ |
1942 |
|
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
1943 |
|
mb->cbp &= 0x3C; |
1944 |
|
|
1945 |
start_timer(); |
start_timer(); |
1946 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
1947 |
&frame->sStat, direction); |
&frame->sStat); |
1948 |
stop_coding_timer(); |
stop_coding_timer(); |
1949 |
} |
} |
1950 |
} |
} |