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)) { |
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 |
|
|
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); |
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 |
|
|
740 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
741 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
742 |
|
|
743 |
if (stats) { |
/* New code */ |
744 |
stats->type = coding2type(frame->coding_type); |
data.stats.type = coding2type(frame->coding_type); |
745 |
stats->quant = frame->quant; |
data.stats.quant = frame->quant; |
746 |
stats->vol_flags = frame->vol_flags; |
data.stats.vol_flags = frame->vol_flags; |
747 |
stats->vop_flags = frame->vop_flags; |
data.stats.vop_flags = frame->vop_flags; |
748 |
stats->length = frame->length; |
data.stats.length = frame->length; |
749 |
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); |
750 |
stats->kblks = frame->sStat.kblks; |
data.stats.kblks = frame->sStat.kblks; |
751 |
stats->mblks = frame->sStat.mblks; |
data.stats.mblks = frame->sStat.mblks; |
752 |
stats->ublks = frame->sStat.ublks; |
data.stats.ublks = frame->sStat.ublks; |
753 |
stats->sse_y = data.sse_y; |
data.stats.sse_y = data.sse_y; |
754 |
stats->sse_u = data.sse_u; |
data.stats.sse_u = data.sse_u; |
755 |
stats->sse_v = data.sse_v; |
data.stats.sse_v = data.sse_v; |
756 |
} |
|
757 |
|
if (stats) |
758 |
|
*stats = data.stats; |
759 |
} |
} |
760 |
|
|
761 |
/* call plugins */ |
/* call plugins */ |
791 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
792 |
} |
} |
793 |
} |
} |
794 |
frame->mbs[0].quant = data.quant; /* BEFORE2 will not affect the quant in stats */ |
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
795 |
} |
} |
796 |
|
|
797 |
|
|
798 |
} |
} |
799 |
|
|
800 |
|
|
|
|
|
|
|
|
801 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
802 |
{ |
{ |
803 |
pEnc->current->frame_num = pEnc->m_framenum; |
pEnc->current->frame_num = pEnc->m_framenum; |
813 |
pEnc->m_framenum--; /* debug ticker */ |
pEnc->m_framenum--; /* debug ticker */ |
814 |
} |
} |
815 |
|
|
816 |
|
static __inline void |
817 |
|
MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) |
818 |
|
{ |
819 |
|
if (pMB->cbp == 0) { |
820 |
|
/* we want to code dquant but the quantizer value will not be used yet |
821 |
|
let's find out if we can postpone dquant to next MB |
822 |
|
*/ |
823 |
|
if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { |
824 |
|
pMB->dquant = 0; /* it's the last MB of all, the easiest case */ |
825 |
|
return; |
826 |
|
} else { |
827 |
|
MACROBLOCK * next = pMB + 1; |
828 |
|
const MACROBLOCK * prev = pMB - 1; |
829 |
|
if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) |
830 |
|
/* mode allows dquant change in the future */ |
831 |
|
if (abs(next->quant - prev->quant) <= 2) { |
832 |
|
/* quant change is not out of range */ |
833 |
|
pMB->quant = prev->quant; |
834 |
|
pMB->dquant = 0; |
835 |
|
next->dquant = next->quant - prev->quant; |
836 |
|
return; |
837 |
|
} |
838 |
|
} |
839 |
|
} |
840 |
|
/* couldn't skip this dquant */ |
841 |
|
pMB->mode = MODE_INTER_Q; |
842 |
|
} |
843 |
|
|
844 |
|
|
845 |
|
|
846 |
static __inline void |
static __inline void |
850 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
851 |
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) ; |
852 |
|
|
853 |
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
#if 0 /* HEAVY DEBUG OUTPUT */ |
854 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
|
855 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
856 |
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", |
857 |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
858 |
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", |
859 |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
860 |
|
#endif |
861 |
|
} |
862 |
|
|
863 |
*/ |
static int |
864 |
|
gcd(int a, int b) |
865 |
|
{ |
866 |
|
int r ; |
867 |
|
|
868 |
|
if (b > a) { |
869 |
|
r = a; |
870 |
|
a = b; |
871 |
|
b = r; |
872 |
} |
} |
873 |
|
|
874 |
|
while ((r = a % b)) { |
875 |
|
a = b; |
876 |
|
b = r; |
877 |
|
} |
878 |
|
return b; |
879 |
|
} |
880 |
|
|
881 |
|
static void |
882 |
|
simplify_par(int *par_width, int *par_height) |
883 |
|
{ |
884 |
|
|
885 |
|
int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
886 |
|
int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; |
887 |
|
int divisor = gcd(_par_width, _par_height); |
888 |
|
|
889 |
|
_par_width /= divisor; |
890 |
|
_par_height /= divisor; |
891 |
|
|
892 |
|
/* 2^8 precision maximum */ |
893 |
|
if (_par_width>255 || _par_height>255) { |
894 |
|
float div; |
895 |
|
emms(); |
896 |
|
if (_par_width>_par_height) |
897 |
|
div = (float)_par_width/255; |
898 |
|
else |
899 |
|
div = (float)_par_height/255; |
900 |
|
|
901 |
|
_par_width = (int)((float)_par_width/div); |
902 |
|
_par_height = (int)((float)_par_height/div); |
903 |
|
} |
904 |
|
|
905 |
|
*par_width = _par_width; |
906 |
|
*par_height = _par_height; |
907 |
|
|
908 |
|
return; |
909 |
|
} |
910 |
|
|
911 |
|
|
912 |
/***************************************************************************** |
/***************************************************************************** |
1121 |
|
|
1122 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1123 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1124 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = frame->vol_flags; |
1125 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1126 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1127 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1156 |
} |
} |
1157 |
} |
} |
1158 |
|
|
1159 |
|
if (type != I_VOP) |
1160 |
|
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ |
1161 |
|
|
1162 |
/* bframes buffer overflow check */ |
/* bframes buffer overflow check */ |
1163 |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1164 |
type = P_VOP; |
type = P_VOP; |
1271 |
pEnc->iFrameNum = 1; |
pEnc->iFrameNum = 1; |
1272 |
|
|
1273 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1274 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->mbParam.vol_flags = pEnc->current->vol_flags; |
1275 |
|
|
1276 |
|
/* Aspect ratio */ |
1277 |
switch(frame->par) { |
switch(frame->par) { |
1278 |
case XVID_PAR_11_VGA: |
case XVID_PAR_11_VGA: |
1279 |
case XVID_PAR_43_PAL: |
case XVID_PAR_43_PAL: |
1284 |
pEnc->mbParam.par = frame->par; |
pEnc->mbParam.par = frame->par; |
1285 |
break; |
break; |
1286 |
default: |
default: |
1287 |
pEnc->mbParam.par = XVID_PAR_EXT; |
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1288 |
break; |
break; |
1289 |
} |
} |
1290 |
pEnc->mbParam.par_width = (frame->par_width)?frame->par_width:1; |
|
1291 |
pEnc->mbParam.par_height = (frame->par_height)?frame->par_height:1; |
/* For extended PAR only, we try to sanityse/simplify par values */ |
1292 |
|
if (pEnc->mbParam.par == XVID_PAR_EXT) { |
1293 |
|
pEnc->mbParam.par_width = frame->par_width; |
1294 |
|
pEnc->mbParam.par_height = frame->par_height; |
1295 |
|
simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); |
1296 |
|
} |
1297 |
|
|
1298 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1299 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1300 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); |
1301 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1302 |
set_inter_matrix(frame->quant_inter_matrix); |
set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); |
1303 |
} |
} |
1304 |
|
|
1305 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1699 |
|
|
1700 |
stop_comp_timer(); |
stop_comp_timer(); |
1701 |
|
|
|
if (pMB->dquant != 0) { |
|
|
pMB->mode = MODE_INTER_Q; |
|
|
} |
|
|
|
|
1702 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1703 |
|
|
1704 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1707 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1708 |
} |
} |
1709 |
|
|
1710 |
|
if (pMB->dquant != 0) |
1711 |
|
MBSetDquant(pMB, x, y, &pEnc->mbParam); |
1712 |
|
|
1713 |
|
|
1714 |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1715 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1716 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |