3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002-2003 Peter Ross <pross@xvid.org> |
8 |
* to use this software module in hardware or software products are |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
21 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: encoder.c,v 1.95.2.16 2003-03-27 17:09:48 edgomez Exp $ |
* $Id: encoder.c,v 1.95.2.27 2003-06-09 13:50:34 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
129 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
130 |
memset(pEnc, 0, sizeof(Encoder)); |
memset(pEnc, 0, sizeof(Encoder)); |
131 |
|
|
132 |
|
pEnc->mbParam.profile = create->profile; |
133 |
|
|
134 |
/* global flags */ |
/* global flags */ |
135 |
pEnc->mbParam.global_flags = create->global; |
pEnc->mbParam.global_flags = create->global; |
136 |
|
|
148 |
if (pEnc->mbParam.fincr>0) |
if (pEnc->mbParam.fincr>0) |
149 |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
simplify_time(&pEnc->mbParam.fincr, &pEnc->mbParam.fbase); |
150 |
|
|
151 |
/* plugin */ |
/* zones */ |
152 |
|
if(create->num_zones > 0) { |
153 |
|
pEnc->num_zones = create->num_zones; |
154 |
|
pEnc->zones = xvid_malloc(sizeof(xvid_enc_zone_t) * pEnc->num_zones, CACHE_LINE); |
155 |
|
if (pEnc->zones == NULL) |
156 |
|
goto xvid_err_memory0; |
157 |
|
memcpy(pEnc->zones, create->zones, sizeof(xvid_enc_zone_t) * pEnc->num_zones); |
158 |
|
} else { |
159 |
|
pEnc->num_zones = 0; |
160 |
|
pEnc->zones = NULL; |
161 |
|
} |
162 |
|
|
163 |
|
/* plugins */ |
164 |
|
if(create->num_plugins > 0) { |
165 |
pEnc->num_plugins = create->num_plugins; |
pEnc->num_plugins = create->num_plugins; |
166 |
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
pEnc->plugins = xvid_malloc(sizeof(xvid_enc_plugin_t) * pEnc->num_plugins, CACHE_LINE); |
167 |
if (pEnc->plugins == NULL) |
if (pEnc->plugins == NULL) |
168 |
goto xvid_err_memory0; |
goto xvid_err_memory0; |
169 |
|
} else { |
170 |
|
pEnc->num_plugins = 0; |
171 |
|
pEnc->plugins = NULL; |
172 |
|
} |
173 |
|
|
174 |
for (n=0; n<pEnc->num_plugins;n++) { |
for (n=0; n<pEnc->num_plugins;n++) { |
175 |
xvid_plg_create_t pcreate; |
xvid_plg_create_t pcreate; |
183 |
|
|
184 |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
memset(&pcreate, 0, sizeof(xvid_plg_create_t)); |
185 |
pcreate.version = XVID_VERSION; |
pcreate.version = XVID_VERSION; |
186 |
|
pcreate.num_zones = pEnc->num_zones; |
187 |
|
pcreate.zones = pEnc->zones; |
188 |
pcreate.width = pEnc->mbParam.width; |
pcreate.width = pEnc->mbParam.width; |
189 |
pcreate.height = pEnc->mbParam.height; |
pcreate.height = pEnc->mbParam.height; |
190 |
pcreate.fincr = pEnc->mbParam.fincr; |
pcreate.fincr = pEnc->mbParam.fincr; |
214 |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
pEnc->mbParam.bquant_ratio = MAX(create->bquant_ratio, 0); |
215 |
pEnc->mbParam.bquant_offset = create->bquant_offset; |
pEnc->mbParam.bquant_offset = create->bquant_offset; |
216 |
|
|
217 |
|
/* min/max quant */ |
218 |
|
for (n=0; n<3; n++) { |
219 |
|
pEnc->mbParam.min_quant[n] = create->min_quant[n] > 0 ? create->min_quant[n] : 2; |
220 |
|
pEnc->mbParam.max_quant[n] = create->max_quant[n] > 0 ? create->max_quant[n] : 31; |
221 |
|
} |
222 |
|
|
223 |
/* frame drop ratio */ |
/* frame drop ratio */ |
224 |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
225 |
|
|
226 |
/* max keyframe interval */ |
/* max keyframe interval */ |
227 |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <=0 ? 250 : create->max_key_interval; |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
228 |
/*XXX: replace 250 hard code with "10seconds * framerate" */ |
10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval; |
229 |
|
|
230 |
/* Bitrate allocator defaults |
/* Bitrate allocator defaults |
231 |
|
|
522 |
} |
} |
523 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
524 |
|
|
525 |
|
xvid_free(pEnc->zones); |
526 |
|
|
527 |
xvid_free(pEnc); |
xvid_free(pEnc); |
528 |
|
|
529 |
create->handle = NULL; |
create->handle = NULL; |
601 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
602 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
603 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
604 |
|
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
605 |
|
pEnc->mbParam.edged_height); |
606 |
|
|
607 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
608 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
624 |
} |
} |
625 |
|
|
626 |
|
|
627 |
if (pEnc->num_plugins>0) |
if (pEnc->num_plugins>0) { |
|
{ |
|
628 |
xvid_plg_destroy_t pdestroy; |
xvid_plg_destroy_t pdestroy; |
629 |
memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); |
memset(&pdestroy, 0, sizeof(xvid_plg_destroy_t)); |
630 |
|
|
639 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
640 |
} |
} |
641 |
|
|
642 |
|
if (pEnc->num_plugins>0) |
643 |
|
xvid_free(pEnc->zones); |
644 |
|
|
645 |
xvid_free(pEnc); |
xvid_free(pEnc); |
646 |
|
|
647 |
return 0; /* ok */ |
return 0; /* ok */ |
663 |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
memset(&data, 0, sizeof(xvid_plg_data_t)); |
664 |
data.version = XVID_VERSION; |
data.version = XVID_VERSION; |
665 |
|
|
666 |
|
/* find zone */ |
667 |
|
for(i=0; i<pEnc->num_zones && pEnc->zones[i].frame<=frame->frame_num; i++) ; |
668 |
|
data.zone = i>0 ? &pEnc->zones[i-1] : NULL; |
669 |
|
|
670 |
data.width = pEnc->mbParam.width; |
data.width = pEnc->mbParam.width; |
671 |
data.height = pEnc->mbParam.height; |
data.height = pEnc->mbParam.height; |
672 |
data.mb_width = pEnc->mbParam.mb_width; |
data.mb_width = pEnc->mbParam.mb_width; |
674 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
675 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
676 |
|
|
677 |
|
for (i=0; i<3; i++) { |
678 |
|
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
679 |
|
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
680 |
|
} |
681 |
|
|
682 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_USER; |
683 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
684 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
699 |
|
|
700 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
701 |
data.type = XVID_TYPE_AUTO; |
data.type = XVID_TYPE_AUTO; |
702 |
data.quant = 2; |
data.quant = 0; |
703 |
|
|
704 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
705 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
709 |
|
|
710 |
/* todo: [vol,vop,motion]_flags*/ |
/* todo: [vol,vop,motion]_flags*/ |
711 |
|
|
712 |
} else { // XVID_PLG_AFTER |
} else { /* XVID_PLG_AFTER */ |
713 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
714 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_USER; |
715 |
data.original.plane[0] = original->y; |
data.original.plane[0] = original->y; |
791 |
/* copy modified values back into frame*/ |
/* copy modified values back into frame*/ |
792 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
793 |
*type = data.type; |
*type = data.type; |
794 |
*quant = data.quant; |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
795 |
|
|
796 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
797 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
933 |
{ |
{ |
934 |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
if (pEnc->bframenum_head < pEnc->bframenum_tail) { |
935 |
|
|
936 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
937 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
938 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
939 |
|
|
960 |
int tmp; |
int tmp; |
961 |
int bits; |
int bits; |
962 |
|
|
963 |
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
964 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
965 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
966 |
|
|
998 |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
if (xFrame->input.csp == XVID_CSP_NULL) /* no futher input */ |
999 |
{ |
{ |
1000 |
|
|
1001 |
|
DPRINTF(XVID_DEBUG_DEBUG,"*** FINISH bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1002 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1003 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1004 |
|
|
1005 |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) { |
1006 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1007 |
} |
} |
1008 |
|
|
1009 |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
/* if the very last frame is to be b-vop, we must change it to a p-vop */ |
1010 |
if (pEnc->bframenum_tail > 0) |
if (pEnc->bframenum_tail > 0) { |
|
{ |
|
1011 |
|
|
1012 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1013 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1021 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1022 |
} |
} |
1023 |
|
|
1024 |
|
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1025 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1026 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1027 |
|
|
1028 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
1029 |
|
|
1030 |
goto done_flush; |
|
1031 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1032 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1033 |
|
}else{ |
1034 |
|
pEnc->flush_bframes = 1; |
1035 |
|
goto done; |
1036 |
|
} |
1037 |
} |
} |
1038 |
|
DPRINTF(XVID_DEBUG_DEBUG, "*** END\n"); |
1039 |
|
|
1040 |
emms(); |
emms(); |
1041 |
return XVID_ERR_END; /* end of stream reached */ |
return XVID_ERR_END; /* end of stream reached */ |
1043 |
goto done; /* nothing to encode yet; encoder lag */ |
goto done; /* nothing to encode yet; encoder lag */ |
1044 |
} |
} |
1045 |
|
|
1046 |
// the current FRAME becomes the reference |
/* the current FRAME becomes the reference */ |
1047 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1048 |
|
|
1049 |
// remove frame from encoding-queue (head), and move it into the current |
/* remove frame from encoding-queue (head), and move it into the current */ |
1050 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1051 |
frame = &pEnc->queue[pEnc->queue_head].frame; |
frame = &pEnc->queue[pEnc->queue_head].frame; |
1052 |
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
1075 |
* frame type & quant selection |
* frame type & quant selection |
1076 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1077 |
|
|
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
|
|
|
|
|
if (frame->type > 0) |
|
1078 |
type = frame->type; |
type = frame->type; |
|
|
|
|
if (frame->quant > 0) |
|
1079 |
pEnc->current->quant = frame->quant; |
pEnc->current->quant = frame->quant; |
1080 |
|
|
1081 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_BEFORE, &type, &pEnc->current->quant, stats); |
1082 |
|
|
1083 |
if (type > 0){ /* XVID_TYPE_?VOP */ |
if (type > 0){ /* XVID_TYPE_?VOP */ |
1084 |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
type = type2coding(type); /* convert XVID_TYPE_?VOP to bitstream coding type */ |
1085 |
} else{ /* XVID_TYPE_AUTO */ |
} else{ /* XVID_TYPE_AUTO */ |
1090 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1091 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1092 |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
|
|
|
|
if (type == B_VOP && !(pEnc->current->vop_flags & XVID_VOP_DYNAMIC_BFRAMES)) { |
|
|
type = P_VOP; /* disable dynamic bframes */ |
|
|
} |
|
1093 |
} |
} |
1094 |
} |
} |
1095 |
|
|
1096 |
/* bframes buffer overflow check */ |
/* bframes buffer overflow check */ |
1097 |
if (type != I_VOP) { |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
|
if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
|
1098 |
type = P_VOP; |
type = P_VOP; |
|
}else{ |
|
|
type = B_VOP; |
|
|
} |
|
1099 |
} |
} |
1100 |
|
|
1101 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1127 |
else if (pEnc->current->quant > 31) |
else if (pEnc->current->quant > 31) |
1128 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
1129 |
|
|
1130 |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1131 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1132 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1133 |
|
|
1140 |
goto repeat; |
goto repeat; |
1141 |
} |
} |
1142 |
|
|
1143 |
|
|
1144 |
|
DPRINTF(XVID_DEBUG_DEBUG,"*** XXXXXX bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1145 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1146 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1147 |
|
|
1148 |
/* for unpacked bframes, output the stats for the last encoded frame */ |
/* for unpacked bframes, output the stats for the last encoded frame */ |
1149 |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
if (!(pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->mbParam.max_bframes > 0) |
1150 |
{ |
{ |
1151 |
if (pEnc->current->stamp > 0) { |
if (pEnc->current->stamp > 0) { |
1152 |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->reference, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1162 |
|
|
1163 |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1164 |
|
|
1165 |
// place this frame back on the encoding-queue (head) |
/* place this frame back on the encoding-queue (head) */ |
1166 |
// we will deal with it next time |
/* we will deal with it next time */ |
1167 |
dec_frame_num(pEnc); |
dec_frame_num(pEnc); |
1168 |
pEnc->iFrameNum--; |
pEnc->iFrameNum--; |
1169 |
|
|
1171 |
pEnc->queue_size++; |
pEnc->queue_size++; |
1172 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1173 |
|
|
1174 |
// grab the last frame from the bframe-queue |
/* grab the last frame from the bframe-queue */ |
1175 |
|
|
1176 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1177 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1192 |
|
|
1193 |
if (type == I_VOP) { |
if (type == I_VOP) { |
1194 |
|
|
1195 |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1196 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1197 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1198 |
|
|
1233 |
* encode this frame as an p-vop |
* encode this frame as an p-vop |
1234 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1235 |
|
|
1236 |
} else { // (type == P_VOP || type == S_VOP) |
} else { /* (type == P_VOP || type == S_VOP) */ |
1237 |
|
|
1238 |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1239 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1240 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1241 |
|
|
1260 |
|
|
1261 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1262 |
|
|
1263 |
/* packed & queued_bframes: dont bother outputting stats, we do so after the flush */ |
/* packed & queued_bframes: dont bother outputting stats here, we do so after the flush */ |
1264 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
1265 |
goto repeat; |
goto repeat; |
1266 |
} |
} |
1267 |
|
|
1268 |
/* packed or no-bframes: output stats */ |
/* packed or no-bframes or no-bframes-queued: output stats */ |
1269 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) || pEnc->mbParam.max_bframes == 0) { |
1270 |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1271 |
} |
} |
1398 |
} |
} |
1399 |
emms(); |
emms(); |
1400 |
|
|
1401 |
|
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
1402 |
|
#if 0 |
1403 |
/* 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 */ |
1404 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1405 |
|
#endif |
1406 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1407 |
|
#if 0 |
1408 |
else |
else |
1409 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1410 |
|
#endif |
1411 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1412 |
|
|
1413 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1464 |
|
|
1465 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1466 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1467 |
//pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
/* pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; */ |
1468 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1469 |
|
|
1470 |
if (!force_inter) |
if (!force_inter) |
1694 |
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); |
1695 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1696 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1697 |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1698 |
} else { |
} else { |
1699 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1700 |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1701 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1702 |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[0].x, pMB->pmvs[0].y, predMV.x, predMV.y, pMB->mvs[0].x, pMB->mvs[0].y); |
1703 |
} |
} |
1704 |
|
|
1705 |
|
|
1711 |
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); |
1712 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1713 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1714 |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1715 |
} else { |
} else { |
1716 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
1717 |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1718 |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1719 |
DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
DPRINTF(XVID_DEBUG_MV,"mv_diff (%i,%i) pred (%i,%i) result (%i,%i)\n", pMB->pmvs[k].x, pMB->pmvs[k].y, predMV.x, predMV.y, pMB->mvs[k].x, pMB->mvs[k].y); |
1720 |
} |
} |
1721 |
|
|
1722 |
} |
} |
1766 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1767 |
|
|
1768 |
/* frame drop code */ |
/* frame drop code */ |
1769 |
DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
#if 0 |
1770 |
|
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
1771 |
|
#endif |
1772 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
1773 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
1774 |
{ |
{ |
1784 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
1785 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1786 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1787 |
//pEnc->current->quarterpel = pEnc->reference->quarterpel; |
/* pEnc->current->quarterpel = pEnc->reference->quarterpel; */ |
1788 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
1789 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
1790 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1806 |
} |
} |
1807 |
*/ |
*/ |
1808 |
|
|
1809 |
|
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
1810 |
|
#if 0 |
1811 |
/* 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 */ |
1812 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
1813 |
|
#endif |
1814 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); |
1815 |
|
#if 0 |
1816 |
else |
else |
1817 |
BitstreamPad(bs); |
BitstreamPad(bs); |
1818 |
|
#endif |
1819 |
|
|
1820 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1821 |
|
|
1850 |
} |
} |
1851 |
#endif |
#endif |
1852 |
|
|
1853 |
//frame->quarterpel = pEnc->mbParam.m_quarterpel; |
/* frame->quarterpel = pEnc->mbParam.m_quarterpel; */ |
1854 |
|
|
1855 |
/* forward */ |
/* forward */ |
1856 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1947 |
|
|
1948 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
1949 |
|
|
1950 |
BitstreamPad(bs); |
BitstreamPadAlways(bs); |
1951 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1952 |
|
|
1953 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |