250 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
251 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
252 |
|
|
253 |
|
/* allocate quant matrix memory */ |
254 |
|
|
255 |
|
pEnc->mbParam.mpeg_quant_matrices = |
256 |
|
xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
257 |
|
|
258 |
|
if (pEnc->mbParam.mpeg_quant_matrices == NULL) |
259 |
|
goto xvid_err_memory2a; |
260 |
|
|
261 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
262 |
|
|
263 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
273 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
274 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
275 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
276 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
277 |
|
|
278 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
279 |
if (image_create |
if (image_create |
317 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
318 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
319 |
if (image_create |
if (image_create |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
|
if (image_create |
|
320 |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
321 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
322 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
|
if (image_create |
|
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
323 |
|
|
324 |
/* Create full bitplane for GMC, this might be wasteful */ |
/* Create full bitplane for GMC, this might be wasteful */ |
325 |
if (image_create |
if (image_create |
411 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
412 |
|
|
413 |
init_timer(); |
init_timer(); |
414 |
|
init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); |
415 |
|
|
416 |
return 0; /* ok */ |
return 0; /* ok */ |
417 |
|
|
471 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
472 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
473 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
474 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
475 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
476 |
|
|
477 |
/* destroy GMC image */ |
/* destroy GMC image */ |
478 |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
479 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
480 |
|
|
481 |
|
xvid_err_memory2a: |
482 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
483 |
|
|
484 |
xvid_err_memory2: |
xvid_err_memory2: |
485 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
562 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
563 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
564 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
565 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
566 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
|
|
|
567 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
568 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
569 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
608 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
609 |
} |
} |
610 |
|
|
611 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
612 |
|
|
613 |
if (pEnc->num_plugins>0) |
if (pEnc->num_plugins>0) |
614 |
xvid_free(pEnc->zones); |
xvid_free(pEnc->zones); |
615 |
|
|
674 |
data.type = *type; |
data.type = *type; |
675 |
data.quant = *quant; |
data.quant = *quant; |
676 |
|
|
677 |
|
data.vol_flags = frame->vol_flags; |
678 |
|
data.vop_flags = frame->vop_flags; |
679 |
|
data.motion_flags = frame->motion_flags; |
680 |
|
|
681 |
|
} else if (opt == XVID_PLG_FRAME) { |
682 |
|
data.type = coding2type(frame->coding_type); |
683 |
|
data.quant = frame->quant; |
684 |
|
|
685 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
686 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
687 |
data.dquant_stride = pEnc->mbParam.mb_width; |
data.dquant_stride = pEnc->mbParam.mb_width; |
688 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
689 |
} |
} |
690 |
|
|
|
data.vol_flags = frame->vol_flags; |
|
|
data.vop_flags = frame->vop_flags; |
|
|
data.motion_flags = frame->motion_flags; |
|
|
|
|
691 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
692 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
693 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_USER; |
772 |
*type = data.type; |
*type = data.type; |
773 |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
774 |
|
|
775 |
|
frame->vol_flags = data.vol_flags; |
776 |
|
frame->vop_flags = data.vop_flags; |
777 |
|
frame->motion_flags = data.motion_flags; |
778 |
|
|
779 |
|
} else if (opt == XVID_PLG_FRAME) { |
780 |
|
|
781 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
782 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
783 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
789 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
790 |
} |
} |
791 |
} |
} |
792 |
|
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
|
frame->vol_flags = data.vol_flags; |
|
|
frame->vop_flags = data.vop_flags; |
|
|
frame->motion_flags = data.motion_flags; |
|
|
} |
|
793 |
} |
} |
794 |
|
|
795 |
|
|
796 |
|
} |
797 |
|
|
798 |
|
|
799 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
811 |
pEnc->m_framenum--; /* debug ticker */ |
pEnc->m_framenum--; /* debug ticker */ |
812 |
} |
} |
813 |
|
|
814 |
|
static __inline void |
815 |
|
MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) |
816 |
|
{ |
817 |
|
if (pMB->cbp == 0) { |
818 |
|
/* we want to code dquant but the quantizer value will not be used yet |
819 |
|
let's find out if we can postpone dquant to next MB |
820 |
|
*/ |
821 |
|
if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { |
822 |
|
pMB->dquant = 0; /* it's the last MB of all, the easiest case */ |
823 |
|
return; |
824 |
|
} else { |
825 |
|
MACROBLOCK * next = pMB + 1; |
826 |
|
const MACROBLOCK * prev = pMB - 1; |
827 |
|
if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) |
828 |
|
/* mode allows dquant change in the future */ |
829 |
|
if (abs(next->quant - prev->quant) <= 2) { |
830 |
|
/* quant change is not out of range */ |
831 |
|
pMB->quant = prev->quant; |
832 |
|
pMB->dquant = 0; |
833 |
|
next->dquant = next->quant - prev->quant; |
834 |
|
return; |
835 |
|
} |
836 |
|
} |
837 |
|
} |
838 |
|
/* couldn't skip this dquant */ |
839 |
|
pMB->mode = MODE_INTER_Q; |
840 |
|
} |
841 |
|
|
842 |
|
|
843 |
|
|
844 |
static __inline void |
static __inline void |
982 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
983 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
984 |
|
|
985 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
986 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
987 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
988 |
|
|
1223 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1224 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1225 |
switch(frame->par) { |
switch(frame->par) { |
1226 |
|
case 0: |
1227 |
|
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1228 |
|
break; |
1229 |
case XVID_PAR_11_VGA: |
case XVID_PAR_11_VGA: |
1230 |
case XVID_PAR_43_PAL: |
case XVID_PAR_43_PAL: |
1231 |
case XVID_PAR_43_NTSC: |
case XVID_PAR_43_NTSC: |
1243 |
|
|
1244 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1245 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1246 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); |
1247 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1248 |
set_inter_matrix(frame->quant_inter_matrix); |
set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); |
1249 |
} |
} |
1250 |
|
|
1251 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1323 |
|
|
1324 |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1325 |
{ |
{ |
1326 |
unsigned int i,j; |
unsigned int i; |
1327 |
int quant = frame->quant; |
MACROBLOCK * pMB = frame->mbs; |
1328 |
|
int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ |
1329 |
if (quant > 31) |
if (quant > 31) |
1330 |
frame->quant = quant = 31; |
frame->quant = quant = 31; |
1331 |
else if (quant < 1) |
else if (quant < 1) |
1332 |
frame->quant = quant = 1; |
frame->quant = quant = 1; |
1333 |
|
|
1334 |
for (j=0; j<pParam->mb_height; j++) |
for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) { |
|
for (i=0; i<pParam->mb_width; i++) { |
|
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
|
1335 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1336 |
if (quant > 31) |
if (quant > 31) |
1337 |
quant = 31; |
quant = 31; |
1338 |
else if (quant < 1) |
else if (quant < 1) |
1339 |
quant = 1; |
quant = 1; |
1340 |
pMB->quant = quant; |
pMB->quant = quant; |
1341 |
|
pMB++; |
1342 |
} |
} |
1343 |
} |
} |
1344 |
|
|
1394 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1395 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1396 |
|
|
1397 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1398 |
|
|
1399 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1400 |
|
|
1401 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1404 |
|
|
1405 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1406 |
|
|
1407 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1408 |
|
|
1409 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1410 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1526 |
|
|
1527 |
current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1528 |
|
|
1529 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1530 |
|
|
1531 |
SetMacroblockQuants(&pEnc->mbParam, current); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1532 |
|
|
1597 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1598 |
} |
} |
1599 |
|
|
1600 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1601 |
|
|
1602 |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1603 |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1645 |
|
|
1646 |
stop_comp_timer(); |
stop_comp_timer(); |
1647 |
|
|
|
if (pMB->dquant != 0) { |
|
|
pMB->mode = MODE_INTER_Q; |
|
|
} |
|
|
|
|
1648 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1649 |
|
|
1650 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1653 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1654 |
} |
} |
1655 |
|
|
1656 |
|
if (pMB->dquant != 0) |
1657 |
|
MBSetDquant(pMB, x, y, &pEnc->mbParam); |
1658 |
|
|
1659 |
|
|
1660 |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1661 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1662 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1818 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1819 |
|
|
1820 |
set_timecodes(current,reference,pParam->fbase); |
set_timecodes(current,reference,pParam->fbase); |
1821 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); |
1822 |
|
|
1823 |
/* copy reference frame details into the current frame */ |
/* copy reference frame details into the current frame */ |
1824 |
current->quant = reference->quant; |
current->quant = reference->quant; |
1925 |
pEnc->current->is_interpolated = 0; |
pEnc->current->is_interpolated = 0; |
1926 |
} |
} |
1927 |
|
|
1928 |
|
frame->coding_type = B_VOP; |
1929 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1930 |
|
|
1931 |
start_timer(); |
start_timer(); |
1932 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1933 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
1938 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1939 |
stop_motion_timer(); |
stop_motion_timer(); |
1940 |
|
|
|
frame->coding_type = B_VOP; |
|
|
|
|
1941 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1942 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
1943 |
|
|
1944 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1945 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |