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.102 2004-03-22 22:36:23 edgomez Exp $ |
* $Id: encoder.c,v 1.106 2004-07-17 11:42:46 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
57 |
Bitstream * bs); |
Bitstream * bs); |
58 |
|
|
59 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
60 |
Bitstream * bs, |
Bitstream * bs); |
|
bool force_inter, |
|
|
bool vol_header); |
|
61 |
|
|
62 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
63 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
100 |
i--; |
i--; |
101 |
} |
} |
102 |
|
|
103 |
/* if neccessary, round to 65535 accuracy */ |
if (*base > 65535 || *inc > 65535) { |
104 |
if (*base > 65535) { |
int *biggest; |
105 |
float div = (float) *base / 65535; |
int *other; |
106 |
*base = (int) (*base / div); |
|
107 |
*inc = (int) (*inc / div); |
if (*base > *inc) { |
108 |
|
biggest = base; |
109 |
|
other = inc; |
110 |
|
} else { |
111 |
|
biggest = inc; |
112 |
|
other = base; |
113 |
|
} |
114 |
|
|
115 |
|
float div = ((float)*biggest)/((float)65535); |
116 |
|
*biggest = (int)(((float)*biggest)/div); |
117 |
|
*other = (int)(((float)*other)/div); |
118 |
} |
} |
119 |
} |
} |
120 |
|
|
131 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
132 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
133 |
|
|
134 |
|
if (create->width<=0 || create->height<=0) |
135 |
|
return XVID_ERR_FAIL; |
136 |
|
|
137 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
138 |
|
|
139 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
1097 |
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", |
1098 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1099 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1100 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1101 |
|
|
1102 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs); |
1103 |
|
|
1104 |
|
|
1105 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1351 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1352 |
} |
} |
1353 |
|
|
1354 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1355 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1356 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1357 |
|
goto done; |
1358 |
|
} |
1359 |
} |
} |
1360 |
|
|
1361 |
|
|
1533 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1534 |
static int |
static int |
1535 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1536 |
Bitstream * bs, |
Bitstream * bs) |
|
bool force_inter, |
|
|
bool vol_header) |
|
1537 |
{ |
{ |
1538 |
float fSigma; |
float fSigma; |
1539 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1541 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1542 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1543 |
|
|
|
int iLimit; |
|
1544 |
int x, y, k; |
int x, y, k; |
1545 |
int iSearchRange; |
int iSearchRange; |
1546 |
int bIntra=0, skip_possible; |
int skip_possible; |
1547 |
FRAMEINFO *const current = pEnc->current; |
FRAMEINFO *const current = pEnc->current; |
1548 |
FRAMEINFO *const reference = pEnc->reference; |
FRAMEINFO *const reference = pEnc->reference; |
1549 |
MBParam * const pParam = &pEnc->mbParam; |
MBParam * const pParam = &pEnc->mbParam; |
1550 |
int mb_width = pParam->mb_width; |
int mb_width = pParam->mb_width; |
1551 |
int mb_height = pParam->mb_height; |
int mb_height = pParam->mb_height; |
1552 |
|
int coded = 1; |
1553 |
|
|
1554 |
|
|
1555 |
/* IMAGE *pCurrent = ¤t->image; */ |
/* IMAGE *pCurrent = ¤t->image; */ |
1574 |
current->rounding_type = pParam->m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1575 |
current->fcode = pParam->m_fcode; |
current->fcode = pParam->m_fcode; |
1576 |
|
|
|
if (!force_inter) |
|
|
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
|
|
else |
|
|
iLimit = mb_width * mb_height + 1; |
|
|
|
|
1577 |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1578 |
if (reference->is_interpolated != current->rounding_type) { |
if (reference->is_interpolated != current->rounding_type) { |
1579 |
start_timer(); |
start_timer(); |
1644 |
} |
} |
1645 |
} |
} |
1646 |
|
|
|
bIntra = |
|
1647 |
MotionEstimation(&pEnc->mbParam, current, reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1648 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1649 |
&pEnc->vGMC, iLimit); |
&pEnc->vGMC, 256*4096); |
1650 |
|
|
1651 |
|
|
1652 |
stop_motion_timer(); |
stop_motion_timer(); |
1653 |
|
|
|
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
|
|
|
|
1654 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
|
if (vol_header) |
|
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); |
|
|
BitstreamPad(bs); |
|
|
} |
|
1655 |
|
|
1656 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1657 |
|
|
1664 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1665 |
¤t->mbs[x + y * pParam->mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
1666 |
|
|
1667 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
int bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1668 |
|
|
1669 |
if (bIntra) { |
if (bIntra) { |
1670 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1865 |
#if 0 |
#if 0 |
1866 |
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); |
1867 |
#endif |
#endif |
1868 |
if (current->sStat.kblks + current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks <= |
1869 |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1870 |
|
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1871 |
{ |
{ |
1872 |
current->sStat.kblks = current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1873 |
current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1883 |
current->rounding_type = reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1884 |
current->fcode = reference->fcode; |
current->fcode = reference->fcode; |
1885 |
current->bcode = reference->bcode; |
current->bcode = reference->bcode; |
1886 |
|
current->stamp = reference->stamp; |
1887 |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1888 |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1889 |
} |
coded = 0; |
1890 |
|
|
1891 |
|
} else { |
1892 |
|
|
1893 |
pEnc->current->is_edged = 0; /* not edged */ |
pEnc->current->is_edged = 0; /* not edged */ |
1894 |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1899 |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1900 |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1901 |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1902 |
|
} |
1903 |
|
|
1904 |
/* XXX: debug |
/* XXX: debug |
1905 |
{ |
{ |
1920 |
|
|
1921 |
current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1922 |
|
|
1923 |
return 0; /* inter */ |
return coded; |
1924 |
} |
} |
1925 |
|
|
1926 |
|
|