26 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
* $Id: encoder.c,v 1.95.2.3 2003-03-10 00:38:49 edgomez Exp $ |
* $Id: encoder.c,v 1.95.2.4 2003-03-13 11:07:20 suxen_drol Exp $ |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
169 |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
170 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
171 |
|
|
172 |
|
/* plugin */ |
173 |
|
pEnc->num_plugins = create->num_plugins; |
174 |
|
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
175 |
|
if (pEnc->plugins == NULL) |
176 |
|
goto xvid_err_memory0; |
177 |
|
|
178 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
179 |
|
xvid_plg_create_t pcreate; |
180 |
|
xvid_plg_info_t pinfo; |
181 |
|
|
182 |
|
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
183 |
|
pinfo.version = XVID_VERSION; |
184 |
|
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
185 |
|
pEnc->plugin_flags |= pinfo.flags; |
186 |
|
} |
187 |
|
|
188 |
|
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
189 |
|
pcreate.version = XVID_VERSION; |
190 |
|
pcreate.width = pEnc->mbParam.width; |
191 |
|
pcreate.height = pEnc->mbParam.height; |
192 |
|
pcreate.fincr = pEnc->mbParam.fincr; |
193 |
|
pcreate.fbase = pEnc->mbParam.fbase; |
194 |
|
pcreate.param = create->plugins[n].param; |
195 |
|
|
196 |
|
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
197 |
|
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
198 |
|
pEnc->plugins[n].func = create->plugins[n].func; |
199 |
|
} |
200 |
|
|
201 |
|
} |
202 |
|
|
203 |
|
/* temp dquants */ |
204 |
|
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
205 |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
206 |
|
/* XXX: error checking */ |
207 |
|
|
208 |
/* bframes */ |
/* bframes */ |
209 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
210 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
515 |
xvid_err_memory1: |
xvid_err_memory1: |
516 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
517 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
518 |
|
|
519 |
|
xvid_free(pEnc->temp_dquants); |
520 |
|
|
521 |
|
xvid_err_memory0: |
522 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
523 |
|
if (pEnc->plugins[n].func) { |
524 |
|
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0); |
525 |
|
} |
526 |
|
} |
527 |
|
xvid_free(pEnc->plugins); |
528 |
|
|
529 |
xvid_free(pEnc); |
xvid_free(pEnc); |
530 |
|
|
531 |
create->handle = NULL; |
create->handle = NULL; |
617 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
618 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
619 |
|
|
620 |
|
xvid_free(pEnc->temp_dquants); |
621 |
|
|
622 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
623 |
|
if (pEnc->plugins[i].func) { |
624 |
|
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
625 |
|
} |
626 |
|
} |
627 |
|
xvid_free(pEnc->plugins); |
628 |
|
|
629 |
xvid_free(pEnc); |
xvid_free(pEnc); |
630 |
|
|
631 |
return 0; /* ok */ |
return 0; /* ok */ |
632 |
} |
} |
633 |
|
|
634 |
|
|
635 |
|
/* |
636 |
|
call the plugins |
637 |
|
*/ |
638 |
|
|
639 |
|
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant) |
640 |
|
{ |
641 |
|
int i; |
642 |
|
xvid_plg_data_t data; |
643 |
|
|
644 |
|
memset(&data, 0, sizeof(xvid_plg_data_t)); |
645 |
|
data.version = XVID_VERSION; |
646 |
|
|
647 |
|
data.reference.csp = XVID_CSP_USER; |
648 |
|
data.reference.plane[0] = pEnc->reference->image.y; |
649 |
|
data.reference.plane[1] = pEnc->reference->image.u; |
650 |
|
data.reference.plane[2] = pEnc->reference->image.v; |
651 |
|
data.reference.stride[0] = pEnc->mbParam.edged_width; |
652 |
|
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
653 |
|
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
654 |
|
|
655 |
|
data.current.csp = XVID_CSP_USER; |
656 |
|
data.current.plane[0] = frame->image.y; |
657 |
|
data.current.plane[1] = frame->image.u; |
658 |
|
data.current.plane[2] = frame->image.v; |
659 |
|
data.current.stride[0] = pEnc->mbParam.edged_width; |
660 |
|
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
661 |
|
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
662 |
|
|
663 |
|
data.original.csp = XVID_CSP_NULL; |
664 |
|
/* todo: data.original */ |
665 |
|
|
666 |
|
if (opt == XVID_PLG_BEFORE) { |
667 |
|
data.type = XVID_TYPE_AUTO; |
668 |
|
data.quant = 2; |
669 |
|
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
670 |
|
//data.qscale_stride = pEnc->mbParam.width; |
671 |
|
//data.qscale_table = pEnc->temp_dquants; |
672 |
|
/* todo: vol,vop,motion flags */ |
673 |
|
|
674 |
|
} else { // XVID_PLG_AFTER |
675 |
|
|
676 |
|
data.type = coding2type(frame->coding_type); |
677 |
|
data.quant = frame->quant; |
678 |
|
/* todo: data.qscale */ |
679 |
|
data.vol_flags = frame->vol_flags; |
680 |
|
data.vop_flags = frame->vop_flags; |
681 |
|
data.motion_flags = frame->motion_flags; |
682 |
|
|
683 |
|
data.length = frame->length; |
684 |
|
data.kblks = frame->sStat.kblks; |
685 |
|
data.mblks = frame->sStat.mblks; |
686 |
|
data.ublks = frame->sStat.ublks; |
687 |
|
} |
688 |
|
|
689 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
690 |
|
if (pEnc->plugins[i].func) { |
691 |
|
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
692 |
|
continue; |
693 |
|
} |
694 |
|
} |
695 |
|
} |
696 |
|
|
697 |
|
if (opt == XVID_PLG_BEFORE) { |
698 |
|
*type = data.type; |
699 |
|
*quant = data.quant; |
700 |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
701 |
|
} |
702 |
|
|
703 |
|
} |
704 |
|
|
705 |
|
|
706 |
|
|
707 |
|
|
708 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
709 |
{ |
{ |
710 |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
874 |
} |
} |
875 |
|
|
876 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
877 |
|
emms(); |
878 |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
879 |
|
emms(); |
880 |
|
|
881 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
882 |
|
|
916 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
917 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
918 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
919 |
|
emms(); |
920 |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
921 |
|
emms(); |
922 |
|
|
923 |
// xFrame->out_flags |= XVID_PACKED_FIXUP?; |
// xFrame->out_flags |= XVID_PACKED_FIXUP?; |
924 |
|
|
988 |
} |
} |
989 |
|
|
990 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
|
int *temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
|
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
|
991 |
|
|
992 |
pEnc->current->quant = |
pEnc->current->quant = |
993 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
994 |
pEnc->mbParam.edged_width, temp_dquants, |
pEnc->mbParam.edged_width, pEnc->temp_dquants, |
995 |
pEnc->current->quant, pEnc->current->quant, |
pEnc->current->quant, pEnc->current->quant, |
996 |
2 * pEnc->current->quant, |
2 * pEnc->current->quant, |
997 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
1004 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1005 |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1006 |
|
|
1007 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2]; |
1008 |
} |
} |
1009 |
|
|
1010 |
#undef OFFSET |
#undef OFFSET |
1011 |
} |
} |
1012 |
|
|
|
xvid_free(temp_dquants); |
|
|
} |
|
|
|
|
|
if (frame->quant > 0) |
|
|
{ |
|
|
pEnc->current->quant = frame->quant; |
|
|
type = frame->type; |
|
|
} |
|
|
else |
|
|
{ |
|
|
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
|
|
type = XVID_TYPE_AUTO; //RateControlGetType(&pEnc->rate_control, ...); |
|
1013 |
} |
} |
1014 |
|
|
1015 |
emms(); /* END floating-point region */ |
emms(); /* END floating-point region */ |
1016 |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant); |
1017 |
|
emms(); |
1018 |
|
|
1019 |
|
if (frame->type > 0) |
1020 |
|
type = frame->type; |
1021 |
|
|
1022 |
|
if (frame->quant > 0) |
1023 |
|
pEnc->current->quant = frame->quant; |
1024 |
|
|
1025 |
if (type > 0) /* XVID_TYPE_?VOP */ |
if (type > 0) /* XVID_TYPE_?VOP */ |
1026 |
{ |
{ |
1201 |
{ |
{ |
1202 |
/* packed(and low_delay) encoding gives us graceful reorded-stats output */ |
/* packed(and low_delay) encoding gives us graceful reorded-stats output */ |
1203 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
1204 |
|
emms(); |
1205 |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
1206 |
|
emms(); |
1207 |
} |
} |
1208 |
else |
else |
1209 |
{ |
{ |
1210 |
/* outherwise, output the previous frame */ |
/* outherwise, output the previous frame */ |
1211 |
/* XXX: for this to work, refernce must be valid for IVOPs too */ |
/* XXX: for this to work, refernce must be valid for IVOPs too */ |
1212 |
if (pEnc->current->stamp > 0) |
if (pEnc->current->stamp > 0) { |
1213 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference); |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference); |
1214 |
|
emms(); |
1215 |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
1216 |
|
emms(); |
1217 |
|
} |
1218 |
else |
else |
1219 |
stats->type = XVID_TYPE_NOTHING; |
stats->type = XVID_TYPE_NOTHING; |
1220 |
} |
} |