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.10 2003-03-20 08:04:18 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" |
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 |
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 */ |
155 |
|
pEnc->num_plugins = create->num_plugins; |
156 |
|
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
157 |
|
if (pEnc->plugins == NULL) |
158 |
|
goto xvid_err_memory0; |
159 |
|
|
160 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
161 |
|
xvid_plg_create_t pcreate; |
162 |
|
xvid_plg_info_t pinfo; |
163 |
|
|
164 |
|
memset(&pinfo, 0, sizeof(xvid_plg_info_t)); |
165 |
|
pinfo.version = XVID_VERSION; |
166 |
|
if (create->plugins[n].func(0, XVID_PLG_INFO, &pinfo, 0) >= 0) { |
167 |
|
pEnc->mbParam.plugin_flags |= pinfo.flags; |
168 |
|
} |
169 |
|
|
170 |
|
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
171 |
|
pcreate.version = XVID_VERSION; |
172 |
|
pcreate.width = pEnc->mbParam.width; |
173 |
|
pcreate.height = pEnc->mbParam.height; |
174 |
|
pcreate.fincr = pEnc->mbParam.fincr; |
175 |
|
pcreate.fbase = pEnc->mbParam.fbase; |
176 |
|
pcreate.param = create->plugins[n].param; |
177 |
|
|
178 |
|
pEnc->plugins[n].func = NULL; /* disable plugins that fail */ |
179 |
|
if (create->plugins[n].func(0, XVID_PLG_CREATE, &pcreate, &pEnc->plugins[n].param) >= 0) { |
180 |
|
pEnc->plugins[n].func = create->plugins[n].func; |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
if ((pEnc->mbParam.global_flags & XVID_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 */ |
190 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
191 |
|
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
192 |
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
193 |
|
} |
194 |
|
/* XXX: error checking */ |
195 |
|
|
196 |
/* bframes */ |
/* bframes */ |
197 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
198 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
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 |
|
|
487 |
xvid_err_memory1: |
xvid_err_memory1: |
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); |
493 |
|
} |
494 |
|
|
495 |
|
xvid_err_memory0: |
496 |
|
for (n=0; n<pEnc->num_plugins;n++) { |
497 |
|
if (pEnc->plugins[n].func) { |
498 |
|
pEnc->plugins[n].func(pEnc->plugins[n].param, XVID_PLG_DESTROY, 0, 0); |
499 |
|
} |
500 |
|
} |
501 |
|
xvid_free(pEnc->plugins); |
502 |
|
|
503 |
xvid_free(pEnc); |
xvid_free(pEnc); |
504 |
|
|
505 |
create->handle = NULL; |
create->handle = NULL; |
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); |
598 |
|
} |
599 |
|
|
600 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
601 |
|
if (pEnc->plugins[i].func) { |
602 |
|
pEnc->plugins[i].func(pEnc->plugins[i].param, XVID_PLG_DESTROY, 0, 0); |
603 |
|
} |
604 |
|
} |
605 |
|
xvid_free(pEnc->plugins); |
606 |
|
|
607 |
xvid_free(pEnc); |
xvid_free(pEnc); |
608 |
|
|
609 |
return 0; /* ok */ |
return 0; /* ok */ |
610 |
} |
} |
611 |
|
|
612 |
|
|
613 |
|
/* |
614 |
|
call the plugins |
615 |
|
*/ |
616 |
|
|
617 |
|
static void call_plugins(Encoder * pEnc, FRAMEINFO * frame, IMAGE * original, |
618 |
|
int opt, int * type, int * quant, xvid_enc_stats_t * stats) |
619 |
|
{ |
620 |
|
unsigned int i, j; |
621 |
|
xvid_plg_data_t data; |
622 |
|
|
623 |
|
/* set data struct */ |
624 |
|
|
625 |
|
memset(&data, 0, sizeof(xvid_plg_data_t)); |
626 |
|
data.version = XVID_VERSION; |
627 |
|
|
628 |
|
data.width = pEnc->mbParam.width; |
629 |
|
data.height = pEnc->mbParam.height; |
630 |
|
data.mb_width = pEnc->mbParam.mb_width; |
631 |
|
data.mb_height = pEnc->mbParam.mb_height; |
632 |
|
data.fincr = frame->fincr; |
633 |
|
data.fbase = pEnc->mbParam.fbase; |
634 |
|
|
635 |
|
data.reference.csp = XVID_CSP_USER; |
636 |
|
data.reference.plane[0] = pEnc->reference->image.y; |
637 |
|
data.reference.plane[1] = pEnc->reference->image.u; |
638 |
|
data.reference.plane[2] = pEnc->reference->image.v; |
639 |
|
data.reference.stride[0] = pEnc->mbParam.edged_width; |
640 |
|
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
641 |
|
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
642 |
|
|
643 |
|
data.current.csp = XVID_CSP_USER; |
644 |
|
data.current.plane[0] = frame->image.y; |
645 |
|
data.current.plane[1] = frame->image.u; |
646 |
|
data.current.plane[2] = frame->image.v; |
647 |
|
data.current.stride[0] = pEnc->mbParam.edged_width; |
648 |
|
data.current.stride[1] = pEnc->mbParam.edged_width/2; |
649 |
|
data.current.stride[2] = pEnc->mbParam.edged_width/2; |
650 |
|
|
651 |
|
data.frame_num = frame->frame_num; |
652 |
|
|
653 |
|
if (opt == XVID_PLG_BEFORE) { |
654 |
|
data.type = XVID_TYPE_AUTO; |
655 |
|
data.quant = 2; |
656 |
|
|
657 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
658 |
|
data.dquant = pEnc->temp_dquants; |
659 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
660 |
|
memset(data.dquant, 0, data.mb_width*data.mb_height); |
661 |
|
} |
662 |
|
|
663 |
|
/* todo: [vol,vop,motion]_flags*/ |
664 |
|
|
665 |
|
} else { // XVID_PLG_AFTER |
666 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
667 |
|
data.original.csp = XVID_CSP_USER; |
668 |
|
data.original.plane[0] = original->y; |
669 |
|
data.original.plane[1] = original->u; |
670 |
|
data.original.plane[2] = original->v; |
671 |
|
data.original.stride[0] = pEnc->mbParam.edged_width; |
672 |
|
data.original.stride[1] = pEnc->mbParam.edged_width/2; |
673 |
|
data.original.stride[2] = pEnc->mbParam.edged_width/2; |
674 |
|
} |
675 |
|
|
676 |
|
if ((frame->vol_flags & XVID_EXTRASTATS) || |
677 |
|
(pEnc->mbParam.plugin_flags & XVID_REQPSNR)) { |
678 |
|
|
679 |
|
data.sse_y = |
680 |
|
plane_sse( original->y, frame->image.y, |
681 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
682 |
|
pEnc->mbParam.height); |
683 |
|
|
684 |
|
data.sse_u = |
685 |
|
plane_sse( original->u, frame->image.u, |
686 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
687 |
|
pEnc->mbParam.height/2); |
688 |
|
|
689 |
|
data.sse_v = |
690 |
|
plane_sse( original->v, frame->image.v, |
691 |
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
692 |
|
pEnc->mbParam.height/2); |
693 |
|
} |
694 |
|
|
695 |
|
data.type = coding2type(frame->coding_type); |
696 |
|
data.quant = frame->quant; |
697 |
|
|
698 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
699 |
|
data.dquant = pEnc->temp_dquants; |
700 |
|
data.dquant_stride = pEnc->mbParam.mb_width; |
701 |
|
|
702 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
703 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
704 |
|
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
705 |
|
} |
706 |
|
} |
707 |
|
|
708 |
|
data.vol_flags = frame->vol_flags; |
709 |
|
data.vop_flags = frame->vop_flags; |
710 |
|
data.motion_flags = frame->motion_flags; |
711 |
|
|
712 |
|
data.length = frame->length; |
713 |
|
data.kblks = frame->sStat.kblks; |
714 |
|
data.mblks = frame->sStat.mblks; |
715 |
|
data.ublks = frame->sStat.ublks; |
716 |
|
|
717 |
|
if (stats) { |
718 |
|
stats->type = coding2type(frame->coding_type); |
719 |
|
stats->quant = frame->quant; |
720 |
|
stats->vol_flags = frame->vol_flags; |
721 |
|
stats->vop_flags = frame->vop_flags; |
722 |
|
stats->length = frame->length; |
723 |
|
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
724 |
|
stats->kblks = frame->sStat.kblks; |
725 |
|
stats->mblks = frame->sStat.mblks; |
726 |
|
stats->ublks = frame->sStat.ublks; |
727 |
|
stats->sse_y = data.sse_y; |
728 |
|
stats->sse_u = data.sse_u; |
729 |
|
stats->sse_v = data.sse_v; |
730 |
|
} |
731 |
|
} |
732 |
|
|
733 |
|
/* call plugins */ |
734 |
|
for (i=0; i<pEnc->num_plugins;i++) { |
735 |
|
emms(); |
736 |
|
if (pEnc->plugins[i].func) { |
737 |
|
if (pEnc->plugins[i].func(pEnc->plugins[i].param, opt, &data, 0) < 0) { |
738 |
|
continue; |
739 |
|
} |
740 |
|
} |
741 |
|
} |
742 |
|
emms(); |
743 |
|
|
744 |
|
/* copy modified values back into frame*/ |
745 |
|
if (opt == XVID_PLG_BEFORE) { |
746 |
|
*type = data.type; |
747 |
|
*quant = data.quant; |
748 |
|
|
749 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
750 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
751 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
752 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = data.dquant[j*data.mb_width + i]; |
753 |
|
} |
754 |
|
}else{ |
755 |
|
for (j=0; j<pEnc->mbParam.mb_height; j++) |
756 |
|
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
757 |
|
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
758 |
|
} |
759 |
|
} |
760 |
|
/* todo: [vol,vop,motion]_flags*/ |
761 |
|
} |
762 |
|
} |
763 |
|
|
764 |
|
|
765 |
|
|
766 |
|
|
767 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
768 |
{ |
{ |
769 |
|
pEnc->current->frame_num = pEnc->m_framenum; |
770 |
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; |
|
771 |
|
|
772 |
|
pEnc->mbParam.m_stamp += pEnc->current->fincr; |
773 |
pEnc->m_framenum++; /* debug ticker */ |
pEnc->m_framenum++; /* debug ticker */ |
774 |
} |
} |
775 |
|
|
776 |
|
static __inline void dec_frame_num(Encoder * pEnc) |
777 |
|
{ |
778 |
|
pEnc->mbParam.m_stamp -= pEnc->mbParam.fincr; |
779 |
|
pEnc->m_framenum--; /* debug ticker */ |
780 |
|
} |
781 |
|
|
782 |
|
|
783 |
|
|
784 |
static __inline void |
static __inline void |
801 |
} |
} |
802 |
|
|
803 |
|
|
|
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 ); |
|
|
} |
|
|
|
|
|
|
|
804 |
|
|
805 |
/***************************************************************************** |
/***************************************************************************** |
806 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
808 |
* Returned values : |
* Returned values : |
809 |
* - >0 - output bytes |
* - >0 - output bytes |
810 |
* - 0 - no output |
* - 0 - no output |
811 |
|
* - XVID_ERR_VERSION - wrong version passed to core |
812 |
|
* - XVID_ERR_END - End of stream reached before end of coding |
813 |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
814 |
* format |
* format |
815 |
****************************************************************************/ |
****************************************************************************/ |
816 |
|
|
817 |
|
|
|
/* XXX: dx50bvop broken! something todo with queue */ |
|
|
|
|
818 |
int |
int |
819 |
enc_encode(Encoder * pEnc, |
enc_encode(Encoder * pEnc, |
820 |
xvid_enc_frame_t * xFrame, |
xvid_enc_frame_t * xFrame, |
822 |
{ |
{ |
823 |
xvid_enc_frame_t * frame; |
xvid_enc_frame_t * frame; |
824 |
int type; |
int type; |
|
uint16_t x, y; |
|
825 |
Bitstream bs; |
Bitstream bs; |
826 |
|
|
827 |
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 */ |
884 |
|
|
885 |
if (pEnc->flush_bframes) |
if (pEnc->flush_bframes) |
886 |
{ |
{ |
887 |
if (pEnc->bframenum_head < pEnc->bframenum_tail) |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
|
{ |
|
888 |
|
|
889 |
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", |
890 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
891 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
892 |
|
|
893 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
894 |
set_stats(stats, &pEnc->rate_control, &pEnc->mbParam, pEnc->bframes[pEnc->bframenum_head]); |
image_copy(&pEnc->sOriginal2, &pEnc->bframes[pEnc->bframenum_head]->image, |
895 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
896 |
|
} |
897 |
|
|
898 |
|
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs); |
899 |
|
call_plugins(pEnc, pEnc->bframes[pEnc->bframenum_head], &pEnc->sOriginal2, XVID_PLG_AFTER, 0, 0, stats); |
900 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
901 |
|
|
|
|
|
902 |
goto done; |
goto done; |
903 |
} |
} |
904 |
|
|
|
/* flush complete: reset counters */ |
|
|
|
|
|
pEnc->flush_bframes = 0; |
|
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
|
|
|
|
905 |
/* write an empty marker to the bitstream. |
/* write an empty marker to the bitstream. |
906 |
|
|
907 |
for divx5 decoder compatibility, this marker must consist |
for divx5 decoder compatibility, this marker must consist |
909 |
indentical to the future-referece frame. |
indentical to the future-referece frame. |
910 |
*/ |
*/ |
911 |
|
|
912 |
if ((pEnc->mbParam.global_flags & XVID_PACKED)) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED && pEnc->bframenum_tail > 0)) { |
913 |
int tmp; |
int tmp; |
914 |
int bits; |
int bits; |
915 |
|
|
928 |
|
|
929 |
/* add the not-coded length to the reference frame size */ |
/* add the not-coded length to the reference frame size */ |
930 |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
pEnc->current->length += (BitstreamPos(&bs) - bits) / 8; |
931 |
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?; |
|
932 |
|
|
933 |
|
/* flush complete: reset counters */ |
934 |
|
pEnc->flush_bframes = 0; |
935 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
936 |
goto done; |
goto done; |
937 |
|
|
938 |
} |
} |
|
} |
|
939 |
|
|
940 |
|
/* flush complete: reset counters */ |
941 |
|
pEnc->flush_bframes = 0; |
942 |
|
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
943 |
|
} |
944 |
|
|
945 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
946 |
* dequeue frame from the encoding queue |
* dequeue frame from the encoding queue |
950 |
{ |
{ |
951 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
952 |
{ |
{ |
953 |
if (pEnc->bframenum_tail > 0) /* are bframes */ |
|
954 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->mbParam.max_bframes > 0) { |
955 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
956 |
|
} |
957 |
|
|
958 |
|
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
959 |
|
if (pEnc->bframenum_tail > 0) |
960 |
{ |
{ |
961 |
|
|
962 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
963 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
964 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
965 |
|
|
966 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
967 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
968 |
|
|
969 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
970 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
971 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
972 |
|
} |
973 |
|
|
974 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
975 |
|
|
976 |
goto done_flush; |
goto done_flush; |
977 |
} |
} |
978 |
|
|
993 |
|
|
994 |
|
|
995 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
996 |
* init pEnc->current field |
* init pEnc->current fields |
997 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
998 |
|
|
999 |
emms(); /* float-point region */ |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
|
|
|
1000 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
1001 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1002 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1008 |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width); |
1009 |
} |
} |
1010 |
|
|
1011 |
if (xFrame->vop_flags & XVID_EXTRASTATS) |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1012 |
{ image_copy(&pEnc->sOriginal, &pEnc->current->image, |
* frame type & quant selection |
1013 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
} |
|
|
|
|
|
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
|
|
int *temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
|
|
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
|
|
|
|
|
pEnc->current->quant = |
|
|
adaptive_quantization(pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, 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[temp_dquants[OFFSET(x, y)] + 2]; |
|
|
} |
|
1014 |
|
|
1015 |
#undef OFFSET |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
|
} |
|
1016 |
|
|
1017 |
xvid_free(temp_dquants); |
if (frame->type > 0) |
1018 |
} |
type = frame->type; |
1019 |
|
|
1020 |
if (frame->quant > 0) |
if (frame->quant > 0) |
|
{ |
|
1021 |
pEnc->current->quant = frame->quant; |
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, ...); |
|
|
} |
|
|
|
|
|
emms(); /* END floating-point region */ |
|
|
|
|
1022 |
|
|
1023 |
if (type > 0) /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
|
{ |
|
1024 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1025 |
} |
} else{ /* XVID_TYPE_AUTO */ |
1026 |
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)) |
|
|
{ |
|
1027 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1028 |
type = I_VOP; |
type = I_VOP; |
1029 |
}else{ |
}else{ |
1030 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1031 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1032 |
pEnc->iFrameNum, pEnc->bframenum_tail); |
pEnc->iFrameNum, pEnc->bframenum_tail); |
1033 |
|
|
1034 |
|
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES)) { |
1035 |
|
type = P_VOP; /* disable dynamic bframes */ |
1036 |
|
} |
1037 |
|
} |
1038 |
|
} |
1039 |
|
|
1040 |
|
/* bframes buffer overflow check */ |
1041 |
|
if (type != I_VOP) { |
1042 |
|
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1043 |
|
type = P_VOP; |
1044 |
|
}else{ |
1045 |
|
type = B_VOP; |
1046 |
} |
} |
1047 |
} |
} |
1048 |
|
|
1049 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1050 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1051 |
|
|
|
|
|
1052 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1053 |
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, |
1054 |
"%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); |
1055 |
} |
} |
1056 |
|
|
1057 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1058 |
* ivop/pvop/bvop selection |
* encode this frame as a b-vop |
1059 |
|
* (we dont encode here, rather we store the frame in the bframes queue, to be encoded later) |
1060 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1061 |
|
if (type == B_VOP) { |
1062 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1063 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1064 |
|
} |
1065 |
|
|
1066 |
if (type == I_VOP) { |
if (frame->quant < 1) { |
1067 |
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
1068 |
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
1069 |
|
|
1070 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
} else { |
1071 |
|
pEnc->current->quant = frame->quant; |
1072 |
|
} |
1073 |
|
|
1074 |
|
if (pEnc->current->quant < 1) |
1075 |
|
pEnc->current->quant = 1; |
1076 |
|
else if (pEnc->current->quant > 31) |
1077 |
|
pEnc->current->quant = 31; |
1078 |
|
|
1079 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i", |
1080 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1081 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1082 |
|
|
1083 |
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
/* store frame into bframe buffer & swap ref back to current */ |
1084 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1085 |
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1086 |
|
|
1087 |
|
pEnc->bframenum_tail++; |
1088 |
|
|
1089 |
|
goto repeat; |
1090 |
|
} |
1091 |
|
|
1092 |
|
/* for unpacked bframes, output the stats for the last encoded frame */ |
1093 |
|
if (!(pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) |
1094 |
|
{ |
1095 |
|
if (pEnc->current->stamp > 0) { |
1096 |
|
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1097 |
} |
} |
1098 |
|
else |
1099 |
|
stats->type = XVID_TYPE_NOTHING; |
1100 |
|
} |
1101 |
|
|
1102 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1103 |
|
* closed-gop |
1104 |
|
* if the frame prior to an iframe is scheduled as a bframe, we must change it to a pframe |
1105 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1106 |
|
|
1107 |
// when we reach an iframe in CLOSED_GOP mode, encode the last bframe as a xFrame |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
|
if ((pEnc->mbParam.global_flags & XVID_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
|
1108 |
|
|
1109 |
// place this frame back on the encoding-queue (head) |
// place this frame back on the encoding-queue (head) |
1110 |
// we will deal with it next time |
// we will deal with it next time |
1111 |
|
dec_frame_num(pEnc); |
1112 |
|
pEnc->iFrameNum--; |
1113 |
|
|
1114 |
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); |
1115 |
|
pEnc->queue_size++; |
1116 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1117 |
|
|
1118 |
// grab the last frame from the bframe-queue |
// grab the last frame from the bframe-queue |
1124 |
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"); |
1125 |
} |
} |
1126 |
|
|
1127 |
/* XXX: convert B-VOP to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1128 |
pEnc->current->quant *= 2; |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
1129 |
|
type = P_VOP; |
1130 |
|
} |
1131 |
|
|
|
FrameCodeP(pEnc, &bs, 1, 0); |
|
1132 |
|
|
1133 |
} else { |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1134 |
|
* encode this frame as an i-vop |
1135 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1136 |
|
|
1137 |
|
if (type == I_VOP) { |
1138 |
|
|
1139 |
|
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1140 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1141 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1142 |
|
|
1143 |
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
1144 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1145 |
|
} |
1146 |
|
|
1147 |
|
|
1148 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1149 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
1165 |
|
|
1166 |
/* ^^^------------------------ */ |
/* ^^^------------------------ */ |
1167 |
|
|
1168 |
|
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1169 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1170 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1171 |
|
} |
1172 |
|
|
1173 |
FrameCodeI(pEnc, &bs); |
FrameCodeI(pEnc, &bs); |
1174 |
xFrame->out_flags |= XVID_KEYFRAME; |
xFrame->out_flags |= XVID_KEYFRAME; |
|
} |
|
1175 |
|
|
1176 |
} else if (((pEnc->current->vop_flags & XVID_DYNAMIC_BFRAMES) && type == P_VOP) || |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1177 |
pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
* encode this frame as an p-vop |
1178 |
/* |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1179 |
* This will be coded as a Predicted Frame |
|
1180 |
*/ |
} else { // (type == P_VOP || type == S_VOP) |
1181 |
|
|
1182 |
DPRINTF(DPRINTF_DEBUG,"*** xFrame bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
1183 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1184 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1185 |
|
|
1187 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
1188 |
} |
} |
1189 |
|
|
1190 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1191 |
|
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1192 |
} else { /* type == B_VOP */ |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
|
|
|
if ((pEnc->current->vop_flags & XVID_DEBUG)) { |
|
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
|
|
} |
|
|
|
|
|
if (frame->bquant < 1) { |
|
|
pEnc->current->quant = ((((pEnc->reference->quant + pEnc->current->quant) * |
|
|
pEnc->mbParam.bquant_ratio) / 2) + pEnc->mbParam.bquant_offset)/100; |
|
|
|
|
|
} else { |
|
|
pEnc->current->quant = frame->bquant; |
|
1193 |
} |
} |
1194 |
|
|
1195 |
if (pEnc->current->quant < 1) |
FrameCodeP(pEnc, &bs, 1, 0); |
|
pEnc->current->quant = 1; |
|
|
else if (pEnc->current->quant > 31) |
|
|
pEnc->current->quant = 31; |
|
|
|
|
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
|
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
|
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
|
|
|
|
|
/* store frame into bframe buffer & swap ref back to current */ |
|
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
|
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
|
|
|
|
|
pEnc->bframenum_tail++; |
|
|
|
|
|
goto repeat; |
|
|
|
|
1196 |
} |
} |
1197 |
|
|
|
if (xFrame->vop_flags & XVID_EXTRASTATS) |
|
|
{ stats->sse_y = |
|
|
plane_sse( pEnc->sOriginal.y, pEnc->current->image.y, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
|
pEnc->mbParam.height); |
|
|
|
|
|
stats->sse_u = |
|
|
plane_sse( pEnc->sOriginal.u, pEnc->current->image.u, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
|
|
|
stats->sse_v = |
|
|
plane_sse( pEnc->sOriginal.v, pEnc->current->image.v, |
|
|
pEnc->mbParam.edged_width/2, pEnc->mbParam.width/2, |
|
|
pEnc->mbParam.height/2); |
|
|
} |
|
1198 |
|
|
1199 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1200 |
* okay, on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1201 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1202 |
|
|
1203 |
done_flush: |
done_flush: |
1204 |
|
|
|
{ |
|
|
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 */ |
|
1205 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1206 |
|
|
1207 |
|
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
1208 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global_flags & XVID_PACKED) && pEnc->bframenum_tail > 0) { |
1209 |
goto repeat; |
goto repeat; |
1210 |
} |
} |
1211 |
|
|
1212 |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) |
/* packed or no-bframes: output stats */ |
1213 |
{ |
if ((pEnc->mbParam.global_flags & XVID_PACKED) || pEnc->mbParam.max_bframes == 0) { |
1214 |
/* 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; |
|
1215 |
} |
} |
1216 |
|
|
1217 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1228 |
} |
} |
1229 |
|
|
1230 |
|
|
1231 |
|
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1232 |
|
{ |
1233 |
|
unsigned int i,j; |
1234 |
|
int quant = frame->quant; |
1235 |
|
|
1236 |
|
for (j=0; j<pParam->mb_height; j++) |
1237 |
|
for (i=0; i<pParam->mb_width; i++) { |
1238 |
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
1239 |
|
quant += pMB->dquant; |
1240 |
|
if (quant > 31) |
1241 |
|
quant = 31; |
1242 |
|
if (quant < 1) |
1243 |
|
quant = 1; |
1244 |
|
pMB->quant = quant; |
1245 |
|
} |
1246 |
|
} |
1247 |
|
|
1248 |
|
|
1249 |
static __inline void |
static __inline void |
1250 |
CodeIntraMB(Encoder * pEnc, |
CodeIntraMB(Encoder * pEnc, |
1251 |
MACROBLOCK * pMB) |
MACROBLOCK * pMB) |
1259 |
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; |
1260 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1261 |
|
|
1262 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1263 |
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; |
|
|
} |
|
1264 |
} |
} |
|
|
|
|
pMB->quant = pEnc->current->quant; |
|
1265 |
} |
} |
1266 |
|
|
1267 |
|
|
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 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1301 |
|
|
1302 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
1303 |
|
|
1304 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1423 |
|
|
1424 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1425 |
|
|
1426 |
|
|
1427 |
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1428 |
|
|
1429 |
start_timer(); |
start_timer(); |
1430 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
1431 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1539 |
|
|
1540 |
stop_comp_timer(); |
stop_comp_timer(); |
1541 |
|
|
1542 |
if ((pEnc->current->vop_flags & XVID_LUMIMASKING)) { |
if (pMB->dquant != 0) { |
|
if (pMB->dquant != NO_CHANGE) { |
|
1543 |
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; |
|
|
} |
|
1544 |
} |
} |
|
pMB->quant = pEnc->current->quant; |
|
1545 |
|
|
1546 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1547 |
|
|
1564 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1565 |
|
|
1566 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1567 |
(pMB->dquant == NO_CHANGE); |
(pMB->dquant == 0); |
1568 |
|
|
1569 |
if (pEnc->current->coding_type == S_VOP) |
if (pEnc->current->coding_type == S_VOP) |
1570 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1857 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1858 |
|
|
1859 |
mb->cbp = |
mb->cbp = |
1860 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1861 |
|
|
1862 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1863 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |