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.1 2003-02-22 08:49:44 suxen_drol Exp $ |
* $Id: encoder.c,v 1.95.2.6 2003-03-15 16:04:38 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" |
168 |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
169 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
170 |
|
|
171 |
|
/* plugin */ |
172 |
|
pEnc->num_plugins = create->num_plugins; |
173 |
|
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
174 |
|
if (pEnc->plugins == NULL) |
175 |
|
goto xvid_err_memory0; |
176 |
|
|
177 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
178 |
|
xvid_plg_create_t pcreate; |
179 |
|
xvid_plg_info_t pinfo; |
180 |
|
|
181 |
|
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
182 |
|
pinfo.version = XVID_VERSION; |
183 |
|
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
184 |
|
pEnc->mbParam.plugin_flags |= pinfo.flags; |
185 |
|
} |
186 |
|
|
187 |
|
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
188 |
|
pcreate.version = XVID_VERSION; |
189 |
|
pcreate.width = pEnc->mbParam.width; |
190 |
|
pcreate.height = pEnc->mbParam.height; |
191 |
|
pcreate.fincr = pEnc->mbParam.fincr; |
192 |
|
pcreate.fbase = pEnc->mbParam.fbase; |
193 |
|
pcreate.param = create->plugins[n].param; |
194 |
|
|
195 |
|
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
196 |
|
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
197 |
|
pEnc->plugins[n].func = create->plugins[n].func; |
198 |
|
} |
199 |
|
} |
200 |
|
|
201 |
|
if ((pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) || |
202 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
203 |
|
pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ |
204 |
|
} |
205 |
|
|
206 |
|
/* temp dquants */ |
207 |
|
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
208 |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
209 |
|
/* XXX: error checking */ |
210 |
|
|
211 |
/* bframes */ |
/* bframes */ |
212 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
213 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
222 |
|
|
223 |
/* Bitrate allocator defaults |
/* Bitrate allocator defaults |
224 |
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
225 |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
226 |
create->min_quantizer = 1; |
create->min_quantizer = 1; |
227 |
|
|
229 |
create->max_quantizer = 31; |
create->max_quantizer = 31; |
230 |
|
|
231 |
if (create->max_quantizer < create->min_quantizer) |
if (create->max_quantizer < create->min_quantizer) |
232 |
create->max_quantizer = create->min_quantizer; |
create->max_quantizer = create->min_quantizer; */ |
|
|
|
|
pEnc->bitrate = create->rc_bitrate; */ |
|
233 |
|
|
234 |
|
|
235 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
254 |
|
|
255 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
256 |
|
|
257 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
258 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
259 |
|
image_null(&pEnc->sOriginal2); |
260 |
|
} |
261 |
|
|
262 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
263 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
271 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
272 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
273 |
|
|
274 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
275 |
{ if (image_create |
if (image_create |
276 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
277 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
278 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
279 |
|
|
280 |
|
if (image_create |
281 |
|
(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
282 |
|
pEnc->mbParam.edged_height) < 0) |
283 |
|
goto xvid_err_memory3; |
284 |
} |
} |
285 |
|
|
286 |
if (image_create |
if (image_create |
415 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
416 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
417 |
|
|
|
/* 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); |
|
|
} */ |
|
|
|
|
418 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
419 |
|
|
420 |
init_timer(); |
init_timer(); |
461 |
|
|
462 |
xvid_err_memory3: |
xvid_err_memory3: |
463 |
|
|
464 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
465 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
466 |
|
pEnc->mbParam.edged_height); |
467 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
468 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
469 |
} |
} |
470 |
|
|
502 |
xvid_err_memory1: |
xvid_err_memory1: |
503 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
504 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
505 |
|
|
506 |
|
xvid_free(pEnc->temp_dquants); |
507 |
|
|
508 |
|
xvid_err_memory0: |
509 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
510 |
|
if (pEnc->plugins[n].func) { |
511 |
|
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0); |
512 |
|
} |
513 |
|
} |
514 |
|
xvid_free(pEnc->plugins); |
515 |
|
|
516 |
xvid_free(pEnc); |
xvid_free(pEnc); |
517 |
|
|
518 |
create->handle = NULL; |
create->handle = NULL; |
591 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
592 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
593 |
|
|
594 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
595 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
596 |
|
pEnc->mbParam.edged_height); |
597 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
598 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
599 |
} |
} |
600 |
|
|
606 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
607 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
608 |
|
|
609 |
|
xvid_free(pEnc->temp_dquants); |
610 |
|
|
611 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
612 |
|
if (pEnc->plugins[i].func) { |
613 |
|
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
614 |
|
} |
615 |
|
} |
616 |
|
xvid_free(pEnc->plugins); |
617 |
|
|
618 |
xvid_free(pEnc); |
xvid_free(pEnc); |
619 |
|
|
620 |
return 0; /* ok */ |
return 0; /* ok */ |
621 |
} |
} |
622 |
|
|
623 |
|
|
624 |
|
/* |
625 |
|
call the plugins |
626 |
|
*/ |
627 |
|
|
628 |
|
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
629 |
|
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
630 |
|
{ |
631 |
|
int i; |
632 |
|
xvid_plg_data_t data; |
633 |
|
|
634 |
|
memset(&data, 0, sizeof(xvid_plg_data_t)); |
635 |
|
data.version = XVID_VERSION; |
636 |
|
|
637 |
|
data.width = pEnc->mbParam.width; |
638 |
|
data.height = pEnc->mbParam.height; |
639 |
|
data.fincr = pEnc->mbParam.fincr; |
640 |
|
data.fbase = pEnc->mbParam.fbase; |
641 |
|
|
642 |
|
data.reference.csp = XVID_CSP_USER; |
643 |
|
data.reference.plane[0] = pEnc->reference->image.y; |
644 |
|
data.reference.plane[1] = pEnc->reference->image.u; |
645 |
|
data.reference.plane[2] = pEnc->reference->image.v; |
646 |
|
data.reference.stride[0] = pEnc->mbParam.edged_width; |
647 |
|
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
648 |
|
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
649 |
|
|
650 |
|
data.current.csp = XVID_CSP_USER; |
651 |
|
data.current.plane[0] = frame->image.y; |
652 |
|
data.current.plane[1] = frame->image.u; |
653 |
|
data.current.plane[2] = frame->image.v; |
654 |
|
data.current.stride[0] = pEnc->mbParam.edged_width; |
655 |
|
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
656 |
|
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
657 |
|
|
658 |
|
data.frame_num = frame->frame_num; |
659 |
|
|
660 |
|
if (opt == XVID_PLG_BEFORE) { |
661 |
|
data.type = XVID_TYPE_AUTO; |
662 |
|
data.quant = 2; |
663 |
|
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
664 |
|
//data.qscale_stride = pEnc->mbParam.width; |
665 |
|
//data.qscale_table = pEnc->temp_dquants; |
666 |
|
/* todo: vol,vop,motion flags */ |
667 |
|
|
668 |
|
} else { // XVID_PLG_AFTER |
669 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
670 |
|
data.original.csp = XVID_CSP_USER; |
671 |
|
data.original.plane[0] = original->y; |
672 |
|
data.original.plane[1] = original->u; |
673 |
|
data.original.plane[2] = original->v; |
674 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
675 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
676 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
677 |
|
} |
678 |
|
|
679 |
|
if ((frame->vol_flags & XVID_EXTRASTATS) || |
680 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
681 |
|
|
682 |
|
data.sse_y = |
683 |
|
plane_sse( original->y, frame->image.y, |
684 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
685 |
|
pEnc->mbParam.height); |
686 |
|
|
687 |
|
data.sse_u = |
688 |
|
plane_sse( original->u, frame->image.u, |
689 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
690 |
|
pEnc->mbParam.height/2); |
691 |
|
|
692 |
|
data.sse_v = |
693 |
|
plane_sse( original->v, frame->image.v, |
694 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
695 |
|
pEnc->mbParam.height/2); |
696 |
|
} |
697 |
|
|
698 |
|
data.type = coding2type(frame->coding_type); |
699 |
|
data.quant = frame->quant; |
700 |
|
/* todo: data.qscale */ |
701 |
|
data.vol_flags = frame->vol_flags; |
702 |
|
data.vop_flags = frame->vop_flags; |
703 |
|
data.motion_flags = frame->motion_flags; |
704 |
|
|
705 |
|
data.length = frame->length; |
706 |
|
data.kblks = frame->sStat.kblks; |
707 |
|
data.mblks = frame->sStat.mblks; |
708 |
|
data.ublks = frame->sStat.ublks; |
709 |
|
|
710 |
|
if (stats) |
711 |
|
{ |
712 |
|
stats->type = coding2type(frame->coding_type); |
713 |
|
stats->quant = frame->quant; |
714 |
|
stats->vol_flags = frame->vol_flags; |
715 |
|
stats->vop_flags = frame->vop_flags; |
716 |
|
stats->length = frame->length; |
717 |
|
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
718 |
|
stats->kblks = frame->sStat.kblks; |
719 |
|
stats->mblks = frame->sStat.mblks; |
720 |
|
stats->ublks = frame->sStat.ublks; |
721 |
|
stats->sse_y = data.sse_y; |
722 |
|
stats->sse_u = data.sse_u; |
723 |
|
stats->sse_v = data.sse_v; |
724 |
|
} |
725 |
|
} |
726 |
|
|
727 |
|
emms(); |
728 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
729 |
|
if (pEnc->plugins[i].func) { |
730 |
|
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
731 |
|
continue; |
732 |
|
} |
733 |
|
} |
734 |
|
} |
735 |
|
emms(); |
736 |
|
|
737 |
|
if (opt == XVID_PLG_BEFORE) { |
738 |
|
*type = data.type; |
739 |
|
*quant = data.quant; |
740 |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
741 |
|
} |
742 |
|
|
743 |
|
} |
744 |
|
|
745 |
|
|
746 |
|
|
747 |
|
|
748 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
749 |
{ |
{ |
750 |
|
pEnc->current->frame_num = pEnc->m_framenum; |
751 |
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; |
|
752 |
|
|
753 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
754 |
pEnc->m_framenum++; /* debug ticker */ |
pEnc->m_framenum++; /* debug ticker */ |
755 |
} |
} |
756 |
|
|
757 |
|
static __inline void dec_frame_num(Encoder * pEnc) |
758 |
|
{ |
759 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
760 |
|
pEnc->m_framenum--; /* debug ticker */ |
761 |
|
} |
762 |
|
|
763 |
|
|
764 |
|
|
765 |
static __inline void |
static __inline void |
782 |
} |
} |
783 |
|
|
784 |
|
|
|
static void |
|
|
set_stats(xvid_enc_stats_t * stats, RateControl * rc, |
|
|
const MBParam * pParam, const FRAMEINFO * frame) |
|
|
{ |
|
|
if (stats) |
|
|
{ |
|
|
stats->type = coding2type(frame->coding_type); |
|
|
stats->quant = frame->quant; |
|
|
stats->vol_flags = frame->vol_flags; |
|
|
stats->vop_flags = frame->vop_flags; |
|
|
stats->length = frame->length; |
|
|
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
|
|
stats->kblks = frame->sStat.kblks; |
|
|
stats->mblks = frame->sStat.mblks; |
|
|
stats->ublks = frame->sStat.ublks; |
|
|
} |
|
|
|
|
|
RateControlUpdate(rc, frame->quant, frame->length, frame->coding_type==I_VOP ); |
|
|
} |
|
|
|
|
|
|
|
785 |
|
|
786 |
/***************************************************************************** |
/***************************************************************************** |
787 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
789 |
* Returned values : |
* Returned values : |
790 |
* - >0 - output bytes |
* - >0 - output bytes |
791 |
* - 0 - no output |
* - 0 - no output |
792 |
|
* - XVID_ERR_VERSION - wrong version passed to core |
793 |
|
* - XVID_ERR_END - End of stream reached before end of coding |
794 |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
795 |
* format |
* format |
796 |
****************************************************************************/ |
****************************************************************************/ |
797 |
|
|
798 |
|
|
|
/* XXX: dx50bvop broken! something todo with queue */ |
|
|
|
|
799 |
int |
int |
800 |
enc_encode(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
801 |
xvid_enc_frame_t * xFrame, |
xvid_enc_frame_t * xFrame, |
803 |
{ |
{ |
804 |
xvid_enc_frame_t * frame; |
xvid_enc_frame_t * frame; |
805 |
int type; |
int type; |
|
uint16_t x, y; |
|
806 |
Bitstream bs; |
Bitstream bs; |
807 |
|
|
808 |
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 */ |
865 |
|
|
866 |
if (pEnc->flush_bframes) |
if (pEnc->flush_bframes) |
867 |
{ |
{ |
868 |
if (pEnc->bframenum_head < pEnc->bframenum_tail) |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
|
{ |
|
869 |
|
|
870 |
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", |
871 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
872 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
873 |
|
|
874 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
875 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
876 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
877 |
|
} |
878 |
|
|
879 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
880 |
|
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats); |
881 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
882 |
|
|
|
|
|
883 |
goto done; |
goto done; |
884 |
} |
} |
885 |
|
|
|
/* flush complete: reset counters */ |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
|
|
|
886 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
887 |
|
|
888 |
for divx5 decoder compatibility, this marker must consist |
for divx5 decoder compatibility, this marker must consist |
890 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
891 |
*/ |
*/ |
892 |
|
|
893 |
if ((pEnc->mbParam.global_flags & XVID_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { |
894 |
int tmp; |
int tmp; |
895 |
int bits; |
int bits; |
896 |
|
|
909 |
|
|
910 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
911 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
912 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
|
|
|
|
// xFrame->out_flags |= XVID_PACKED_FIXUP?; |
|
913 |
|
|
914 |
|
/* flush complete: reset counters */ |
915 |
|
pEnc->flush_bframes = 0; |
916 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
917 |
goto done; |
goto done; |
918 |
|
|
919 |
} |
} |
|
} |
|
920 |
|
|
921 |
|
/* flush complete: reset counters */ |
922 |
|
pEnc->flush_bframes = 0; |
923 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
924 |
|
} |
925 |
|
|
926 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
927 |
* dequeue frame from the encoding queue |
* dequeue frame from the encoding queue |
931 |
{ |
{ |
932 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
933 |
{ |
{ |
934 |
if (pEnc->bframenum_tail > 0) /* are bframes */ |
|
935 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { |
936 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
937 |
|
} |
938 |
|
|
939 |
|
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
940 |
|
if (pEnc->bframenum_tail > 0) |
941 |
{ |
{ |
942 |
|
|
943 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
944 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
945 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
946 |
|
|
947 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
948 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
949 |
|
|
950 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
951 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
952 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
953 |
|
} |
954 |
|
|
955 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
956 |
|
|
957 |
goto done_flush; |
goto done_flush; |
958 |
} |
} |
959 |
|
|
974 |
|
|
975 |
|
|
976 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
977 |
* init pEnc->current field |
* init pEnc->current fields |
978 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
979 |
|
|
|
emms(); /* float-point region */ |
|
|
|
|
980 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
981 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
982 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
988 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
989 |
} |
} |
990 |
|
|
991 |
if (xFrame->vop_flags & XVID_EXTRASTATS) |
emms(); /* float-point region */ |
|
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
} |
|
|
|
|
992 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
993 |
int *temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
unsigned int x, y; |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
|
994 |
|
|
995 |
pEnc->current->quant = |
pEnc->current->quant = |
996 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
997 |
pEnc->mbParam.edged_width, temp_dquants, |
pEnc->mbParam.edged_width, pEnc->temp_dquants, |
998 |
pEnc->current->quant, pEnc->current->quant, |
pEnc->current->quant, pEnc->current->quant, |
999 |
2 * pEnc->current->quant, |
2 * pEnc->current->quant, |
1000 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
1007 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1008 |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
1009 |
|
|
1010 |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2]; |
1011 |
} |
} |
1012 |
|
|
1013 |
#undef OFFSET |
#undef OFFSET |
1014 |
} |
} |
1015 |
|
|
|
xvid_free(temp_dquants); |
|
1016 |
} |
} |
1017 |
|
emms(); /* END floating-point region */ |
1018 |
|
|
1019 |
if (frame->quant > 0) |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1020 |
{ |
* frame type & quant selection |
1021 |
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, ...); |
|
|
} |
|
1022 |
|
|
1023 |
emms(); /* END floating-point region */ |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
1024 |
|
|
1025 |
|
if (frame->type > 0) |
1026 |
|
type = frame->type; |
1027 |
|
|
1028 |
if (type > 0) /* XVID_TYPE_?VOP */ |
if (frame->quant > 0) |
1029 |
{ |
pEnc->current->quant = frame->quant; |
1030 |
|
|
1031 |
|
if (type > 0){ /* XVID_TYPE_?VOP */ |
1032 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1033 |
} |
} else{ /* XVID_TYPE_AUTO */ |
1034 |
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)) |
|
|
{ |
|
1035 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1036 |
type = I_VOP; |
type = I_VOP; |
1037 |
}else{ |
}else{ |
1038 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1039 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1040 |
pEnc->iFrameNum, pEnc->bframenum_tail); |
pEnc->iFrameNum, pEnc->bframenum_tail); |
1041 |
|
|
1042 |
|
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { |
1043 |
|
type = P_VOP; /* disable dynamic bframes */ |
1044 |
|
} |
1045 |
|
} |
1046 |
|
} |
1047 |
|
|
1048 |
|
/* bframes buffer overflow check */ |
1049 |
|
if (type != I_VOP) { |
1050 |
|
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1051 |
|
type = P_VOP; |
1052 |
|
}else{ |
1053 |
|
type = B_VOP; |
1054 |
} |
} |
1055 |
} |
} |
1056 |
|
|
1057 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1058 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1059 |
|
|
|
|
|
1060 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1061 |
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, |
1062 |
"%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); |
1063 |
} |
} |
1064 |
|
|
1065 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1066 |
* ivop/pvop/bvop selection |
* encode this frame as a b-vop |
1067 |
|
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1068 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1069 |
|
if (type == B_VOP) { |
1070 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1071 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1072 |
|
} |
1073 |
|
|
1074 |
if (type == I_VOP) { |
if (frame->bquant < 1) { |
1075 |
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1076 |
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1077 |
|
|
1078 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
} else { |
1079 |
|
pEnc->current->quant = frame->bquant; |
1080 |
|
} |
1081 |
|
|
1082 |
|
if (pEnc->current->quant < 1) |
1083 |
|
pEnc->current->quant = 1; |
1084 |
|
else if (pEnc->current->quant > 31) |
1085 |
|
pEnc->current->quant = 31; |
1086 |
|
|
1087 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i", |
1088 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1089 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1090 |
|
|
1091 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
/* store frame into bframe buffer & swap ref back to current */ |
1092 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1093 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1094 |
|
|
1095 |
|
pEnc->bframenum_tail++; |
1096 |
|
|
1097 |
|
goto repeat; |
1098 |
|
} |
1099 |
|
|
1100 |
|
/* for unpacked bframes, output the stats for the last encoded frame */ |
1101 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1102 |
|
{ |
1103 |
|
if (pEnc->current->stamp > 0) { |
1104 |
|
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1105 |
|
} |
1106 |
|
else |
1107 |
|
stats->type = XVID_TYPE_NOTHING; |
1108 |
} |
} |
1109 |
|
|
1110 |
// when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1111 |
if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
* closed-gop |
1112 |
|
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1113 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1114 |
|
|
1115 |
|
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1116 |
|
|
1117 |
// place this frame back on the encoding-queue (head) |
// place this frame back on the encoding-queue (head) |
1118 |
// we will deal with it next time |
// we will deal with it next time |
1119 |
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); |
1120 |
|
pEnc->queue_size++; |
1121 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1122 |
|
|
1123 |
|
dec_frame_num(pEnc); |
1124 |
|
pEnc->iFrameNum--; |
1125 |
|
|
1126 |
// grab the last frame from the bframe-queue |
// grab the last frame from the bframe-queue |
1127 |
|
|
1128 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1132 |
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"); |
1133 |
} |
} |
1134 |
|
|
1135 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1136 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
1137 |
|
type = P_VOP; |
1138 |
|
} |
1139 |
|
|
|
FrameCodeP(pEnc, &bs, 1, 0); |
|
1140 |
|
|
1141 |
} else { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1142 |
|
* encode this frame as an i-vop |
1143 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1144 |
|
|
1145 |
|
if (type == I_VOP) { |
1146 |
|
|
1147 |
|
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1148 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1149 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1150 |
|
|
1151 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1152 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1153 |
|
} |
1154 |
|
|
1155 |
|
|
1156 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1157 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1173 |
|
|
1174 |
/* ^^^------------------------ */ |
/* ^^^------------------------ */ |
1175 |
|
|
1176 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1177 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1178 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1179 |
|
} |
1180 |
|
|
1181 |
FrameCodeI(pEnc, &bs); |
FrameCodeI(pEnc, &bs); |
1182 |
xFrame->out_flags |= XVID_KEYFRAME; |
xFrame->out_flags |= XVID_KEYFRAME; |
|
} |
|
1183 |
|
|
1184 |
} else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) || |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1185 |
pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
* encode this frame as an p-vop |
1186 |
/* |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1187 |
* This will be coded as a Predicted Frame |
|
1188 |
*/ |
} else { // (type == P_VOP || type == S_VOP) |
1189 |
|
|
1190 |
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", |
1191 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1192 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1193 |
|
|
1195 |
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"); |
1196 |
} |
} |
1197 |
|
|
1198 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1199 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1200 |
} 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; |
|
1201 |
} |
} |
1202 |
|
|
1203 |
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; |
|
|
|
|
1204 |
} |
} |
1205 |
|
|
|
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); |
|
|
} |
|
1206 |
|
|
1207 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1208 |
* okay, on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1209 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1210 |
|
|
1211 |
done_flush: |
done_flush: |
1212 |
|
|
|
{ |
|
|
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); |
|
|
} |
|
|
|
|
|
/* packed_mode: repeat */ |
|
1213 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1214 |
|
|
1215 |
|
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1216 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
1217 |
goto repeat; |
goto repeat; |
1218 |
} |
} |
1219 |
|
|
1220 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
/* packed or no-bframes: output stats */ |
1221 |
{ |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) { |
1222 |
/* packed(and low_delay) encoding gives us graceful reorded-stats output */ |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
|
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
|
|
} |
|
|
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); |
|
|
else |
|
|
stats->type = XVID_TYPE_NOTHING; |
|
1223 |
} |
} |
1224 |
|
|
1225 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1293 |
stop_edges_timer(); |
stop_edges_timer(); |
1294 |
} |
} |
1295 |
|
|
|
//pEnc->iFrameNum = 0; |
|
1296 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1297 |
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; |
|
1298 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1299 |
|
|
1300 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1860 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1861 |
|
|
1862 |
mb->cbp = |
mb->cbp = |
1863 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1864 |
|
|
1865 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1866 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |