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.16 2003-03-27 17:09:48 edgomez 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" |
54 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
55 |
|
|
56 |
/***************************************************************************** |
/***************************************************************************** |
|
* Local macros |
|
|
****************************************************************************/ |
|
|
|
|
|
#define SWAP(_T_,A,B) { _T_ tmp = A; A = B; B = tmp; } |
|
|
|
|
|
/***************************************************************************** |
|
57 |
* Local function prototypes |
* Local function prototypes |
58 |
****************************************************************************/ |
****************************************************************************/ |
59 |
|
|
69 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
70 |
Bitstream * bs); |
Bitstream * bs); |
71 |
|
|
|
/***************************************************************************** |
|
|
* Local data |
|
|
****************************************************************************/ |
|
|
|
|
|
static int DQtab[4] = { |
|
|
-1, -2, 1, 2 |
|
|
}; |
|
|
|
|
|
static int iDQtab[5] = { |
|
|
1, 0, NO_CHANGE, 2, 3 |
|
|
}; |
|
|
|
|
72 |
|
|
73 |
/***************************************************************************** |
/***************************************************************************** |
74 |
* Encoder creation |
* Encoder creation |
116 |
|
|
117 |
|
|
118 |
int |
int |
119 |
enc_create(xvid_enc_create_t * create, xvid_enc_rc_t * rc) |
enc_create(xvid_enc_create_t * create) |
120 |
{ |
{ |
121 |
Encoder *pEnc; |
Encoder *pEnc; |
122 |
int n; |
int n; |
123 |
|
|
124 |
if (XVID_MAJOR(create->version) != 1 || (rc && XVID_MAJOR(rc->version) != 1)) /* v1.x.x */ |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
125 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
126 |
|
|
127 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
148 |
/* framerate */ |
/* framerate */ |
149 |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
pEnc->mbParam.fincr = MAX(create->fincr, 0); |
150 |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
pEnc->mbParam.fbase = create->fincr <= 0 ? 25 : create->fbase; |
151 |
|
if (pEnc->mbParam.fincr>0) |
152 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
153 |
|
|
154 |
/* plugin */ |
/* plugin */ |
164 |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
165 |
pinfo.version = XVID_VERSION; |
pinfo.version = XVID_VERSION; |
166 |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
167 |
pEnc->plugin_flags |= pinfo.flags; |
pEnc->mbParam.plugin_flags |= pinfo.flags; |
168 |
} |
} |
169 |
|
|
170 |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
179 |
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) { |
180 |
pEnc->plugins[n].func = create->plugins[n].func; |
pEnc->plugins[n].func = create->plugins[n].func; |
181 |
} |
} |
182 |
|
} |
183 |
|
|
184 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_EXTRASTATS_ENABLE) || |
185 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
186 |
|
pEnc->mbParam.plugin_flags |= XVID_REQORIGINAL; /* psnr calculation requires the original */ |
187 |
} |
} |
188 |
|
|
189 |
/* temp dquants */ |
/* temp dquants */ |
190 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
191 |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
192 |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
193 |
|
} |
194 |
/* XXX: error checking */ |
/* XXX: error checking */ |
195 |
|
|
196 |
/* bframes */ |
/* bframes */ |
207 |
|
|
208 |
/* Bitrate allocator defaults |
/* Bitrate allocator defaults |
209 |
|
|
|
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; |
|
|
|
|
|
|
|
|
|
|
210 |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
211 |
create->min_quantizer = 1; |
create->min_quantizer = 1; |
212 |
|
|
214 |
create->max_quantizer = 31; |
create->max_quantizer = 31; |
215 |
|
|
216 |
if (create->max_quantizer < create->min_quantizer) |
if (create->max_quantizer < create->min_quantizer) |
217 |
create->max_quantizer = create->min_quantizer; |
create->max_quantizer = create->min_quantizer; */ |
|
|
|
|
pEnc->bitrate = create->rc_bitrate; */ |
|
218 |
|
|
219 |
|
|
220 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
239 |
|
|
240 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
241 |
|
|
242 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
243 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
244 |
|
image_null(&pEnc->sOriginal2); |
245 |
|
} |
246 |
|
|
247 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
248 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
256 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
257 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
258 |
|
|
259 |
if (create->global & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
260 |
{ if (image_create |
if (image_create |
261 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
262 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
263 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
264 |
|
|
265 |
|
if (image_create |
266 |
|
(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
267 |
|
pEnc->mbParam.edged_height) < 0) |
268 |
|
goto xvid_err_memory3; |
269 |
} |
} |
270 |
|
|
271 |
if (image_create |
if (image_create |
400 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
401 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
402 |
|
|
|
/* 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); |
|
|
} */ |
|
|
|
|
403 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
404 |
|
|
405 |
init_timer(); |
init_timer(); |
446 |
|
|
447 |
xvid_err_memory3: |
xvid_err_memory3: |
448 |
|
|
449 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
450 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
451 |
|
pEnc->mbParam.edged_height); |
452 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
453 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
454 |
} |
} |
455 |
|
|
488 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
489 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
490 |
|
|
491 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
492 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
493 |
|
} |
494 |
|
|
495 |
xvid_err_memory0: |
xvid_err_memory0: |
496 |
for (n=0; n<pEnc->num_plugins;n++) { |
for (n=0; n<pEnc->num_plugins;n++) { |
578 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
579 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
580 |
|
|
581 |
if (pEnc->mbParam.global_flags & XVID_EXTRASTATS_ENABLE) |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
582 |
{ image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
583 |
|
pEnc->mbParam.edged_height); |
584 |
|
image_destroy(&pEnc->sOriginal2, pEnc->mbParam.edged_width, |
585 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
586 |
} |
} |
587 |
|
|
593 |
xvid_free(pEnc->reference->mbs); |
xvid_free(pEnc->reference->mbs); |
594 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
595 |
|
|
596 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
597 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
598 |
|
} |
599 |
|
|
600 |
|
|
601 |
|
if (pEnc->num_plugins>0) |
602 |
|
{ |
603 |
|
xvid_plg_destroy_t pdestroy; |
604 |
|
memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); |
605 |
|
|
606 |
|
pdestroy.version = XVID_VERSION; |
607 |
|
pdestroy.num_frames = pEnc->m_framenum; |
608 |
|
|
609 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
610 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
611 |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, &pdestroy, 0); |
612 |
} |
} |
613 |
} |
} |
614 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
615 |
|
} |
616 |
|
|
617 |
xvid_free(pEnc); |
xvid_free(pEnc); |
618 |
|
|
624 |
call the plugins |
call the plugins |
625 |
*/ |
*/ |
626 |
|
|
627 |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, int opt, int * type, int * quant) |
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
628 |
|
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
629 |
{ |
{ |
630 |
int i; |
unsigned int i, j; |
631 |
xvid_plg_data_t data; |
xvid_plg_data_t data; |
632 |
|
|
633 |
|
/* set data struct */ |
634 |
|
|
635 |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
636 |
data.version = XVID_VERSION; |
data.version = XVID_VERSION; |
637 |
|
|
638 |
|
data.width = pEnc->mbParam.width; |
639 |
|
data.height = pEnc->mbParam.height; |
640 |
|
data.mb_width = pEnc->mbParam.mb_width; |
641 |
|
data.mb_height = pEnc->mbParam.mb_height; |
642 |
|
data.fincr = frame->fincr; |
643 |
|
data.fbase = pEnc->mbParam.fbase; |
644 |
|
|
645 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_USER; |
646 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
647 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
658 |
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
659 |
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
660 |
|
|
661 |
data.original.csp = XVID_CSP_NULL; |
data.frame_num = frame->frame_num; |
|
/* todo: data.original */ |
|
662 |
|
|
663 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
664 |
data.type = XVID_TYPE_AUTO; |
data.type = XVID_TYPE_AUTO; |
665 |
data.quant = 2; |
data.quant = 2; |
666 |
//memset(pEnc->temp_dquants, NO_CHANGE, pEnc->mbParam.width * pEnc->mbParam.height); |
|
667 |
//data.qscale_stride = pEnc->mbParam.width; |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
668 |
//data.qscale_table = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
669 |
/* todo: vol,vop,motion flags */ |
data.dquant_stride = pEnc->mbParam.mb_width; |
670 |
|
memset(data.dquant, 0, data.mb_width*data.mb_height); |
671 |
|
} |
672 |
|
|
673 |
|
/* todo: [vol,vop,motion]_flags*/ |
674 |
|
|
675 |
} else { // XVID_PLG_AFTER |
} else { // XVID_PLG_AFTER |
676 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
677 |
|
data.original.csp = XVID_CSP_USER; |
678 |
|
data.original.plane[0] = original->y; |
679 |
|
data.original.plane[1] = original->u; |
680 |
|
data.original.plane[2] = original->v; |
681 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
682 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
683 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
684 |
|
} |
685 |
|
|
686 |
|
if ((frame->vol_flags & XVID_VOL_EXTRASTATS) || |
687 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
688 |
|
|
689 |
|
data.sse_y = |
690 |
|
plane_sse( original->y, frame->image.y, |
691 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
692 |
|
pEnc->mbParam.height); |
693 |
|
|
694 |
|
data.sse_u = |
695 |
|
plane_sse( original->u, frame->image.u, |
696 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
697 |
|
pEnc->mbParam.height/2); |
698 |
|
|
699 |
|
data.sse_v = |
700 |
|
plane_sse( original->v, frame->image.v, |
701 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
702 |
|
pEnc->mbParam.height/2); |
703 |
|
} |
704 |
|
|
705 |
data.type = coding2type(frame->coding_type); |
data.type = coding2type(frame->coding_type); |
706 |
data.quant = frame->quant; |
data.quant = frame->quant; |
707 |
/* todo: data.qscale */ |
|
708 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
709 |
|
data.dquant = pEnc->temp_dquants; |
710 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
711 |
|
|
712 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
713 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
714 |
|
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
715 |
|
} |
716 |
|
} |
717 |
|
|
718 |
data.vol_flags = frame->vol_flags; |
data.vol_flags = frame->vol_flags; |
719 |
data.vop_flags = frame->vop_flags; |
data.vop_flags = frame->vop_flags; |
720 |
data.motion_flags = frame->motion_flags; |
data.motion_flags = frame->motion_flags; |
723 |
data.kblks = frame->sStat.kblks; |
data.kblks = frame->sStat.kblks; |
724 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
725 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
726 |
|
|
727 |
|
if (stats) { |
728 |
|
stats->type = coding2type(frame->coding_type); |
729 |
|
stats->quant = frame->quant; |
730 |
|
stats->vol_flags = frame->vol_flags; |
731 |
|
stats->vop_flags = frame->vop_flags; |
732 |
|
stats->length = frame->length; |
733 |
|
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
734 |
|
stats->kblks = frame->sStat.kblks; |
735 |
|
stats->mblks = frame->sStat.mblks; |
736 |
|
stats->ublks = frame->sStat.ublks; |
737 |
|
stats->sse_y = data.sse_y; |
738 |
|
stats->sse_u = data.sse_u; |
739 |
|
stats->sse_v = data.sse_v; |
740 |
|
} |
741 |
} |
} |
742 |
|
|
743 |
|
/* call plugins */ |
744 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<pEnc->num_plugins;i++) { |
745 |
|
emms(); |
746 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
747 |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
748 |
continue; |
continue; |
749 |
} |
} |
750 |
} |
} |
751 |
} |
} |
752 |
|
emms(); |
753 |
|
|
754 |
|
/* copy modified values back into frame*/ |
755 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
756 |
*type = data.type; |
*type = data.type; |
757 |
*quant = data.quant; |
*quant = data.quant; |
|
/* todo: copy modified qscale,vol,vop,motion flags into the frame*/ |
|
|
} |
|
758 |
|
|
759 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
760 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
761 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
762 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; |
763 |
|
} |
764 |
|
}else{ |
765 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
766 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
767 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
768 |
|
} |
769 |
|
} |
770 |
|
/* todo: [vol,vop,motion]_flags*/ |
771 |
|
} |
772 |
} |
} |
773 |
|
|
774 |
|
|
776 |
|
|
777 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
778 |
{ |
{ |
779 |
|
pEnc->current->frame_num = pEnc->m_framenum; |
780 |
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; |
|
781 |
|
|
782 |
|
pEnc->mbParam.m_stamp += pEnc->current->fincr; |
783 |
pEnc->m_framenum++; /* debug ticker */ |
pEnc->m_framenum++; /* debug ticker */ |
784 |
} |
} |
785 |
|
|
786 |
|
static __inline void dec_frame_num(Encoder * pEnc) |
787 |
|
{ |
788 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
789 |
|
pEnc->m_framenum--; /* debug ticker */ |
790 |
|
} |
791 |
|
|
792 |
|
|
793 |
|
|
794 |
static __inline void |
static __inline void |
811 |
} |
} |
812 |
|
|
813 |
|
|
|
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 ); |
|
|
} |
|
|
|
|
|
|
|
814 |
|
|
815 |
/***************************************************************************** |
/***************************************************************************** |
816 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
825 |
****************************************************************************/ |
****************************************************************************/ |
826 |
|
|
827 |
|
|
|
/* XXX: dx50bvop broken! something todo with queue */ |
|
|
|
|
828 |
int |
int |
829 |
enc_encode(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
830 |
xvid_enc_frame_t * xFrame, |
xvid_enc_frame_t * xFrame, |
832 |
{ |
{ |
833 |
xvid_enc_frame_t * frame; |
xvid_enc_frame_t * frame; |
834 |
int type; |
int type; |
|
uint16_t x, y; |
|
835 |
Bitstream bs; |
Bitstream bs; |
836 |
|
|
837 |
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 */ |
855 |
if (image_input |
if (image_input |
856 |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&q->image, pEnc->mbParam.width, pEnc->mbParam.height, |
857 |
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
pEnc->mbParam.edged_width, (uint8_t**)xFrame->input.plane, xFrame->input.stride, |
858 |
xFrame->input.csp, xFrame->vol_flags & XVID_INTERLACING)) |
xFrame->input.csp, xFrame->vol_flags & XVID_VOL_INTERLACING)) |
859 |
{ |
{ |
860 |
emms(); |
emms(); |
861 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
862 |
} |
} |
863 |
stop_conv_timer(); |
stop_conv_timer(); |
864 |
|
|
865 |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
866 |
image_chroma_optimize(&q->image, |
image_chroma_optimize(&q->image, |
867 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
868 |
} |
} |
894 |
|
|
895 |
if (pEnc->flush_bframes) |
if (pEnc->flush_bframes) |
896 |
{ |
{ |
897 |
if (pEnc->bframenum_head < pEnc->bframenum_tail) |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
|
{ |
|
898 |
|
|
899 |
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", |
900 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
901 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
902 |
|
|
903 |
if (pEnc->bframes[pEnc->bframenum_head]->vop_flags & XVID_EXTRASTATS) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
904 |
image_copy(&pEnc->sOriginal, &pEnc->bframes[pEnc->bframenum_head]->image, |
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
905 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
906 |
} |
} |
907 |
|
|
908 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
909 |
|
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats); |
|
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]); |
|
|
emms(); |
|
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
|
|
emms(); |
|
|
|
|
910 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
911 |
|
|
|
|
|
912 |
goto done; |
goto done; |
913 |
} |
} |
914 |
|
|
|
/* flush complete: reset counters */ |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
|
|
|
915 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
916 |
|
|
917 |
for divx5 decoder compatibility, this marker must consist |
for divx5 decoder compatibility, this marker must consist |
919 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
920 |
*/ |
*/ |
921 |
|
|
922 |
if ((pEnc->mbParam.global_flags & XVID_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED && pEnc->bframenum_tail > 0)) { |
923 |
int tmp; |
int tmp; |
924 |
int bits; |
int bits; |
925 |
|
|
938 |
|
|
939 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
940 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
941 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->current); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
|
emms(); |
|
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
|
|
emms(); |
|
|
|
|
|
// xFrame->out_flags |= XVID_PACKED_FIXUP?; |
|
942 |
|
|
943 |
|
/* flush complete: reset counters */ |
944 |
|
pEnc->flush_bframes = 0; |
945 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
946 |
goto done; |
goto done; |
947 |
|
|
948 |
} |
} |
|
} |
|
949 |
|
|
950 |
|
/* flush complete: reset counters */ |
951 |
|
pEnc->flush_bframes = 0; |
952 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
953 |
|
} |
954 |
|
|
955 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
956 |
* dequeue frame from the encoding queue |
* dequeue frame from the encoding queue |
960 |
{ |
{ |
961 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
962 |
{ |
{ |
963 |
if (pEnc->bframenum_tail > 0) /* are bframes */ |
|
964 |
|
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
965 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
966 |
|
} |
967 |
|
|
968 |
|
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
969 |
|
if (pEnc->bframenum_tail > 0) |
970 |
{ |
{ |
971 |
|
|
972 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
973 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
974 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
975 |
|
|
976 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
977 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
978 |
|
|
979 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
980 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
981 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
982 |
|
} |
983 |
|
|
984 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
985 |
|
|
986 |
goto done_flush; |
goto done_flush; |
987 |
} |
} |
988 |
|
|
1003 |
|
|
1004 |
|
|
1005 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1006 |
* init pEnc->current field |
* init pEnc->current fields |
1007 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1008 |
|
|
1009 |
emms(); /* float-point region */ |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1010 |
|
inc_frame_num(pEnc); |
1011 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
1012 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1013 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1014 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1015 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
1016 |
|
|
1017 |
if ((xFrame->vop_flags & XVID_CHROMAOPT)) { |
|
1018 |
|
if ((xFrame->vop_flags & XVID_VOP_CHROMAOPT)) { |
1019 |
image_chroma_optimize(&pEnc->current->image, |
image_chroma_optimize(&pEnc->current->image, |
1020 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1021 |
} |
} |
1022 |
|
|
1023 |
if (xFrame->vop_flags & XVID_EXTRASTATS) { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1024 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
* frame type & quant selection |
1025 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
} |
|
|
|
|
|
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
|
|
|
|
|
pEnc->current->quant = |
|
|
adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->temp_dquants, |
|
|
pEnc->current->quant, pEnc->current->quant, |
|
|
2 * pEnc->current->quant, |
|
|
pEnc->mbParam.mb_width, |
|
|
pEnc->mbParam.mb_height); |
|
|
|
|
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
|
|
|
|
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
|
|
|
|
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
|
|
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
|
|
|
|
|
pMB->dquant = iDQtab[pEnc->temp_dquants[OFFSET(x, y)] + 2]; |
|
|
} |
|
|
|
|
|
#undef OFFSET |
|
|
} |
|
|
|
|
|
} |
|
1026 |
|
|
1027 |
emms(); /* END floating-point region */ |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
|
call_plugins(pEnc, pEnc->current, XVID_PLG_BEFORE, &type, &pEnc->current->quant); |
|
|
emms(); |
|
1028 |
|
|
1029 |
if (frame->type > 0) |
if (frame->type > 0) |
1030 |
type = frame->type; |
type = frame->type; |
1032 |
if (frame->quant > 0) |
if (frame->quant > 0) |
1033 |
pEnc->current->quant = frame->quant; |
pEnc->current->quant = frame->quant; |
1034 |
|
|
1035 |
if (type > 0) /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
|
{ |
|
1036 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1037 |
} |
} else{ /* XVID_TYPE_AUTO */ |
1038 |
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)) |
|
|
{ |
|
1039 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1040 |
type = I_VOP; |
type = I_VOP; |
1041 |
}else{ |
}else{ |
1042 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1043 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1044 |
pEnc->iFrameNum, pEnc->bframenum_tail); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
1045 |
|
|
1046 |
|
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_VOP_DYNAMIC_BFRAMES)) { |
1047 |
|
type = P_VOP; /* disable dynamic bframes */ |
1048 |
|
} |
1049 |
|
} |
1050 |
|
} |
1051 |
|
|
1052 |
|
/* bframes buffer overflow check */ |
1053 |
|
if (type != I_VOP) { |
1054 |
|
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1055 |
|
type = P_VOP; |
1056 |
|
}else{ |
1057 |
|
type = B_VOP; |
1058 |
} |
} |
1059 |
} |
} |
|
inc_frame_num(pEnc); |
|
|
pEnc->iFrameNum++; |
|
1060 |
|
|
1061 |
|
pEnc->iFrameNum++; |
1062 |
|
|
1063 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1064 |
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, |
1065 |
"%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); |
1066 |
} |
} |
1067 |
|
|
1068 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1069 |
* ivop/pvop/bvop selection |
* encode this frame as a b-vop |
1070 |
|
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1071 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1072 |
|
if (type == B_VOP) { |
1073 |
|
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1074 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1075 |
|
} |
1076 |
|
|
1077 |
if (type == I_VOP) { |
if (frame->quant < 1) { |
1078 |
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1079 |
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1080 |
|
|
1081 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
} else { |
1082 |
|
pEnc->current->quant = frame->quant; |
1083 |
|
} |
1084 |
|
|
1085 |
|
if (pEnc->current->quant < 1) |
1086 |
|
pEnc->current->quant = 1; |
1087 |
|
else if (pEnc->current->quant > 31) |
1088 |
|
pEnc->current->quant = 31; |
1089 |
|
|
1090 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i", |
1091 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1092 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1093 |
|
|
1094 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
/* store frame into bframe buffer & swap ref back to current */ |
1095 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1096 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1097 |
|
|
1098 |
|
pEnc->bframenum_tail++; |
1099 |
|
|
1100 |
|
goto repeat; |
1101 |
} |
} |
1102 |
|
|
1103 |
// 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 */ |
1104 |
if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1105 |
|
{ |
1106 |
|
if (pEnc->current->stamp > 0) { |
1107 |
|
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1108 |
|
} |
1109 |
|
else |
1110 |
|
stats->type = XVID_TYPE_NOTHING; |
1111 |
|
} |
1112 |
|
|
1113 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1114 |
|
* closed-gop |
1115 |
|
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1116 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1117 |
|
|
1118 |
|
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1119 |
|
|
1120 |
// place this frame back on the encoding-queue (head) |
// place this frame back on the encoding-queue (head) |
1121 |
// we will deal with it next time |
// we will deal with it next time |
1122 |
|
dec_frame_num(pEnc); |
1123 |
|
pEnc->iFrameNum--; |
1124 |
|
|
1125 |
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); |
1126 |
|
pEnc->queue_size++; |
1127 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1128 |
|
|
1129 |
// grab the last frame from the bframe-queue |
// grab the last frame from the bframe-queue |
1131 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1132 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1133 |
|
|
1134 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1135 |
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"); |
1136 |
} |
} |
1137 |
|
|
1138 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1139 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
1140 |
|
type = P_VOP; |
1141 |
|
} |
1142 |
|
|
|
FrameCodeP(pEnc, &bs, 1, 0); |
|
1143 |
|
|
1144 |
} else { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1145 |
|
* encode this frame as an i-vop |
1146 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1147 |
|
|
1148 |
|
if (type == I_VOP) { |
1149 |
|
|
1150 |
|
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1151 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1152 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1153 |
|
|
1154 |
|
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1155 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1156 |
|
} |
1157 |
|
|
1158 |
|
|
1159 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1160 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1161 |
|
|
1162 |
if ((pEnc->mbParam.vol_flags & XVID_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1163 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1164 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(frame->quant_intra_matrix); |
1165 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1168 |
|
|
1169 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1170 |
|
|
1171 |
if (!(pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
1172 |
pEnc->current->vop_flags &= ~XVID_REDUCED; |
pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; |
1173 |
|
|
1174 |
if (!(pEnc->current->vol_flags & XVID_INTERLACING)) |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1175 |
pEnc->current->vop_flags &= ~(XVID_TOPFIELDFIRST|XVID_ALTERNATESCAN); |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
1176 |
|
|
1177 |
/* ^^^------------------------ */ |
/* ^^^------------------------ */ |
1178 |
|
|
1179 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1180 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1181 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1182 |
|
} |
1183 |
|
|
1184 |
FrameCodeI(pEnc, &bs); |
FrameCodeI(pEnc, &bs); |
1185 |
xFrame->out_flags |= XVID_KEYFRAME; |
xFrame->out_flags |= XVID_KEYFRAME; |
|
} |
|
1186 |
|
|
1187 |
} else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) || |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1188 |
pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
* encode this frame as an p-vop |
1189 |
/* |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1190 |
* This will be coded as a Predicted Frame |
|
1191 |
*/ |
} else { // (type == P_VOP || type == S_VOP) |
1192 |
|
|
1193 |
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", |
1194 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1195 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1196 |
|
|
1197 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1198 |
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"); |
1199 |
} |
} |
1200 |
|
|
1201 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1202 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1203 |
} 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; |
|
1204 |
} |
} |
1205 |
|
|
1206 |
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; |
|
|
|
|
1207 |
} |
} |
1208 |
|
|
|
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); |
|
|
} |
|
1209 |
|
|
1210 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1211 |
* okay, on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1212 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1213 |
|
|
1214 |
done_flush: |
done_flush: |
1215 |
|
|
|
#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 */ |
|
1216 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1217 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
|
1218 |
|
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1219 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1220 |
goto repeat; |
goto repeat; |
1221 |
} |
} |
1222 |
|
|
1223 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
/* packed or no-bframes: output stats */ |
1224 |
{ |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) { |
1225 |
/* 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); |
|
|
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); |
|
|
emms(); |
|
|
call_plugins(pEnc, pEnc->current, XVID_PLG_AFTER, 0, 0); |
|
|
emms(); |
|
|
} |
|
|
else |
|
|
stats->type = XVID_TYPE_NOTHING; |
|
1226 |
} |
} |
1227 |
|
|
1228 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1239 |
} |
} |
1240 |
|
|
1241 |
|
|
1242 |
|
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1243 |
|
{ |
1244 |
|
unsigned int i,j; |
1245 |
|
int quant = frame->quant; |
1246 |
|
|
1247 |
|
for (j=0; j<pParam->mb_height; j++) |
1248 |
|
for (i=0; i<pParam->mb_width; i++) { |
1249 |
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
1250 |
|
quant += pMB->dquant; |
1251 |
|
if (quant > 31) |
1252 |
|
quant = 31; |
1253 |
|
if (quant < 1) |
1254 |
|
quant = 1; |
1255 |
|
pMB->quant = quant; |
1256 |
|
} |
1257 |
|
} |
1258 |
|
|
1259 |
|
|
1260 |
static __inline void |
static __inline void |
1261 |
CodeIntraMB(Encoder * pEnc, |
CodeIntraMB(Encoder * pEnc, |
1262 |
MACROBLOCK * pMB) |
MACROBLOCK * pMB) |
1270 |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
1271 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1272 |
|
|
1273 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1274 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
|
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
1275 |
} |
} |
1276 |
} |
} |
1277 |
|
|
|
pMB->quant = pEnc->current->quant; |
|
|
} |
|
|
|
|
1278 |
|
|
1279 |
|
|
1280 |
static int |
static int |
1290 |
|
|
1291 |
uint16_t x, y; |
uint16_t x, y; |
1292 |
|
|
1293 |
if ((pEnc->current->vol_flags & XVID_REDUCED_ENABLE)) |
if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
1294 |
{ |
{ |
1295 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1296 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1304 |
stop_edges_timer(); |
stop_edges_timer(); |
1305 |
} |
} |
1306 |
|
|
|
//pEnc->iFrameNum = 0; |
|
1307 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1308 |
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; |
|
1309 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1310 |
|
|
1311 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1312 |
|
|
1313 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1314 |
|
|
1315 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1336 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1337 |
|
|
1338 |
start_timer(); |
start_timer(); |
1339 |
if (pEnc->current->vop_flags & XVID_GREYSCALE) |
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1340 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1341 |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1342 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1345 |
stop_coding_timer(); |
stop_coding_timer(); |
1346 |
} |
} |
1347 |
|
|
1348 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1349 |
{ |
{ |
1350 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1351 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1354 |
emms(); |
emms(); |
1355 |
|
|
1356 |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1357 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1358 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1359 |
else |
else |
1360 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1400 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1401 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1402 |
|
|
1403 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1404 |
{ |
{ |
1405 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pEnc->mbParam.width + 31) / 32; |
1406 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pEnc->mbParam.height + 31) / 32; |
1422 |
else |
else |
1423 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1424 |
|
|
1425 |
if ((pEnc->current->vop_flags & XVID_HALFPEL)) { |
if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { |
1426 |
start_timer(); |
start_timer(); |
1427 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1428 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1429 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1430 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), |
1431 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1432 |
stop_inter_timer(); |
stop_inter_timer(); |
1433 |
} |
} |
1434 |
|
|
1435 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1436 |
|
|
1437 |
|
|
1438 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1439 |
|
|
1440 |
start_timer(); |
start_timer(); |
1441 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1442 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1450 |
|
|
1451 |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
1452 |
|
|
1453 |
if ( ( pEnc->current->vol_flags & XVID_GMC ) |
if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) |
1454 |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
1455 |
{ |
{ |
1456 |
pEnc->current->coding_type = S_VOP; |
pEnc->current->coding_type = S_VOP; |
1462 |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
1463 |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
1464 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
1465 |
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0, |
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0, |
1466 |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
1467 |
|
|
1468 |
} |
} |
1511 |
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1512 |
pEnc->mbParam.edged_width, 65536); |
pEnc->mbParam.edged_width, 65536); |
1513 |
|
|
1514 |
if (pEnc->current->motion_flags & PMV_CHROMA16) { |
if (pEnc->current->motion_flags & XVID_ME_CHROMA16) { |
1515 |
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
1516 |
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
1517 |
|
|
1521 |
|
|
1522 |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
1523 |
|
|
1524 |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
1525 |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
1526 |
else |
else |
1527 |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
1544 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1545 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1546 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1547 |
(pEnc->current->vol_flags & XVID_QUARTERPEL), |
(pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), |
1548 |
(pEnc->current->vop_flags & XVID_REDUCED), |
(pEnc->current->vop_flags & XVID_VOP_REDUCED), |
1549 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1550 |
|
|
1551 |
stop_comp_timer(); |
stop_comp_timer(); |
1552 |
|
|
1553 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1554 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
|
pEnc->current->quant += DQtab[pMB->dquant]; |
|
|
if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
else if (pEnc->current->quant < 1) |
|
|
pEnc->current->quant = 1; |
|
|
} |
|
1555 |
} |
} |
|
pMB->quant = pEnc->current->quant; |
|
1556 |
|
|
1557 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1558 |
|
|
1575 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1576 |
|
|
1577 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1578 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == 0); |
1579 |
|
|
1580 |
if (pEnc->current->coding_type == S_VOP) |
if (pEnc->current->coding_type == S_VOP) |
1581 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1582 |
else if (pEnc->current->coding_type == P_VOP) { |
else if (pEnc->current->coding_type == P_VOP) { |
1583 |
if ((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
1584 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1585 |
else |
else |
1586 |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1607 |
} |
} |
1608 |
|
|
1609 |
if (!bSkip) { /* no SKIP, but trivial block */ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1610 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1611 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1612 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1613 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1634 |
} |
} |
1635 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1636 |
|
|
1637 |
if ((pEnc->current->vop_flags & XVID_GREYSCALE)) |
if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) |
1638 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1639 |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1640 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1641 |
} |
} |
1642 |
|
|
1643 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1644 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1645 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1646 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1657 |
{ int k; |
{ int k; |
1658 |
for (k=1;k<4;k++) |
for (k=1;k<4;k++) |
1659 |
{ |
{ |
1660 |
if((pEnc->mbParam.vol_flags & XVID_QUARTERPEL)) { |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
1661 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1662 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1663 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1678 |
} |
} |
1679 |
} |
} |
1680 |
|
|
1681 |
if ((pEnc->current->vop_flags & XVID_REDUCED)) |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
1682 |
{ |
{ |
1683 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
1684 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
1700 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1701 |
|
|
1702 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1703 |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
1704 |
{ |
{ |
1705 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1706 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1707 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1708 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1709 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1710 |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
1711 |
{ |
{ |
1712 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1713 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1755 |
*/ |
*/ |
1756 |
|
|
1757 |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
1758 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1759 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1760 |
else |
else |
1761 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1786 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1787 |
} |
} |
1788 |
|
|
1789 |
/* XXX: pEnc->current->global_flags &= ~XVID_REDUCED; reduced resoltion not yet supported */ |
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
1790 |
|
|
1791 |
if (!first){ |
if (!first){ |
1792 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1802 |
start_timer(); |
start_timer(); |
1803 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1804 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1805 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1806 |
stop_inter_timer(); |
stop_inter_timer(); |
1807 |
|
|
1808 |
/* backward */ |
/* backward */ |
1812 |
start_timer(); |
start_timer(); |
1813 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1814 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1815 |
(pEnc->mbParam.vol_flags & XVID_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1816 |
stop_inter_timer(); |
stop_inter_timer(); |
1817 |
|
|
1818 |
start_timer(); |
start_timer(); |
1849 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1850 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1851 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1852 |
int direction = frame->vop_flags & XVID_ALTERNATESCAN ? 2 : 0; |
int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; |
1853 |
|
|
1854 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1855 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1857 |
continue; |
continue; |
1858 |
} |
} |
1859 |
|
|
1860 |
if (mb->mode != MODE_DIRECT_NONE_MV) { |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1861 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1862 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1863 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1867 |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1868 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1869 |
|
|
1870 |
mb->cbp = |
if (mb->mode != MODE_DIRECT_NONE_MV) |
1871 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1872 |
|
|
1873 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1874 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |