32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
36 |
|
* MinChen <chenm001@163.com> |
37 |
* 20.06.2002 bframe patch |
* 20.06.2002 bframe patch |
38 |
* 08.05.2002 fix some problem in DEBUG mode; |
* 08.05.2002 fix some problem in DEBUG mode; |
39 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.51 2002-06-30 10:46:29 suxen_drol Exp $ |
* $Id: encoder.c,v 1.62 2002-07-21 23:34:07 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
68 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
69 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
70 |
|
|
71 |
|
#ifdef _SMP |
72 |
|
#include "motion/smp_motion_est.h" |
73 |
|
#endif |
74 |
/***************************************************************************** |
/***************************************************************************** |
75 |
* Local macros |
* Local macros |
76 |
****************************************************************************/ |
****************************************************************************/ |
209 |
|
|
210 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
211 |
|
|
212 |
if (pParam->max_key_interval == 0) |
if (pParam->max_key_interval <= 0) |
213 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
214 |
|
|
|
|
|
215 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
216 |
if (pEnc == NULL) |
if (pEnc == NULL) |
217 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
236 |
|
|
237 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
238 |
|
|
239 |
|
#ifdef _SMP |
240 |
|
pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); |
241 |
|
#endif |
242 |
|
|
243 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
244 |
|
|
245 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
344 |
pEnc->global = pParam->global; |
pEnc->global = pParam->global; |
345 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
346 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
347 |
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
348 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
349 |
|
|
350 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
422 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
423 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
424 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
425 |
|
pEnc->last_pframe = 1; |
426 |
#endif |
#endif |
427 |
|
|
428 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
853 |
else |
else |
854 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
855 |
|
|
856 |
if (pEnc->current->quant < 1) |
/* if (pEnc->current->quant < 1) |
857 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
858 |
|
|
859 |
if (pEnc->current->quant > 31) |
if (pEnc->current->quant > 31) |
860 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
861 |
|
*/ |
862 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
863 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
864 |
|
|
1005 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1006 |
*/ |
*/ |
1007 |
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
|
|
|
1008 |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1009 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1010 |
} |
} |
1016 |
} else { |
} else { |
1017 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1018 |
} |
} |
1019 |
|
/* if (pEnc->current->quant < 1) |
1020 |
|
pEnc->current->quant = 1; |
1021 |
|
|
1022 |
|
if (pEnc->current->quant > 31) |
1023 |
|
pEnc->current->quant = 31; |
1024 |
|
|
1025 |
|
*/ |
1026 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1027 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1028 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1029 |
|
|
1030 |
|
|
1031 |
|
|
1032 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1033 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1330 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1331 |
MVBLOCKHINT *bhint = |
MVBLOCKHINT *bhint = |
1332 |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1333 |
VECTOR pred[4]; |
VECTOR pred; |
1334 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1335 |
int vec; |
int vec; |
1336 |
|
|
1337 |
pMB->mode = |
pMB->mode = |
1351 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1352 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1353 |
|
|
1354 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
|
0, pred, dummy); |
|
1355 |
|
|
1356 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1357 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1358 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1359 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1360 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
1361 |
} |
} |
1362 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1363 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1370 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1371 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1372 |
|
|
1373 |
get_pmvdata(pEnc->current->mbs, x, y, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
|
1374 |
|
|
1375 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1376 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1377 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1378 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1379 |
} |
} |
1380 |
} else // intra / stuffing / not_coded |
} else // intra / stuffing / not_coded |
1381 |
{ |
{ |
1606 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1607 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1608 |
} else { |
} else { |
1609 |
|
|
1610 |
|
#ifdef _SMP |
1611 |
|
if (pEnc->mbParam.num_threads > 1) |
1612 |
|
bIntra = |
1613 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1614 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1615 |
|
iLimit); |
1616 |
|
else |
1617 |
|
#endif |
1618 |
bIntra = |
bIntra = |
1619 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1620 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1621 |
iLimit); |
iLimit); |
1622 |
|
|
1623 |
} |
} |
1624 |
stop_motion_timer(); |
stop_motion_timer(); |
1625 |
|
|
1731 |
|
|
1732 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1733 |
|
|
1734 |
|
#ifdef BFRAMES |
1735 |
|
/* frame drop code */ |
1736 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1737 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks <= |
1738 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1739 |
|
{ |
1740 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1741 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1742 |
|
|
1743 |
|
BitstreamReset(bs); |
1744 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1745 |
|
|
1746 |
|
// copy reference frame details into the current frame |
1747 |
|
pEnc->current->quant = pEnc->reference->quant; |
1748 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1749 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1750 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1751 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1752 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1753 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1754 |
|
|
1755 |
|
} |
1756 |
|
#endif |
1757 |
|
|
1758 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1759 |
|
|
1760 |
|
#ifdef BFRAMES |
1761 |
|
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->mbParam.m_ticks) % (int32_t)pEnc->mbParam.fbase; |
1762 |
|
pEnc->last_pframe = pEnc->mbParam.m_ticks; |
1763 |
|
#endif |
1764 |
|
|
1765 |
return 0; // inter |
return 0; // inter |
1766 |
} |
} |
1767 |
|
|
1782 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1783 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1784 |
|
|
1785 |
|
#ifdef BFRAMES_DEC_DEBUG |
1786 |
|
FILE *fp; |
1787 |
|
static char first=0; |
1788 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1789 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1790 |
|
} |
1791 |
|
|
1792 |
|
if (!first){ |
1793 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1794 |
|
} |
1795 |
|
#endif |
1796 |
|
|
1797 |
// forward |
// forward |
1798 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1799 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1817 |
stop_inter_timer(); |
stop_inter_timer(); |
1818 |
|
|
1819 |
start_timer(); |
start_timer(); |
1820 |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1821 |
|
((int32_t)pEnc->mbParam.fbase + (int32_t)pEnc->mbParam.m_ticks + 1 - (int32_t)pEnc->last_pframe) % pEnc->mbParam.fbase, |
1822 |
|
pEnc->time_pp, |
1823 |
|
pEnc->reference->mbs, f_ref, |
1824 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1825 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1826 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1863 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1864 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1865 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1866 |
|
|
1867 |
|
mb->cbp = 0; |
1868 |
|
#ifdef BFRAMES_DEC_DEBUG |
1869 |
|
BFRAME_DEBUG |
1870 |
|
#endif |
1871 |
continue; |
continue; |
1872 |
} |
} |
1873 |
|
|
1886 |
|
|
1887 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1888 |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
1889 |
mb->mode = 5; // skipped |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
1890 |
} |
} |
1891 |
|
|
1892 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1904 |
} |
} |
1905 |
// DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y); |
// DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y); |
1906 |
|
|
1907 |
|
#ifdef BFRAMES_DEC_DEBUG |
1908 |
|
BFRAME_DEBUG |
1909 |
|
#endif |
1910 |
start_timer(); |
start_timer(); |
1911 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1912 |
&pEnc->sStat); |
&pEnc->sStat); |
1919 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1920 |
|
|
1921 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1922 |
|
|
1923 |
|
#ifdef BFRAMES_DEC_DEBUG |
1924 |
|
if (!first){ |
1925 |
|
first=1; |
1926 |
|
if (fp) |
1927 |
|
fclose(fp); |
1928 |
|
} |
1929 |
|
#endif |
1930 |
} |
} |
1931 |
#endif |
#endif |