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.57 2003-12-12 09:58:40 syskin Exp $ |
* $Id: encoder.c,v 1.95.2.66 2004-03-30 12:30:48 syskin Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
123 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
124 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
125 |
|
|
126 |
|
if (create->width<=0 || create->height<=0) |
127 |
|
return XVID_ERR_FAIL; |
128 |
|
|
129 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
130 |
|
|
131 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
655 |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
656 |
} |
} |
657 |
|
|
658 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_PLANAR; |
659 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
660 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
661 |
data.reference.plane[2] = pEnc->reference->image.v; |
data.reference.plane[2] = pEnc->reference->image.v; |
663 |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
664 |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
665 |
|
|
666 |
data.current.csp = XVID_CSP_USER; |
data.current.csp = XVID_CSP_PLANAR; |
667 |
data.current.plane[0] = frame->image.y; |
data.current.plane[0] = frame->image.y; |
668 |
data.current.plane[1] = frame->image.u; |
data.current.plane[1] = frame->image.u; |
669 |
data.current.plane[2] = frame->image.v; |
data.current.plane[2] = frame->image.v; |
693 |
|
|
694 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
695 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
696 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_PLANAR; |
697 |
data.original.plane[0] = original->y; |
data.original.plane[0] = original->y; |
698 |
data.original.plane[1] = original->u; |
data.original.plane[1] = original->u; |
699 |
data.original.plane[2] = original->v; |
data.original.plane[2] = original->v; |
743 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
744 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
745 |
|
|
746 |
if (stats) { |
/* New code */ |
747 |
stats->type = coding2type(frame->coding_type); |
data.stats.type = coding2type(frame->coding_type); |
748 |
stats->quant = frame->quant; |
data.stats.quant = frame->quant; |
749 |
stats->vol_flags = frame->vol_flags; |
data.stats.vol_flags = frame->vol_flags; |
750 |
stats->vop_flags = frame->vop_flags; |
data.stats.vop_flags = frame->vop_flags; |
751 |
stats->length = frame->length; |
data.stats.length = frame->length; |
752 |
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); |
753 |
stats->kblks = frame->sStat.kblks; |
data.stats.kblks = frame->sStat.kblks; |
754 |
stats->mblks = frame->sStat.mblks; |
data.stats.mblks = frame->sStat.mblks; |
755 |
stats->ublks = frame->sStat.ublks; |
data.stats.ublks = frame->sStat.ublks; |
756 |
stats->sse_y = data.sse_y; |
data.stats.sse_y = data.sse_y; |
757 |
stats->sse_u = data.sse_u; |
data.stats.sse_u = data.sse_u; |
758 |
stats->sse_v = data.sse_v; |
data.stats.sse_v = data.sse_v; |
759 |
} |
|
760 |
|
if (stats) |
761 |
|
*stats = data.stats; |
762 |
} |
} |
763 |
|
|
764 |
/* call plugins */ |
/* call plugins */ |
853 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
854 |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
855 |
|
|
856 |
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
#if 0 /* HEAVY DEBUG OUTPUT */ |
857 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
|
858 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
859 |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
860 |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
861 |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
862 |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
863 |
|
#endif |
864 |
|
} |
865 |
|
|
866 |
*/ |
static int |
867 |
|
gcd(int a, int b) |
868 |
|
{ |
869 |
|
int r ; |
870 |
|
|
871 |
|
if (b > a) { |
872 |
|
r = a; |
873 |
|
a = b; |
874 |
|
b = r; |
875 |
} |
} |
876 |
|
|
877 |
|
while ((r = a % b)) { |
878 |
|
a = b; |
879 |
|
b = r; |
880 |
|
} |
881 |
|
return b; |
882 |
|
} |
883 |
|
|
884 |
|
static void |
885 |
|
simplify_par(int *par_width, int *par_height) |
886 |
|
{ |
887 |
|
|
888 |
|
int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
889 |
|
int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; |
890 |
|
int divisor = gcd(_par_width, _par_height); |
891 |
|
|
892 |
|
_par_width /= divisor; |
893 |
|
_par_height /= divisor; |
894 |
|
|
895 |
|
/* 2^8 precision maximum */ |
896 |
|
if (_par_width>255 || _par_height>255) { |
897 |
|
float div; |
898 |
|
emms(); |
899 |
|
if (_par_width>_par_height) |
900 |
|
div = (float)_par_width/255; |
901 |
|
else |
902 |
|
div = (float)_par_height/255; |
903 |
|
|
904 |
|
_par_width = (int)((float)_par_width/div); |
905 |
|
_par_height = (int)((float)_par_height/div); |
906 |
|
} |
907 |
|
|
908 |
|
*par_width = _par_width; |
909 |
|
*par_height = _par_height; |
910 |
|
|
911 |
|
return; |
912 |
|
} |
913 |
|
|
914 |
|
|
915 |
/***************************************************************************** |
/***************************************************************************** |
1089 |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1090 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1091 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1092 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1093 |
|
|
1094 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
1095 |
|
|
1125 |
|
|
1126 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1127 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1128 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = frame->vol_flags; |
1129 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1130 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1131 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1160 |
} |
} |
1161 |
} |
} |
1162 |
|
|
1163 |
|
if (type != I_VOP) |
1164 |
|
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ |
1165 |
|
|
1166 |
/* bframes buffer overflow check */ |
/* bframes buffer overflow check */ |
1167 |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1168 |
type = P_VOP; |
type = P_VOP; |
1275 |
pEnc->iFrameNum = 1; |
pEnc->iFrameNum = 1; |
1276 |
|
|
1277 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1278 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->mbParam.vol_flags = pEnc->current->vol_flags; |
1279 |
|
|
1280 |
|
/* Aspect ratio */ |
1281 |
switch(frame->par) { |
switch(frame->par) { |
|
case 0: |
|
|
pEnc->mbParam.par = XVID_PAR_11_VGA; |
|
|
break; |
|
1282 |
case XVID_PAR_11_VGA: |
case XVID_PAR_11_VGA: |
1283 |
case XVID_PAR_43_PAL: |
case XVID_PAR_43_PAL: |
1284 |
case XVID_PAR_43_NTSC: |
case XVID_PAR_43_NTSC: |
1288 |
pEnc->mbParam.par = frame->par; |
pEnc->mbParam.par = frame->par; |
1289 |
break; |
break; |
1290 |
default: |
default: |
1291 |
pEnc->mbParam.par = XVID_PAR_EXT; |
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1292 |
break; |
break; |
1293 |
} |
} |
1294 |
pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; |
|
1295 |
pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; |
/* For extended PAR only, we try to sanityse/simplify par values */ |
1296 |
|
if (pEnc->mbParam.par == XVID_PAR_EXT) { |
1297 |
|
pEnc->mbParam.par_width = frame->par_width; |
1298 |
|
pEnc->mbParam.par_height = frame->par_height; |
1299 |
|
simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); |
1300 |
|
} |
1301 |
|
|
1302 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1303 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1343 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1344 |
} |
} |
1345 |
|
|
1346 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs, 1, 0) == 0 ) { |
1347 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1348 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1349 |
|
goto done; |
1350 |
|
} |
1351 |
} |
} |
1352 |
|
|
1353 |
|
|
1448 |
start_timer(); |
start_timer(); |
1449 |
image_setedges(&pEnc->current->image, |
image_setedges(&pEnc->current->image, |
1450 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1451 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height, 0); |
1452 |
stop_edges_timer(); |
stop_edges_timer(); |
1453 |
} |
} |
1454 |
|
|
1460 |
|
|
1461 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1462 |
|
|
1463 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1464 |
|
|
1465 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1466 |
|
|
1544 |
MBParam * const pParam = &pEnc->mbParam; |
MBParam * const pParam = &pEnc->mbParam; |
1545 |
int mb_width = pParam->mb_width; |
int mb_width = pParam->mb_width; |
1546 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1547 |
|
int coded = 1; |
1548 |
|
|
1549 |
|
|
1550 |
/* IMAGE *pCurrent = ¤t->image; */ |
/* IMAGE *pCurrent = ¤t->image; */ |
1560 |
if (!reference->is_edged) { |
if (!reference->is_edged) { |
1561 |
start_timer(); |
start_timer(); |
1562 |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1563 |
pParam->width, pParam->height); |
pParam->width, pParam->height, 0); |
1564 |
stop_edges_timer(); |
stop_edges_timer(); |
1565 |
reference->is_edged = 1; |
reference->is_edged = 1; |
1566 |
} |
} |
1656 |
|
|
1657 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1658 |
if (vol_header) |
if (vol_header) |
1659 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); |
1660 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1661 |
} |
} |
1662 |
|
|
1872 |
#if 0 |
#if 0 |
1873 |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1874 |
#endif |
#endif |
1875 |
if (current->sStat.kblks + current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks <= |
1876 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
1877 |
{ |
{ |
1878 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1889 |
current->rounding_type = reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1890 |
current->fcode = reference->fcode; |
current->fcode = reference->fcode; |
1891 |
current->bcode = reference->bcode; |
current->bcode = reference->bcode; |
1892 |
|
current->stamp = reference->stamp; |
1893 |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1894 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1895 |
} |
coded = 0; |
1896 |
|
|
1897 |
|
} else { |
1898 |
|
|
1899 |
pEnc->current->is_edged = 0; /* not edged */ |
pEnc->current->is_edged = 0; /* not edged */ |
1900 |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1905 |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1906 |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1907 |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1908 |
|
} |
1909 |
|
|
1910 |
/* XXX: debug |
/* XXX: debug |
1911 |
{ |
{ |
1926 |
|
|
1927 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1928 |
|
|
1929 |
return 0; /* inter */ |
return coded; |
1930 |
} |
} |
1931 |
|
|
1932 |
|
|
1961 |
if (!pEnc->reference->is_edged) { |
if (!pEnc->reference->is_edged) { |
1962 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1963 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1964 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1965 |
pEnc->current->is_edged = 1; |
pEnc->current->is_edged = 1; |
1966 |
} |
} |
1967 |
|
|
1978 |
if (!pEnc->current->is_edged) { |
if (!pEnc->current->is_edged) { |
1979 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1980 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1981 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1982 |
pEnc->current->is_edged = 1; |
pEnc->current->is_edged = 1; |
1983 |
} |
} |
1984 |
|
|