38 |
#include "image/font.h" |
#include "image/font.h" |
39 |
#include "motion/sad.h" |
#include "motion/sad.h" |
40 |
#include "motion/motion.h" |
#include "motion/motion.h" |
41 |
|
#include "motion/gmc.h" |
42 |
|
|
43 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
44 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
45 |
#include "bitstream/bitstream.h" |
#include "bitstream/bitstream.h" |
117 |
Encoder *pEnc; |
Encoder *pEnc; |
118 |
int n; |
int n; |
119 |
|
|
120 |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
121 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
122 |
|
|
123 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
124 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
125 |
|
|
126 |
|
if (create->width<=0 || create->height<=0) |
127 |
|
return XVID_ERR_FAIL; |
128 |
|
|
129 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
130 |
|
|
131 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
191 |
pcreate.zones = pEnc->zones; |
pcreate.zones = pEnc->zones; |
192 |
pcreate.width = pEnc->mbParam.width; |
pcreate.width = pEnc->mbParam.width; |
193 |
pcreate.height = pEnc->mbParam.height; |
pcreate.height = pEnc->mbParam.height; |
194 |
|
pcreate.mb_width = pEnc->mbParam.mb_width; |
195 |
|
pcreate.mb_height = pEnc->mbParam.mb_height; |
196 |
pcreate.fincr = pEnc->mbParam.fincr; |
pcreate.fincr = pEnc->mbParam.fincr; |
197 |
pcreate.fbase = pEnc->mbParam.fbase; |
pcreate.fbase = pEnc->mbParam.fbase; |
198 |
pcreate.param = create->plugins[n].param; |
pcreate.param = create->plugins[n].param; |
212 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
213 |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
214 |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
215 |
|
if (pEnc->temp_dquants==NULL) |
216 |
|
goto xvid_err_memory1a; |
217 |
} |
} |
|
/* XXX: error checking */ |
|
218 |
|
|
219 |
/* bframes */ |
/* bframes */ |
220 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
231 |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
232 |
|
|
233 |
/* max keyframe interval */ |
/* max keyframe interval */ |
234 |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? (10 * (int)pEnc->mbParam.fbase) / (int)pEnc->mbParam.fincr : create->max_key_interval; |
|
10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval; |
|
|
|
|
|
/* Bitrate allocator defaults |
|
|
|
|
|
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
|
|
create->min_quantizer = 1; |
|
|
|
|
|
if ((create->max_quantizer <= 0) || (create->max_quantizer > 31)) |
|
|
create->max_quantizer = 31; |
|
|
|
|
|
if (create->max_quantizer < create->min_quantizer) |
|
|
create->max_quantizer = create->min_quantizer; */ |
|
|
|
|
235 |
|
|
236 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
237 |
|
|
253 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
254 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
255 |
|
|
256 |
|
/* allocate quant matrix memory */ |
257 |
|
|
258 |
|
pEnc->mbParam.mpeg_quant_matrices = |
259 |
|
xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
260 |
|
|
261 |
|
if (pEnc->mbParam.mpeg_quant_matrices == NULL) |
262 |
|
goto xvid_err_memory2a; |
263 |
|
|
264 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
265 |
|
|
266 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
276 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
277 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
278 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
279 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
280 |
|
|
281 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
282 |
if (image_create |
if (image_create |
320 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
321 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
322 |
if (image_create |
if (image_create |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
|
if (image_create |
|
323 |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
324 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
325 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
|
if (image_create |
|
|
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
326 |
|
|
327 |
/* Create full bitplane for GMC, this might be wasteful */ |
/* Create full bitplane for GMC, this might be wasteful */ |
328 |
if (image_create |
if (image_create |
392 |
image_null(&pEnc->queue[n].image); |
image_null(&pEnc->queue[n].image); |
393 |
|
|
394 |
|
|
395 |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
|
{ |
|
396 |
if (image_create |
if (image_create |
397 |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
398 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
399 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
|
|
|
400 |
} |
} |
401 |
|
|
|
|
|
402 |
/* timestamp stuff */ |
/* timestamp stuff */ |
403 |
|
|
404 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
414 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
415 |
|
|
416 |
init_timer(); |
init_timer(); |
417 |
|
init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); |
418 |
|
|
419 |
return 0; /* ok */ |
return 0; /* ok */ |
420 |
|
|
424 |
|
|
425 |
xvid_err_memory5: |
xvid_err_memory5: |
426 |
|
|
427 |
if (pEnc->mbParam.max_bframes > 0) { |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
428 |
int i; |
image_destroy(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
|
|
|
|
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
|
429 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
430 |
} |
} |
431 |
|
|
432 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
|
} |
|
433 |
|
|
434 |
xvid_err_memory4: |
xvid_err_memory4: |
435 |
|
|
443 |
|
|
444 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
445 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
446 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
447 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
448 |
} |
} |
449 |
|
|
450 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
474 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
475 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
476 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
477 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
478 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
479 |
|
|
480 |
/* destroy GMC image */ |
/* destroy GMC image */ |
481 |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
482 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
483 |
|
|
484 |
|
xvid_err_memory2a: |
485 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
486 |
|
|
487 |
xvid_err_memory2: |
xvid_err_memory2: |
488 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
492 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
493 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
494 |
|
|
495 |
|
xvid_err_memory1a: |
496 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
497 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
498 |
} |
} |
531 |
int i; |
int i; |
532 |
|
|
533 |
/* B Frames specific */ |
/* B Frames specific */ |
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
|
|
534 |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
|
535 |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
536 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
537 |
} |
} |
|
xvid_free(pEnc->queue); |
|
|
} |
|
538 |
|
|
539 |
|
xvid_free(pEnc->queue); |
540 |
|
|
541 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
542 |
|
|
547 |
|
|
548 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
549 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
550 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
551 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
552 |
} |
} |
553 |
|
|
565 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
566 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
567 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
568 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
569 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
|
|
|
570 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
571 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
572 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
611 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
612 |
} |
} |
613 |
|
|
614 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
615 |
|
|
616 |
if (pEnc->num_plugins>0) |
if (pEnc->num_plugins>0) |
617 |
xvid_free(pEnc->zones); |
xvid_free(pEnc->zones); |
618 |
|
|
647 |
data.mb_height = pEnc->mbParam.mb_height; |
data.mb_height = pEnc->mbParam.mb_height; |
648 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
649 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
650 |
|
data.bquant_ratio = pEnc->mbParam.bquant_ratio; |
651 |
|
data.bquant_offset = pEnc->mbParam.bquant_offset; |
652 |
|
|
653 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
654 |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
655 |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
656 |
} |
} |
657 |
|
|
658 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_PLANAR; |
659 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
660 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
661 |
data.reference.plane[2] = pEnc->reference->image.v; |
data.reference.plane[2] = pEnc->reference->image.v; |
663 |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
664 |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
665 |
|
|
666 |
data.current.csp = XVID_CSP_USER; |
data.current.csp = XVID_CSP_PLANAR; |
667 |
data.current.plane[0] = frame->image.y; |
data.current.plane[0] = frame->image.y; |
668 |
data.current.plane[1] = frame->image.u; |
data.current.plane[1] = frame->image.u; |
669 |
data.current.plane[2] = frame->image.v; |
data.current.plane[2] = frame->image.v; |
674 |
data.frame_num = frame->frame_num; |
data.frame_num = frame->frame_num; |
675 |
|
|
676 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
677 |
data.type = XVID_TYPE_AUTO; |
data.type = *type; |
678 |
data.quant = 0; |
data.quant = *quant; |
679 |
|
|
680 |
|
data.vol_flags = frame->vol_flags; |
681 |
|
data.vop_flags = frame->vop_flags; |
682 |
|
data.motion_flags = frame->motion_flags; |
683 |
|
|
684 |
|
} else if (opt == XVID_PLG_FRAME) { |
685 |
|
data.type = coding2type(frame->coding_type); |
686 |
|
data.quant = frame->quant; |
687 |
|
|
688 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
689 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
691 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
692 |
} |
} |
693 |
|
|
|
/* todo: [vol,vop,motion]_flags*/ |
|
|
|
|
694 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
695 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
696 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_PLANAR; |
697 |
data.original.plane[0] = original->y; |
data.original.plane[0] = original->y; |
698 |
data.original.plane[1] = original->u; |
data.original.plane[1] = original->u; |
699 |
data.original.plane[2] = original->v; |
data.original.plane[2] = original->v; |
730 |
|
|
731 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
732 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
733 |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant;; |
data.dquant[j*data.dquant_stride + i] = frame->mbs[j*pEnc->mbParam.mb_width + i].dquant; |
734 |
} |
} |
735 |
} |
} |
736 |
|
|
743 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
744 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
745 |
|
|
746 |
if (stats) { |
/* New code */ |
747 |
stats->type = coding2type(frame->coding_type); |
data.stats.type = coding2type(frame->coding_type); |
748 |
stats->quant = frame->quant; |
data.stats.quant = frame->quant; |
749 |
stats->vol_flags = frame->vol_flags; |
data.stats.vol_flags = frame->vol_flags; |
750 |
stats->vop_flags = frame->vop_flags; |
data.stats.vop_flags = frame->vop_flags; |
751 |
stats->length = frame->length; |
data.stats.length = frame->length; |
752 |
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); |
753 |
stats->kblks = frame->sStat.kblks; |
data.stats.kblks = frame->sStat.kblks; |
754 |
stats->mblks = frame->sStat.mblks; |
data.stats.mblks = frame->sStat.mblks; |
755 |
stats->ublks = frame->sStat.ublks; |
data.stats.ublks = frame->sStat.ublks; |
756 |
stats->sse_y = data.sse_y; |
data.stats.sse_y = data.sse_y; |
757 |
stats->sse_u = data.sse_u; |
data.stats.sse_u = data.sse_u; |
758 |
stats->sse_v = data.sse_v; |
data.stats.sse_v = data.sse_v; |
759 |
} |
|
760 |
|
if (stats) |
761 |
|
*stats = data.stats; |
762 |
} |
} |
763 |
|
|
764 |
/* call plugins */ |
/* call plugins */ |
765 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
766 |
emms(); |
emms(); |
767 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
768 |
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) { |
777 |
*type = data.type; |
*type = data.type; |
778 |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
779 |
|
|
780 |
|
frame->vol_flags = data.vol_flags; |
781 |
|
frame->vop_flags = data.vop_flags; |
782 |
|
frame->motion_flags = data.motion_flags; |
783 |
|
|
784 |
|
} else if (opt == XVID_PLG_FRAME) { |
785 |
|
|
786 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
787 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
788 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
794 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
795 |
} |
} |
796 |
} |
} |
797 |
/* todo: [vol,vop,motion]_flags*/ |
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
|
} |
|
798 |
} |
} |
799 |
|
|
800 |
|
|
801 |
|
} |
802 |
|
|
803 |
|
|
804 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
816 |
pEnc->m_framenum--; /* debug ticker */ |
pEnc->m_framenum--; /* debug ticker */ |
817 |
} |
} |
818 |
|
|
819 |
|
static __inline void |
820 |
|
MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) |
821 |
|
{ |
822 |
|
if (pMB->cbp == 0) { |
823 |
|
/* we want to code dquant but the quantizer value will not be used yet |
824 |
|
let's find out if we can postpone dquant to next MB |
825 |
|
*/ |
826 |
|
if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { |
827 |
|
pMB->dquant = 0; /* it's the last MB of all, the easiest case */ |
828 |
|
return; |
829 |
|
} else { |
830 |
|
MACROBLOCK * next = pMB + 1; |
831 |
|
const MACROBLOCK * prev = pMB - 1; |
832 |
|
if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) |
833 |
|
/* mode allows dquant change in the future */ |
834 |
|
if (abs(next->quant - prev->quant) <= 2) { |
835 |
|
/* quant change is not out of range */ |
836 |
|
pMB->quant = prev->quant; |
837 |
|
pMB->dquant = 0; |
838 |
|
next->dquant = next->quant - prev->quant; |
839 |
|
return; |
840 |
|
} |
841 |
|
} |
842 |
|
} |
843 |
|
/* couldn't skip this dquant */ |
844 |
|
pMB->mode = MODE_INTER_Q; |
845 |
|
} |
846 |
|
|
847 |
|
|
848 |
|
|
849 |
static __inline void |
static __inline void |
853 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
854 |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
855 |
|
|
856 |
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
#if 0 /* HEAVY DEBUG OUTPUT */ |
857 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
|
858 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
859 |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
860 |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
861 |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
862 |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
863 |
|
#endif |
864 |
|
} |
865 |
|
|
866 |
*/ |
static int |
867 |
|
gcd(int a, int b) |
868 |
|
{ |
869 |
|
int r ; |
870 |
|
|
871 |
|
if (b > a) { |
872 |
|
r = a; |
873 |
|
a = b; |
874 |
|
b = r; |
875 |
|
} |
876 |
|
|
877 |
|
while ((r = a % b)) { |
878 |
|
a = b; |
879 |
|
b = r; |
880 |
|
} |
881 |
|
return b; |
882 |
|
} |
883 |
|
|
884 |
|
static void |
885 |
|
simplify_par(int *par_width, int *par_height) |
886 |
|
{ |
887 |
|
|
888 |
|
int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
889 |
|
int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; |
890 |
|
int divisor = gcd(_par_width, _par_height); |
891 |
|
|
892 |
|
_par_width /= divisor; |
893 |
|
_par_height /= divisor; |
894 |
|
|
895 |
|
/* 2^8 precision maximum */ |
896 |
|
if (_par_width>255 || _par_height>255) { |
897 |
|
float div; |
898 |
|
emms(); |
899 |
|
if (_par_width>_par_height) |
900 |
|
div = (float)_par_width/255; |
901 |
|
else |
902 |
|
div = (float)_par_height/255; |
903 |
|
|
904 |
|
_par_width = (int)((float)_par_width/div); |
905 |
|
_par_height = (int)((float)_par_height/div); |
906 |
} |
} |
907 |
|
|
908 |
|
*par_width = _par_width; |
909 |
|
*par_height = _par_height; |
910 |
|
|
911 |
|
return; |
912 |
|
} |
913 |
|
|
914 |
|
|
915 |
/***************************************************************************** |
/***************************************************************************** |
934 |
int type; |
int type; |
935 |
Bitstream bs; |
Bitstream bs; |
936 |
|
|
937 |
if (XVID_MAJOR(xFrame->version) != 1 || (stats && XVID_MAJOR(stats->version) != 1)) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(xFrame->version) != 1 || (stats && XVID_VERSION_MAJOR(stats->version) != 1)) /* v1.x.x */ |
938 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
939 |
|
|
940 |
xFrame->out_flags = 0; |
xFrame->out_flags = 0; |
971 |
|
|
972 |
if (xFrame->quant_intra_matrix) |
if (xFrame->quant_intra_matrix) |
973 |
{ |
{ |
974 |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, sizeof(xFrame->quant_intra_matrix)); |
memcpy(q->quant_intra_matrix, xFrame->quant_intra_matrix, 64*sizeof(unsigned char)); |
975 |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
976 |
} |
} |
977 |
|
|
978 |
if (xFrame->quant_inter_matrix) |
if (xFrame->quant_inter_matrix) |
979 |
{ |
{ |
980 |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, sizeof(xFrame->quant_inter_matrix)); |
memcpy(q->quant_inter_matrix, xFrame->quant_inter_matrix, 64*sizeof(unsigned char)); |
981 |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
982 |
} |
} |
983 |
|
|
1032 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
1033 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
1034 |
|
|
1035 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
1036 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1037 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
1038 |
|
|
1077 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1078 |
|
|
1079 |
/* convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
1080 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1081 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1082 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1083 |
|
|
1084 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1085 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1089 |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1090 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1091 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1092 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1093 |
|
|
1094 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs, 1, 0); |
1095 |
|
|
1125 |
|
|
1126 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1127 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1128 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = frame->vol_flags; |
1129 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1130 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1131 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1155 |
}else{ |
}else{ |
1156 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1157 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1158 |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold, |
1159 |
|
(pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL); |
1160 |
} |
} |
1161 |
} |
} |
1162 |
|
|
1163 |
|
if (type != I_VOP) |
1164 |
|
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ |
1165 |
|
|
1166 |
/* bframes buffer overflow check */ |
/* bframes buffer overflow check */ |
1167 |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1168 |
type = P_VOP; |
type = P_VOP; |
1172 |
|
|
1173 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1174 |
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, |
1175 |
"%i st:%i if:%i", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
"%d st:%lld if:%d", pEnc->current->frame_num, pEnc->current->stamp, pEnc->iFrameNum); |
1176 |
} |
} |
1177 |
|
|
1178 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1251 |
} |
} |
1252 |
|
|
1253 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1254 |
pEnc->current->quant = ((pEnc->current->quant*100) - pEnc->mbParam.bquant_offset) / pEnc->mbParam.bquant_ratio; |
pEnc->current->quant = 100*pEnc->current->quant - pEnc->mbParam.bquant_offset; |
1255 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1256 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1257 |
type = P_VOP; |
type = P_VOP; |
1258 |
} |
} |
1259 |
|
|
1272 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
1273 |
} |
} |
1274 |
|
|
1275 |
|
pEnc->iFrameNum = 1; |
1276 |
|
|
1277 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1278 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->mbParam.vol_flags = pEnc->current->vol_flags; |
1279 |
|
|
1280 |
|
/* Aspect ratio */ |
1281 |
|
switch(frame->par) { |
1282 |
|
case XVID_PAR_11_VGA: |
1283 |
|
case XVID_PAR_43_PAL: |
1284 |
|
case XVID_PAR_43_NTSC: |
1285 |
|
case XVID_PAR_169_PAL: |
1286 |
|
case XVID_PAR_169_NTSC: |
1287 |
|
case XVID_PAR_EXT: |
1288 |
|
pEnc->mbParam.par = frame->par; |
1289 |
|
break; |
1290 |
|
default: |
1291 |
|
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1292 |
|
break; |
1293 |
|
} |
1294 |
|
|
1295 |
|
/* For extended PAR only, we try to sanityse/simplify par values */ |
1296 |
|
if (pEnc->mbParam.par == XVID_PAR_EXT) { |
1297 |
|
pEnc->mbParam.par_width = frame->par_width; |
1298 |
|
pEnc->mbParam.par_height = frame->par_height; |
1299 |
|
simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); |
1300 |
|
} |
1301 |
|
|
1302 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1303 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1304 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); |
1305 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1306 |
set_inter_matrix(frame->quant_inter_matrix); |
set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); |
1307 |
} |
} |
1308 |
|
|
1309 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1343 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1344 |
} |
} |
1345 |
|
|
1346 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs, 1, 0) == 0 ) { |
1347 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1348 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1349 |
|
goto done; |
1350 |
|
} |
1351 |
} |
} |
1352 |
|
|
1353 |
|
|
1355 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1356 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1357 |
|
|
1358 |
done_flush: |
/*done_flush:*/ |
1359 |
|
|
1360 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1361 |
|
|
1385 |
|
|
1386 |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1387 |
{ |
{ |
1388 |
unsigned int i,j; |
unsigned int i; |
1389 |
int quant = frame->quant; |
MACROBLOCK * pMB = frame->mbs; |
1390 |
|
int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ |
1391 |
|
if (quant > 31) |
1392 |
|
frame->quant = quant = 31; |
1393 |
|
else if (quant < 1) |
1394 |
|
frame->quant = quant = 1; |
1395 |
|
|
1396 |
for (j=0; j<pParam->mb_height; j++) |
for (i = 0; i < pParam->mb_height * pParam->mb_width; i++) { |
|
for (i=0; i<pParam->mb_width; i++) { |
|
|
MACROBLOCK * pMB = &frame->mbs[j*pParam->mb_width + i]; |
|
1397 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1398 |
if (quant > 31) |
if (quant > 31) |
1399 |
quant = 31; |
quant = 31; |
1400 |
if (quant < 1) |
else if (quant < 1) |
1401 |
quant = 1; |
quant = 1; |
1402 |
pMB->quant = quant; |
pMB->quant = quant; |
1403 |
|
pMB++; |
1404 |
} |
} |
1405 |
} |
} |
1406 |
|
|
1448 |
start_timer(); |
start_timer(); |
1449 |
image_setedges(&pEnc->current->image, |
image_setedges(&pEnc->current->image, |
1450 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1451 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height, 0); |
1452 |
stop_edges_timer(); |
stop_edges_timer(); |
1453 |
} |
} |
1454 |
|
|
1456 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1457 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1458 |
|
|
1459 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1460 |
|
|
1461 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1462 |
|
|
1463 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1464 |
|
|
1465 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1466 |
|
|
1467 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1468 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
|
1469 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1470 |
|
|
1471 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1472 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1504 |
} |
} |
1505 |
emms(); |
emms(); |
1506 |
|
|
1507 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
1508 |
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1509 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1510 |
|
|
1511 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1512 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1513 |
|
|
1514 |
/* XXX: hinted me |
pEnc->current->is_edged = 0; /* not edged */ |
1515 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
|
HintedMEGet(pEnc, 1); |
|
|
}*/ |
|
1516 |
|
|
1517 |
return 1; /* intra */ |
return 1; /* intra */ |
1518 |
} |
} |
1535 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1536 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1537 |
|
|
|
int mb_width = pEnc->mbParam.mb_width; |
|
|
int mb_height = pEnc->mbParam.mb_height; |
|
|
|
|
1538 |
int iLimit; |
int iLimit; |
1539 |
int x, y, k; |
int x, y, k; |
1540 |
int iSearchRange; |
int iSearchRange; |
1541 |
int bIntra, skip_possible; |
int bIntra=0, skip_possible; |
1542 |
|
FRAMEINFO *const current = pEnc->current; |
1543 |
|
FRAMEINFO *const reference = pEnc->reference; |
1544 |
|
MBParam * const pParam = &pEnc->mbParam; |
1545 |
|
int mb_width = pParam->mb_width; |
1546 |
|
int mb_height = pParam->mb_height; |
1547 |
|
int coded = 1; |
1548 |
|
|
|
/* IMAGE *pCurrent = &pEnc->current->image; */ |
|
|
IMAGE *pRef = &pEnc->reference->image; |
|
1549 |
|
|
1550 |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
/* IMAGE *pCurrent = ¤t->image; */ |
1551 |
|
IMAGE *pRef = &reference->image; |
1552 |
|
|
1553 |
|
if ((current->vop_flags & XVID_VOP_REDUCED)) |
1554 |
{ |
{ |
1555 |
mb_width = (pEnc->mbParam.width + 31) / 32; |
mb_width = (pParam->width + 31) / 32; |
1556 |
mb_height = (pEnc->mbParam.height + 31) / 32; |
mb_height = (pParam->height + 31) / 32; |
1557 |
} |
} |
1558 |
|
|
1559 |
|
|
1560 |
|
if (!reference->is_edged) { |
1561 |
start_timer(); |
start_timer(); |
1562 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1563 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pParam->width, pParam->height, 0); |
1564 |
stop_edges_timer(); |
stop_edges_timer(); |
1565 |
|
reference->is_edged = 1; |
1566 |
|
} |
1567 |
|
|
1568 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1569 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1570 |
/* pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; */ |
current->fcode = pParam->m_fcode; |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
|
1571 |
|
|
1572 |
if (!force_inter) |
if (!force_inter) |
1573 |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
1574 |
else |
else |
1575 |
iLimit = mb_width * mb_height + 1; |
iLimit = mb_width * mb_height + 1; |
1576 |
|
|
1577 |
if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1578 |
|
if (reference->is_interpolated != current->rounding_type) { |
1579 |
start_timer(); |
start_timer(); |
1580 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1581 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1582 |
pEnc->mbParam.edged_height, |
pParam->edged_height, |
1583 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1584 |
pEnc->current->rounding_type); |
current->rounding_type); |
1585 |
stop_inter_timer(); |
stop_inter_timer(); |
1586 |
|
reference->is_interpolated = current->rounding_type; |
1587 |
|
} |
1588 |
} |
} |
1589 |
|
|
1590 |
pEnc->current->coding_type = P_VOP; |
current->coding_type = P_VOP; |
1591 |
|
|
1592 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1593 |
|
|
1594 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1595 |
|
|
1596 |
start_timer(); |
start_timer(); |
1597 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1598 |
HintedMESet(pEnc, &bIntra); |
{ int gmcval; |
1599 |
else*/ |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1600 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1601 |
|
|
1602 |
|
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1603 |
|
gmcval = GlobalMotionEstRefine(¤t->warp, |
1604 |
|
current->mbs, pParam, |
1605 |
|
current, reference, |
1606 |
|
¤t->image, |
1607 |
|
&reference->image, |
1608 |
|
&pEnc->vInterH, |
1609 |
|
&pEnc->vInterV, |
1610 |
|
&pEnc->vInterHV); |
1611 |
|
} else { |
1612 |
|
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
1613 |
|
current, |
1614 |
|
&reference->image, |
1615 |
|
¤t->image, |
1616 |
|
pEnc->vGMC.y); |
1617 |
|
} |
1618 |
|
|
1619 |
|
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1620 |
|
|
1621 |
|
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
1622 |
|
generate_GMCparameters( 3, 3, ¤t->warp, |
1623 |
|
pParam->width, pParam->height, |
1624 |
|
¤t->new_gmc_data); |
1625 |
|
|
1626 |
|
if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) || |
1627 |
|
(current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) ) |
1628 |
|
{ |
1629 |
|
current->coding_type = S_VOP; |
1630 |
|
|
1631 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1632 |
|
pParam->mb_width, pParam->mb_height, |
1633 |
|
pParam->edged_width, pParam->edged_width/2, |
1634 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1635 |
|
current->rounding_type, current->mbs, &pEnc->vGMC); |
1636 |
|
|
1637 |
|
} else { |
1638 |
|
|
1639 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1640 |
|
pParam->mb_width, pParam->mb_height, |
1641 |
|
pParam->edged_width, pParam->edged_width/2, |
1642 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1643 |
|
current->rounding_type, current->mbs, NULL); /* no warping, just AMV */ |
1644 |
|
} |
1645 |
|
} |
1646 |
|
|
1647 |
bIntra = |
bIntra = |
1648 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, current, reference, |
1649 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1650 |
iLimit); |
&pEnc->vGMC, iLimit); |
1651 |
|
|
1652 |
|
|
1653 |
stop_motion_timer(); |
stop_motion_timer(); |
1654 |
|
|
1655 |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
1656 |
|
|
1657 |
if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) |
set_timecodes(current,reference,pParam->fbase); |
|
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
|
|
{ |
|
|
pEnc->current->coding_type = S_VOP; |
|
|
|
|
|
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
&pEnc->current->gmc_data); |
|
|
|
|
|
generate_GMCimage(&pEnc->current->gmc_data, &pEnc->reference->image, |
|
|
pEnc->mbParam.mb_width, pEnc->mbParam.mb_height, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_width/2, |
|
|
pEnc->mbParam.m_fcode, (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0, |
|
|
pEnc->current->rounding_type, pEnc->current->mbs, &pEnc->vGMC); |
|
|
|
|
|
} |
|
|
|
|
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
|
1658 |
if (vol_header) |
if (vol_header) |
1659 |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam, current); |
1660 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1661 |
} |
} |
1662 |
|
|
1663 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
1664 |
|
|
1665 |
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1666 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
1667 |
|
|
1668 |
|
|
1669 |
for (y = 0; y < mb_height; y++) { |
for (y = 0; y < mb_height; y++) { |
1670 |
for (x = 0; x < mb_width; x++) { |
for (x = 0; x < mb_width; x++) { |
1671 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = |
1672 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
¤t->mbs[x + y * pParam->mb_width]; |
|
|
|
|
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
|
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
|
1673 |
|
|
1674 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1675 |
|
|
1676 |
if (bIntra) { |
if (bIntra) { |
1677 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1678 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1679 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1680 |
|
|
1681 |
start_timer(); |
start_timer(); |
1682 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(current, x, y, pParam->mb_width, qcoeff); |
1683 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1684 |
|
|
1685 |
pEnc->current->sStat.kblks++; |
current->sStat.kblks++; |
1686 |
|
|
1687 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
1688 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1689 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1690 |
|
qcoeff[5*64+0]=0; |
1691 |
|
} |
1692 |
|
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1693 |
stop_coding_timer(); |
stop_coding_timer(); |
1694 |
continue; |
continue; |
1695 |
} |
} |
1696 |
|
|
|
if (pEnc->current->coding_type == S_VOP) { |
|
|
|
|
|
int32_t iSAD = sad16(pEnc->current->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->vGMC.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->mbParam.edged_width, 65536); |
|
|
|
|
|
if (pEnc->current->motion_flags & XVID_ME_CHROMA16) { |
|
|
iSAD += sad8(pEnc->current->image.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
|
|
pEnc->vGMC.u + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
|
|
|
|
|
iSAD += sad8(pEnc->current->image.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, |
|
|
pEnc->vGMC.v + 8*y*(pEnc->mbParam.edged_width/2) + 8*x, pEnc->mbParam.edged_width/2); |
|
|
} |
|
|
|
|
|
if (iSAD <= pMB->sad16) { /* mode decision GMC */ |
|
|
|
|
|
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
|
|
pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; |
|
|
else |
|
|
pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; |
|
|
|
|
|
pMB->mode = MODE_INTER; |
|
|
pMB->mcsel = 1; |
|
|
pMB->sad16 = iSAD; |
|
|
} else { |
|
|
pMB->mcsel = 0; |
|
|
} |
|
|
} else { |
|
|
pMB->mcsel = 0; /* just a precaution */ |
|
|
} |
|
|
|
|
1697 |
start_timer(); |
start_timer(); |
1698 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1699 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1700 |
&pEnc->vInterHV, &pEnc->vGMC, |
&pEnc->vInterHV, &pEnc->vGMC, |
1701 |
&pEnc->current->image, |
¤t->image, |
1702 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pParam->width, |
1703 |
pEnc->mbParam.height, |
pParam->height, |
1704 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1705 |
(pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
1706 |
(pEnc->current->vop_flags & XVID_VOP_REDUCED), |
(current->vop_flags & XVID_VOP_REDUCED), |
1707 |
pEnc->current->rounding_type); |
current->rounding_type); |
1708 |
|
|
1709 |
stop_comp_timer(); |
stop_comp_timer(); |
1710 |
|
|
|
if (pMB->dquant != 0) { |
|
|
pMB->mode = MODE_INTER_Q; |
|
|
} |
|
|
|
|
1711 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1712 |
|
|
1713 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->mode != MODE_NOT_CODED) |
1714 |
{ pMB->cbp = |
{ pMB->cbp = |
1715 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
1716 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1717 |
} |
} |
1718 |
|
|
1719 |
|
if (pMB->dquant != 0) |
1720 |
|
MBSetDquant(pMB, x, y, &pEnc->mbParam); |
1721 |
|
|
1722 |
|
|
1723 |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1724 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1725 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1726 |
pEnc->current->sStat.mblks++; |
current->sStat.mblks++; |
1727 |
} else { |
} else { |
1728 |
pEnc->current->sStat.ublks++; |
current->sStat.ublks++; |
1729 |
} |
} |
1730 |
|
|
1731 |
start_timer(); |
start_timer(); |
1735 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
1736 |
(pMB->dquant == 0); |
(pMB->dquant == 0); |
1737 |
|
|
1738 |
if (pEnc->current->coding_type == S_VOP) |
if (current->coding_type == S_VOP) |
1739 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1740 |
else if (pEnc->current->coding_type == P_VOP) { |
else if (current->coding_type == P_VOP) { |
1741 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
if ((pParam->vol_flags & XVID_VOL_QUARTERPEL)) |
1742 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
1743 |
else |
else |
1744 |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
1748 |
|
|
1749 |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1750 |
|
|
1751 |
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
if (current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1752 |
{ |
{ |
1753 |
int bSkip = 1; |
int bSkip = 1; |
1754 |
|
|
1755 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1756 |
{ |
{ |
1757 |
int iSAD; |
int iSAD; |
1758 |
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1759 |
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1760 |
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
pParam->edged_width,BFRAME_SKIP_THRESHHOLD); |
1761 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1762 |
{ bSkip = 0; |
{ bSkip = 0; |
1763 |
break; |
break; |
1765 |
} |
} |
1766 |
|
|
1767 |
if (!bSkip) { /* no SKIP, but trivial block */ |
if (!bSkip) { /* no SKIP, but trivial block */ |
1768 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1769 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1770 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1771 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1772 |
} |
} |
1773 |
else { |
else { |
1774 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1775 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1776 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1777 |
} |
} |
1778 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1779 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1780 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1781 |
stop_coding_timer(); |
stop_coding_timer(); |
1782 |
|
|
1783 |
continue; /* next MB */ |
continue; /* next MB */ |
1792 |
} |
} |
1793 |
/* ordinary case: normal coded INTER/INTER4V block */ |
/* ordinary case: normal coded INTER/INTER4V block */ |
1794 |
|
|
1795 |
if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) |
if ((current->vop_flags & XVID_VOP_GREYSCALE)) |
1796 |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1797 |
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 */ |
1798 |
qcoeff[5*64+0]=0; |
qcoeff[5*64+0]=0; |
1799 |
} |
} |
1800 |
|
|
1801 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1802 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1803 |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
1804 |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1805 |
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); |
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); |
1806 |
} else { |
} else { |
1807 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1808 |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
1809 |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1810 |
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); |
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); |
1815 |
{ int k; |
{ int k; |
1816 |
for (k=1;k<4;k++) |
for (k=1;k<4;k++) |
1817 |
{ |
{ |
1818 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
if((pParam->vol_flags & XVID_VOL_QUARTERPEL)) { |
1819 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
1820 |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
1821 |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.y; |
1822 |
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); |
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); |
1823 |
} else { |
} else { |
1824 |
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, k); |
1825 |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
1826 |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.y; |
1827 |
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); |
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); |
1830 |
} |
} |
1831 |
} |
} |
1832 |
|
|
1833 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1834 |
stop_coding_timer(); |
stop_coding_timer(); |
1835 |
|
|
1836 |
} |
} |
1837 |
} |
} |
1838 |
|
|
1839 |
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
if ((current->vop_flags & XVID_VOP_REDUCED)) |
1840 |
{ |
{ |
1841 |
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_deblock_rrv(¤t->image, pParam->edged_width, |
1842 |
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
current->mbs, mb_width, mb_height, pParam->mb_width, |
1843 |
16, 0); |
16, 0); |
1844 |
} |
} |
1845 |
|
|
1846 |
emms(); |
emms(); |
1847 |
|
|
1848 |
/* XXX: hinted me |
if (current->sStat.iMvCount == 0) |
1849 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
current->sStat.iMvCount = 1; |
|
HintedMEGet(pEnc, 0); |
|
|
}*/ |
|
|
|
|
|
if (pEnc->current->sStat.iMvCount == 0) |
|
|
pEnc->current->sStat.iMvCount = 1; |
|
1850 |
|
|
1851 |
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
fSigma = (float) sqrt((float) current->sStat.iMvSum / current->sStat.iMvCount); |
1852 |
|
|
1853 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pParam->m_fcode); |
1854 |
|
|
1855 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1856 |
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
&& (pParam->m_fcode <= (3 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
1857 |
{ |
{ |
1858 |
pEnc->mbParam.m_fcode++; |
pParam->m_fcode++; |
1859 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1860 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1861 |
&& (pEnc->fMvPrevSigma >= 0) |
&& (pEnc->fMvPrevSigma >= 0) |
1862 |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
1863 |
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
&& (pParam->m_fcode >= (2 + (pParam->vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
1864 |
{ |
{ |
1865 |
pEnc->mbParam.m_fcode--; |
pParam->m_fcode--; |
1866 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1867 |
} |
} |
1868 |
|
|
1870 |
|
|
1871 |
/* frame drop code */ |
/* frame drop code */ |
1872 |
#if 0 |
#if 0 |
1873 |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", current->sStat.kblks, current->sStat.mblks, current->sStat.ublks); |
1874 |
#endif |
#endif |
1875 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks <= |
1876 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100) |
1877 |
{ |
{ |
1878 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1879 |
pEnc->current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1880 |
|
|
1881 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1882 |
|
|
1883 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(current,reference,pParam->fbase); |
1884 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); |
1885 |
|
|
1886 |
/* copy reference frame details into the current frame */ |
/* copy reference frame details into the current frame */ |
1887 |
pEnc->current->quant = pEnc->reference->quant; |
current->quant = reference->quant; |
1888 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
current->motion_flags = reference->motion_flags; |
1889 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1890 |
/* pEnc->current->quarterpel = pEnc->reference->quarterpel; */ |
current->fcode = reference->fcode; |
1891 |
pEnc->current->fcode = pEnc->reference->fcode; |
current->bcode = reference->bcode; |
1892 |
pEnc->current->bcode = pEnc->reference->bcode; |
current->stamp = reference->stamp; |
1893 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(¤t->image, &reference->image, pParam->edged_width, pParam->height); |
1894 |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1895 |
|
coded = 0; |
1896 |
|
|
1897 |
|
} else { |
1898 |
|
|
1899 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1900 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1901 |
|
|
1902 |
|
/* what was this frame's interpolated reference will become |
1903 |
|
forward (past) reference in b-frame coding */ |
1904 |
|
|
1905 |
|
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1906 |
|
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1907 |
|
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1908 |
} |
} |
1909 |
|
|
1910 |
/* XXX: debug |
/* XXX: debug |
1911 |
{ |
{ |
1912 |
char s[100]; |
char s[100]; |
1913 |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
1914 |
image_dump_yuvpgm(&pEnc->current->image, |
image_dump_yuvpgm(¤t->image, |
1915 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1916 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1917 |
|
|
1918 |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
1919 |
image_dump_yuvpgm(&pEnc->reference->image, |
image_dump_yuvpgm(&reference->image, |
1920 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1921 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1922 |
} |
} |
1923 |
*/ |
*/ |
1924 |
|
|
1925 |
/* XXX: Remove the two #if 0 blocks when we are sure we must always pad the stream */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
|
#if 0 |
|
|
/* for divx5 compatibility, we must always pad between the packed p and b frames */ |
|
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) |
|
|
#endif |
|
|
BitstreamPadAlways(bs); |
|
|
#if 0 |
|
|
else |
|
|
BitstreamPad(bs); |
|
|
#endif |
|
1926 |
|
|
1927 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1928 |
|
|
1929 |
return 0; /* inter */ |
return coded; |
1930 |
} |
} |
1931 |
|
|
1932 |
|
|
1957 |
} |
} |
1958 |
#endif |
#endif |
1959 |
|
|
|
/* frame->quarterpel = pEnc->mbParam.m_quarterpel; */ |
|
|
|
|
1960 |
/* forward */ |
/* forward */ |
1961 |
|
if (!pEnc->reference->is_edged) { |
1962 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1963 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1964 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1965 |
|
pEnc->current->is_edged = 1; |
1966 |
|
} |
1967 |
|
|
1968 |
|
if (pEnc->reference->is_interpolated != 0) { |
1969 |
start_timer(); |
start_timer(); |
1970 |
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, |
1971 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1972 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1973 |
stop_inter_timer(); |
stop_inter_timer(); |
1974 |
|
pEnc->reference->is_interpolated = 0; |
1975 |
|
} |
1976 |
|
|
1977 |
/* backward */ |
/* backward */ |
1978 |
|
if (!pEnc->current->is_edged) { |
1979 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1980 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1981 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1982 |
|
pEnc->current->is_edged = 1; |
1983 |
|
} |
1984 |
|
|
1985 |
|
if (pEnc->current->is_interpolated != 0) { |
1986 |
start_timer(); |
start_timer(); |
1987 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1988 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1989 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1990 |
stop_inter_timer(); |
stop_inter_timer(); |
1991 |
|
pEnc->current->is_interpolated = 0; |
1992 |
|
} |
1993 |
|
|
1994 |
start_timer(); |
frame->coding_type = B_VOP; |
1995 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1996 |
|
|
1997 |
|
start_timer(); |
1998 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1999 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
2000 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
2002 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
2003 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
2004 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
|
2005 |
stop_motion_timer(); |
stop_motion_timer(); |
2006 |
|
|
|
/* |
|
|
if (test_quant_type(&pEnc->mbParam, pEnc->current)) { |
|
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
|
|
} |
|
|
*/ |
|
|
|
|
|
frame->coding_type = B_VOP; |
|
|
|
|
2007 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
2008 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
2009 |
|
|
2010 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
2011 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
2017 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
2018 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
2019 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
int direction = frame->vop_flags & XVID_VOP_ALTERNATESCAN ? 2 : 0; |
|
2020 |
|
|
2021 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
2022 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
2023 |
/* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */ |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
2024 |
|
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
2025 |
|
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0, 0); |
2026 |
|
} |
2027 |
|
|
2028 |
continue; |
continue; |
2029 |
} |
} |
2030 |
|
|
2047 |
} |
} |
2048 |
} |
} |
2049 |
|
|
2050 |
#ifdef BFRAMES_DEC_DEBUG |
/* keep only bits 5-2 -- Chroma blocks will just be skipped by the |
2051 |
BFRAME_DEBUG |
* coding function for BFrames, that's why we don't zero teh DC |
2052 |
#endif |
* coeffs */ |
2053 |
|
if ((frame->vop_flags & XVID_VOP_GREYSCALE)) |
2054 |
|
mb->cbp &= 0x3C; |
2055 |
|
|
2056 |
start_timer(); |
start_timer(); |
2057 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
2058 |
&frame->sStat, direction); |
&frame->sStat); |
2059 |
stop_coding_timer(); |
stop_coding_timer(); |
2060 |
} |
} |
2061 |
} |
} |
2064 |
|
|
2065 |
/* TODO: dynamic fcode/bcode ??? */ |
/* TODO: dynamic fcode/bcode ??? */ |
2066 |
|
|
2067 |
BitstreamPadAlways(bs); |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
2068 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
2069 |
|
|
2070 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |