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.4 2003-03-13 11:07:20 suxen_drol Exp $ |
* $Id: encoder.c,v 1.95.2.5 2003-03-15 14:32:56 suxen_drol Exp $ |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
47 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
48 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
49 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
|
#include "utils/ratecontrol.h" |
|
50 |
#include "utils/emms.h" |
#include "utils/emms.h" |
51 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
52 |
#include "quant/adapt_quant.h" |
#include "quant/adapt_quant.h" |
181 |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
182 |
pinfo.version = XVID_VERSION; |
pinfo.version = XVID_VERSION; |
183 |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
184 |
pEnc->plugin_flags |= pinfo.flags; |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
185 |
} |
} |
186 |
|
|
187 |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
196 |
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
197 |
pEnc->plugins[n].func = create->plugins[n].func; |
pEnc->plugins[n].func = create->plugins[n].func; |
198 |
} |
} |
|
|
|
199 |
} |
} |
200 |
|
|
201 |
/* temp dquants */ |
/* temp dquants */ |
217 |
|
|
218 |
/* Bitrate allocator defaults |
/* Bitrate allocator defaults |
219 |
|
|
|
if (create->rc_bitrate <= 0) |
|
|
create->rc_bitrate = 900000; |
|
|
|
|
|
if (create->rc_reaction_delay_factor <= 0) |
|
|
create->rc_reaction_delay_factor = 16; |
|
|
|
|
|
if (create->rc_averaging_period <= 0) |
|
|
create->rc_averaging_period = 100; |
|
|
|
|
|
if (create->rc_buffer <= 0) |
|
|
create->rc_buffer = 100; |
|
|
|
|
|
|
|
|
|
|
220 |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
221 |
create->min_quantizer = 1; |
create->min_quantizer = 1; |
222 |
|
|
224 |
create->max_quantizer = 31; |
create->max_quantizer = 31; |
225 |
|
|
226 |
if (create->max_quantizer < create->min_quantizer) |
if (create->max_quantizer < create->min_quantizer) |
227 |
create->max_quantizer = create->min_quantizer; |
create->max_quantizer = create->min_quantizer; */ |
|
|
|
|
pEnc->bitrate = create->rc_bitrate; */ |
|
228 |
|
|
229 |
|
|
230 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
249 |
|
|
250 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
251 |
|
|
252 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
253 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
254 |
|
image_null(&pEnc->sOriginal2); |
255 |
|
} |
256 |
|
|
257 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
258 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
266 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
267 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
268 |
|
|
269 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
270 |
{ if (image_create |
if (image_create |
271 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
272 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
273 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
274 |
|
|
275 |
|
if (image_create |
276 |
|
(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
277 |
|
pEnc->mbParam.edged_height) < 0) |
278 |
|
goto xvid_err_memory3; |
279 |
} |
} |
280 |
|
|
281 |
if (image_create |
if (image_create |
410 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
411 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
412 |
|
|
|
/* XXX: cleanup ratecontol when new code is ready */ |
|
|
/* if (create->rc_bitrate) { |
|
|
RateControlInit(&pEnc->rate_control, create->rc_bitrate, |
|
|
create->rc_reaction_delay_factor, |
|
|
create->rc_averaging_period, create->rc_buffer, |
|
|
create->fbase * 1000 / create->fincr, |
|
|
create->max_quantizer, create->min_quantizer); |
|
|
} */ |
|
|
|
|
413 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
414 |
|
|
415 |
init_timer(); |
init_timer(); |
456 |
|
|
457 |
xvid_err_memory3: |
xvid_err_memory3: |
458 |
|
|
459 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
460 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
461 |
|
pEnc->mbParam.edged_height); |
462 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
463 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
464 |
} |
} |
465 |
|
|
586 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
587 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
588 |
|
|
589 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
590 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
591 |
|
pEnc->mbParam.edged_height); |
592 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
593 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
594 |
} |
} |
595 |
|
|
620 |
call the plugins |
call the plugins |
621 |
*/ |
*/ |
622 |
|
|
623 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant) |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, int opt, int * type, int * quant) |
624 |
{ |
{ |
625 |
int i; |
int i; |
626 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
628 |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
629 |
data.version = XVID_VERSION; |
data.version = XVID_VERSION; |
630 |
|
|
631 |
|
data.width = pEnc->mbParam.width; |
632 |
|
data.height = pEnc->mbParam.height; |
633 |
|
data.fincr = pEnc->mbParam.fincr; |
634 |
|
data.fbase = pEnc->mbParam.fbase; |
635 |
|
|
636 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_USER; |
637 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
638 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
649 |
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
650 |
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
651 |
|
|
652 |
data.original.csp = XVID_CSP_NULL; |
data.frame_num = frame->frame_num; |
|
/* todo: data.original */ |
|
653 |
|
|
654 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
655 |
data.type = XVID_TYPE_AUTO; |
data.type = XVID_TYPE_AUTO; |
660 |
/* todo: vol,vop,motion flags */ |
/* todo: vol,vop,motion flags */ |
661 |
|
|
662 |
} else { // XVID_PLG_AFTER |
} else { // XVID_PLG_AFTER |
663 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
664 |
|
data.original.csp = XVID_CSP_USER; |
665 |
|
data.original.plane[0] = original->y; |
666 |
|
data.original.plane[1] = original->u; |
667 |
|
data.original.plane[2] = original->v; |
668 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
669 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
670 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
671 |
|
} |
672 |
|
|
673 |
data.type = coding2type(frame->coding_type); |
data.type = coding2type(frame->coding_type); |
674 |
data.quant = frame->quant; |
data.quant = frame->quant; |
704 |
|
|
705 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
706 |
{ |
{ |
707 |
|
pEnc->current->frame_num = pEnc->m_framenum; |
708 |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
pEnc->current->stamp = pEnc->mbParam.m_stamp; /* first frame is zero */ |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
|
709 |
|
|
710 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
711 |
pEnc->m_framenum++; /* debug ticker */ |
pEnc->m_framenum++; /* debug ticker */ |
712 |
} |
} |
713 |
|
|
714 |
|
static __inline void dec_frame_num(Encoder * pEnc) |
715 |
|
{ |
716 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
717 |
|
pEnc->m_framenum--; /* debug ticker */ |
718 |
|
} |
719 |
|
|
720 |
|
|
721 |
|
|
722 |
static __inline void |
static __inline void |
740 |
|
|
741 |
|
|
742 |
static void |
static void |
743 |
set_stats(xvid_enc_stats_t * stats, RateControl * rc, |
set_stats(xvid_enc_stats_t * stats, const MBParam * pParam, const FRAMEINFO * frame) |
|
const MBParam * pParam, const FRAMEINFO * frame) |
|
744 |
{ |
{ |
745 |
if (stats) |
if (stats) |
746 |
{ |
{ |
754 |
stats->mblks = frame->sStat.mblks; |
stats->mblks = frame->sStat.mblks; |
755 |
stats->ublks = frame->sStat.ublks; |
stats->ublks = frame->sStat.ublks; |
756 |
} |
} |
|
|
|
|
RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP ); |
|
757 |
} |
} |
758 |
|
|
759 |
|
|
771 |
****************************************************************************/ |
****************************************************************************/ |
772 |
|
|
773 |
|
|
|
/* XXX: dx50bvop broken! something todo with queue */ |
|
|
|
|
774 |
int |
int |
775 |
enc_encode(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
776 |
xvid_enc_frame_t * xFrame, |
xvid_enc_frame_t * xFrame, |
778 |
{ |
{ |
779 |
xvid_enc_frame_t * frame; |
xvid_enc_frame_t * frame; |
780 |
int type; |
int type; |
|
uint16_t x, y; |
|
781 |
Bitstream bs; |
Bitstream bs; |
782 |
|
|
783 |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
840 |
|
|
841 |
if (pEnc->flush_bframes) |
if (pEnc->flush_bframes) |
842 |
{ |
{ |
843 |
if (pEnc->bframenum_head < pEnc->bframenum_tail) |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
|
{ |
|
844 |
|
|
845 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
846 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
847 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
848 |
|
|
849 |
if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
850 |
image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image, |
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
851 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
852 |
} |
} |
853 |
|
|
854 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
855 |
|
|
|
if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) { |
|
|
stats->sse_y = |
|
|
plane_sse( pEnc->sOriginal.y, pEnc->bframes[pEnc->bframenum_head]->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
stats->sse_u = |
|
|
plane_sse( pEnc->sOriginal.u, pEnc->bframes[pEnc->bframenum_head]->image.u, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
|
|
|
stats->sse_v = |
|
|
plane_sse( pEnc->sOriginal.v, pEnc->bframes[pEnc->bframenum_head]->image.v, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
} |
|
|
|
|
|
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
|
856 |
emms(); |
emms(); |
857 |
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
set_stats(stats, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
858 |
|
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0); |
859 |
emms(); |
emms(); |
860 |
|
|
861 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
862 |
|
|
|
|
|
863 |
goto done; |
goto done; |
864 |
} |
} |
865 |
|
|
|
/* flush complete: reset counters */ |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
|
|
|
866 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
867 |
|
|
868 |
for divx5 decoder compatibility, this marker must consist |
for divx5 decoder compatibility, this marker must consist |
870 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
871 |
*/ |
*/ |
872 |
|
|
873 |
if ((pEnc->mbParam.global_flags & XVID_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { |
874 |
int tmp; |
int tmp; |
875 |
int bits; |
int bits; |
876 |
|
|
889 |
|
|
890 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
891 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
|
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
|
892 |
emms(); |
emms(); |
893 |
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
set_stats(stats, &pEnc->mbParam, pEnc->current); |
894 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
895 |
emms(); |
emms(); |
896 |
|
|
897 |
// xFrame->out_flags |= XVID_PACKED_FIXUP?; |
/* flush complete: reset counters */ |
898 |
|
pEnc->flush_bframes = 0; |
899 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
900 |
goto done; |
goto done; |
901 |
|
|
902 |
} |
} |
|
} |
|
903 |
|
|
904 |
|
/* flush complete: reset counters */ |
905 |
|
pEnc->flush_bframes = 0; |
906 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
907 |
|
} |
908 |
|
|
909 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
910 |
* dequeue frame from the encoding queue |
* dequeue frame from the encoding queue |
914 |
{ |
{ |
915 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
916 |
{ |
{ |
917 |
if (pEnc->bframenum_tail > 0) /* are bframes */ |
|
918 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { |
919 |
|
emms(); |
920 |
|
set_stats(stats, &pEnc->mbParam, pEnc->current); |
921 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
922 |
|
emms(); |
923 |
|
} |
924 |
|
|
925 |
|
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
926 |
|
if (pEnc->bframenum_tail > 0) |
927 |
{ |
{ |
928 |
|
|
929 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
930 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
931 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
932 |
|
|
933 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
934 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
935 |
|
|
936 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
937 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
938 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
939 |
|
} |
940 |
|
|
941 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
942 |
|
|
943 |
goto done_flush; |
goto done_flush; |
944 |
} |
} |
945 |
|
|
960 |
|
|
961 |
|
|
962 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
963 |
* init pEnc->current field |
* init pEnc->current fields |
964 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
965 |
|
|
|
emms(); /* float-point region */ |
|
|
|
|
966 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
967 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
968 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
974 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
975 |
} |
} |
976 |
|
|
977 |
if (xFrame->vop_flags & XVID_EXTRASTATS) { |
emms(); /* float-point region */ |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
} |
|
978 |
|
|
979 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
980 |
|
unsigned int x, y; |
981 |
|
|
982 |
pEnc->current->quant = |
pEnc->current->quant = |
983 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
1002 |
|
|
1003 |
} |
} |
1004 |
|
|
1005 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1006 |
|
* frame type & quant selection |
1007 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1008 |
|
|
1009 |
emms(); /* END floating-point region */ |
emms(); /* END floating-point region */ |
1010 |
call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant); |
1011 |
emms(); |
emms(); |
1012 |
|
|
1013 |
if (frame->type > 0) |
if (frame->type > 0) |
1016 |
if (frame->quant > 0) |
if (frame->quant > 0) |
1017 |
pEnc->current->quant = frame->quant; |
pEnc->current->quant = frame->quant; |
1018 |
|
|
1019 |
if (type > 0) /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
|
{ |
|
1020 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1021 |
} |
} else{ /* XVID_TYPE_AUTO */ |
1022 |
else /* XVID_TYPE_AUTO */ |
if (pEnc->iFrameNum == 0 || (pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)){ |
|
{ |
|
|
if (pEnc->iFrameNum == 0 || |
|
|
(pEnc->mbParam.iMaxKeyInterval > 0 && pEnc->iFrameNum >= pEnc->mbParam.iMaxKeyInterval)) |
|
|
{ |
|
1023 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1024 |
type = I_VOP; |
type = I_VOP; |
1025 |
}else{ |
}else{ |
1026 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1027 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1028 |
pEnc->iFrameNum, pEnc->bframenum_tail); |
pEnc->iFrameNum, pEnc->bframenum_tail); |
1029 |
|
|
1030 |
|
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { |
1031 |
|
type = P_VOP; /* disable dynamic bframes */ |
1032 |
|
} |
1033 |
|
} |
1034 |
} |
} |
1035 |
|
|
1036 |
|
/* bframes buffer overflow check */ |
1037 |
|
if (type != I_VOP) { |
1038 |
|
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1039 |
|
type = P_VOP; |
1040 |
|
}else{ |
1041 |
|
type = B_VOP; |
1042 |
} |
} |
1043 |
|
} |
1044 |
|
|
1045 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1046 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1047 |
|
|
|
|
|
1048 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1049 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
1050 |
"%i st:%i if:%i", pEnc->m_framenum, pEnc->current->stamp, pEnc->iFrameNum); |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
1051 |
} |
} |
1052 |
|
|
1053 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1054 |
* ivop/pvop/bvop selection |
* encode this frame as a b-vop |
1055 |
|
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1056 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1057 |
|
if (type == B_VOP) { |
1058 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1059 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1060 |
|
} |
1061 |
|
|
1062 |
if (type == I_VOP) { |
if (frame->bquant < 1) { |
1063 |
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1064 |
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1065 |
|
|
1066 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
} else { |
1067 |
|
pEnc->current->quant = frame->bquant; |
1068 |
|
} |
1069 |
|
|
1070 |
|
if (pEnc->current->quant < 1) |
1071 |
|
pEnc->current->quant = 1; |
1072 |
|
else if (pEnc->current->quant > 31) |
1073 |
|
pEnc->current->quant = 31; |
1074 |
|
|
1075 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i", |
1076 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1077 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1078 |
|
|
1079 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
/* store frame into bframe buffer & swap ref back to current */ |
1080 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1081 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1082 |
|
|
1083 |
|
pEnc->bframenum_tail++; |
1084 |
|
|
1085 |
|
goto repeat; |
1086 |
} |
} |
1087 |
|
|
1088 |
// when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame |
/* for unpacked bframes, output the stats for the last encoded frame */ |
1089 |
if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1090 |
|
{ |
1091 |
|
if (pEnc->current->stamp > 0) { |
1092 |
|
emms(); |
1093 |
|
set_stats(stats, &pEnc->mbParam, pEnc->reference); |
1094 |
|
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
1095 |
|
emms(); |
1096 |
|
} |
1097 |
|
else |
1098 |
|
stats->type = XVID_TYPE_NOTHING; |
1099 |
|
} |
1100 |
|
|
1101 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1102 |
|
* closed-gop |
1103 |
|
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1104 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1105 |
|
|
1106 |
|
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1107 |
|
|
1108 |
// place this frame back on the encoding-queue (head) |
// place this frame back on the encoding-queue (head) |
1109 |
// we will deal with it next time |
// we will deal with it next time |
1110 |
pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); |
pEnc->queue_head = (pEnc->queue_head + (pEnc->mbParam.max_bframes+1) - 1) % (pEnc->mbParam.max_bframes+1); |
1111 |
|
pEnc->queue_size++; |
1112 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1113 |
|
|
1114 |
|
dec_frame_num(pEnc); |
1115 |
|
pEnc->iFrameNum--; |
1116 |
|
|
1117 |
// grab the last frame from the bframe-queue |
// grab the last frame from the bframe-queue |
1118 |
|
|
1119 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1123 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
1124 |
} |
} |
1125 |
|
|
1126 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1127 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
1128 |
|
type = P_VOP; |
1129 |
|
} |
1130 |
|
|
|
FrameCodeP(pEnc, &bs, 1, 0); |
|
1131 |
|
|
1132 |
} else { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1133 |
|
* encode this frame as an i-vop |
1134 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1135 |
|
|
1136 |
|
if (type == I_VOP) { |
1137 |
|
|
1138 |
|
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1139 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1140 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1141 |
|
|
1142 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1143 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1144 |
|
} |
1145 |
|
|
1146 |
|
|
1147 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1148 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1164 |
|
|
1165 |
/* ^^^------------------------ */ |
/* ^^^------------------------ */ |
1166 |
|
|
1167 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1168 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1169 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1170 |
|
} |
1171 |
|
|
1172 |
|
|
1173 |
FrameCodeI(pEnc, &bs); |
FrameCodeI(pEnc, &bs); |
1174 |
xFrame->out_flags |= XVID_KEYFRAME; |
xFrame->out_flags |= XVID_KEYFRAME; |
|
} |
|
1175 |
|
|
1176 |
} else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) || |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1177 |
pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
* encode this frame as an p-vop |
1178 |
/* |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1179 |
* This will be coded as a Predicted Frame |
|
1180 |
*/ |
} else { // (type == P_VOP || type == S_VOP) |
1181 |
|
|
1182 |
DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1183 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1184 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1185 |
|
|
1187 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1188 |
} |
} |
1189 |
|
|
1190 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1191 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1192 |
} else { /* type == B_VOP */ |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
|
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
|
|
} |
|
|
|
|
|
if (frame->bquant < 1) { |
|
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
|
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
|
|
|
|
|
} else { |
|
|
pEnc->current->quant = frame->bquant; |
|
1193 |
} |
} |
1194 |
|
|
1195 |
if (pEnc->current->quant < 1) |
FrameCodeP(pEnc, &bs, 1, 0); |
|
pEnc->current->quant = 1; |
|
|
else if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
|
|
|
|
|
/* store frame into bframe buffer & swap ref back to current */ |
|
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->bframenum_tail++; |
|
|
|
|
|
goto repeat; |
|
|
|
|
1196 |
} |
} |
1197 |
|
|
|
if (xFrame->vop_flags & XVID_EXTRASTATS) { |
|
|
stats->sse_y = |
|
|
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
stats->sse_u = |
|
|
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
|
|
|
stats->sse_v = |
|
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
} |
|
1198 |
|
|
1199 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1200 |
* okay, on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1201 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1202 |
|
|
1203 |
done_flush: |
done_flush: |
1204 |
|
|
|
#if 0 |
|
|
{ |
|
|
char tmp[100]; |
|
|
wsprintf(tmp,"\\frame%03i.pgm", pEnc->m_framenum); |
|
|
image_dump_yuvpgm(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height, tmp); |
|
|
} |
|
|
#endif |
|
|
|
|
|
/* packed_mode: repeat */ |
|
1205 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1206 |
|
|
1207 |
|
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1208 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
1209 |
goto repeat; |
goto repeat; |
1210 |
} |
} |
1211 |
|
|
1212 |
|
/* packed or no-bframes: output stats */ |
1213 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
1214 |
{ |
{ |
|
/* packed(and low_delay) encoding gives us graceful reorded-stats output */ |
|
|
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
|
|
emms(); |
|
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
|
|
emms(); |
|
|
} |
|
|
else |
|
|
{ |
|
|
/* outherwise, output the previous frame */ |
|
|
/* XXX: for this to work, refernce must be valid for IVOPs too */ |
|
|
if (pEnc->current->stamp > 0) { |
|
|
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->reference); |
|
1215 |
emms(); |
emms(); |
1216 |
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
set_stats(stats, &pEnc->mbParam, pEnc->current); |
1217 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0); |
1218 |
emms(); |
emms(); |
1219 |
} |
} |
|
else |
|
|
stats->type = XVID_TYPE_NOTHING; |
|
|
} |
|
1220 |
|
|
1221 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1222 |
* done; return number of bytes consumed |
* done; return number of bytes consumed |
1289 |
stop_edges_timer(); |
stop_edges_timer(); |
1290 |
} |
} |
1291 |
|
|
|
//pEnc->iFrameNum = 0; |
|
1292 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1293 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
|
//pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
|
1294 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1295 |
|
|
1296 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |