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" |
57 |
Bitstream * bs); |
Bitstream * bs); |
58 |
|
|
59 |
static int FrameCodeP(Encoder * pEnc, |
static int FrameCodeP(Encoder * pEnc, |
60 |
Bitstream * bs, |
Bitstream * bs); |
|
bool force_inter, |
|
|
bool vol_header); |
|
61 |
|
|
62 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
63 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
100 |
i--; |
i--; |
101 |
} |
} |
102 |
|
|
103 |
/* if neccessary, round to 65535 accuracy */ |
if (*base > 65535 || *inc > 65535) { |
104 |
if (*base > 65535) { |
int *biggest; |
105 |
float div = (float) *base / 65535; |
int *other; |
106 |
*base = (int) (*base / div); |
float div; |
107 |
*inc = (int) (*inc / div); |
|
108 |
|
if (*base > *inc) { |
109 |
|
biggest = base; |
110 |
|
other = inc; |
111 |
|
} else { |
112 |
|
biggest = inc; |
113 |
|
other = base; |
114 |
|
} |
115 |
|
|
116 |
|
div = ((float)*biggest)/((float)65535); |
117 |
|
*biggest = (int)(((float)*biggest)/div); |
118 |
|
*other = (int)(((float)*other)/div); |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
126 |
Encoder *pEnc; |
Encoder *pEnc; |
127 |
int n; |
int n; |
128 |
|
|
129 |
if (XVID_MAJOR(create->version) != 1) /* v1.x.x */ |
if (XVID_VERSION_MAJOR(create->version) != 1) /* v1.x.x */ |
130 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
131 |
|
|
132 |
if (create->width%2 || create->height%2) |
if (create->width%2 || create->height%2) |
133 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
134 |
|
|
135 |
|
if (create->width<=0 || create->height<=0) |
136 |
|
return XVID_ERR_FAIL; |
137 |
|
|
138 |
/* allocate encoder struct */ |
/* allocate encoder struct */ |
139 |
|
|
140 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
146 |
|
|
147 |
/* global flags */ |
/* global flags */ |
148 |
pEnc->mbParam.global_flags = create->global; |
pEnc->mbParam.global_flags = create->global; |
149 |
|
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED)) |
150 |
|
pEnc->mbParam.global_flags |= XVID_GLOBAL_DIVX5_USERDATA; |
151 |
|
|
152 |
/* width, height */ |
/* width, height */ |
153 |
pEnc->mbParam.width = create->width; |
pEnc->mbParam.width = create->width; |
202 |
pcreate.zones = pEnc->zones; |
pcreate.zones = pEnc->zones; |
203 |
pcreate.width = pEnc->mbParam.width; |
pcreate.width = pEnc->mbParam.width; |
204 |
pcreate.height = pEnc->mbParam.height; |
pcreate.height = pEnc->mbParam.height; |
205 |
|
pcreate.mb_width = pEnc->mbParam.mb_width; |
206 |
|
pcreate.mb_height = pEnc->mbParam.mb_height; |
207 |
pcreate.fincr = pEnc->mbParam.fincr; |
pcreate.fincr = pEnc->mbParam.fincr; |
208 |
pcreate.fbase = pEnc->mbParam.fbase; |
pcreate.fbase = pEnc->mbParam.fbase; |
209 |
pcreate.param = create->plugins[n].param; |
pcreate.param = create->plugins[n].param; |
223 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
224 |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
225 |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
226 |
|
if (pEnc->temp_dquants==NULL) |
227 |
|
goto xvid_err_memory1a; |
228 |
} |
} |
|
/* XXX: error checking */ |
|
229 |
|
|
230 |
/* bframes */ |
/* bframes */ |
231 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
242 |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
pEnc->mbParam.frame_drop_ratio = MAX(create->frame_drop_ratio, 0); |
243 |
|
|
244 |
/* max keyframe interval */ |
/* max keyframe interval */ |
245 |
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; */ |
|
|
|
|
246 |
|
|
247 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
248 |
|
|
264 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
265 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
266 |
|
|
267 |
|
/* allocate quant matrix memory */ |
268 |
|
|
269 |
|
pEnc->mbParam.mpeg_quant_matrices = |
270 |
|
xvid_malloc(sizeof(uint16_t) * 64 * 8, CACHE_LINE); |
271 |
|
|
272 |
|
if (pEnc->mbParam.mpeg_quant_matrices == NULL) |
273 |
|
goto xvid_err_memory2a; |
274 |
|
|
275 |
/* allocate interpolation image memory */ |
/* allocate interpolation image memory */ |
276 |
|
|
277 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
287 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
288 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
289 |
image_null(&pEnc->vInterV); |
image_null(&pEnc->vInterV); |
|
image_null(&pEnc->vInterVf); |
|
290 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
|
image_null(&pEnc->vInterHVf); |
|
291 |
|
|
292 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
293 |
if (image_create |
if (image_create |
331 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
332 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
333 |
if (image_create |
if (image_create |
|
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height) < 0) |
|
|
goto xvid_err_memory3; |
|
|
if (image_create |
|
334 |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
335 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
336 |
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; |
|
337 |
|
|
338 |
/* Create full bitplane for GMC, this might be wasteful */ |
/* Create full bitplane for GMC, this might be wasteful */ |
339 |
if (image_create |
if (image_create |
403 |
image_null(&pEnc->queue[n].image); |
image_null(&pEnc->queue[n].image); |
404 |
|
|
405 |
|
|
406 |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
|
{ |
|
407 |
if (image_create |
if (image_create |
408 |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
(&pEnc->queue[n].image, pEnc->mbParam.edged_width, |
409 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
410 |
goto xvid_err_memory5; |
goto xvid_err_memory5; |
|
|
|
411 |
} |
} |
412 |
|
|
|
|
|
413 |
/* timestamp stuff */ |
/* timestamp stuff */ |
414 |
|
|
415 |
pEnc->mbParam.m_stamp = 0; |
pEnc->mbParam.m_stamp = 0; |
425 |
create->handle = (void *) pEnc; |
create->handle = (void *) pEnc; |
426 |
|
|
427 |
init_timer(); |
init_timer(); |
428 |
|
init_mpeg_matrix(pEnc->mbParam.mpeg_quant_matrices); |
429 |
|
|
430 |
return 0; /* ok */ |
return 0; /* ok */ |
431 |
|
|
435 |
|
|
436 |
xvid_err_memory5: |
xvid_err_memory5: |
437 |
|
|
438 |
if (pEnc->mbParam.max_bframes > 0) { |
for (n = 0; n < pEnc->mbParam.max_bframes+1; n++) { |
439 |
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, |
|
440 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
441 |
} |
} |
442 |
|
|
443 |
xvid_free(pEnc->queue); |
xvid_free(pEnc->queue); |
|
} |
|
444 |
|
|
445 |
xvid_err_memory4: |
xvid_err_memory4: |
446 |
|
|
454 |
|
|
455 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
456 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
457 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
458 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
459 |
} |
} |
460 |
|
|
461 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
485 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
486 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
487 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
488 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
489 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
490 |
|
|
491 |
/* destroy GMC image */ |
/* destroy GMC image */ |
492 |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vGMC, pEnc->mbParam.edged_width, |
493 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
494 |
|
|
495 |
|
xvid_err_memory2a: |
496 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
497 |
|
|
498 |
xvid_err_memory2: |
xvid_err_memory2: |
499 |
xvid_free(pEnc->current->mbs); |
xvid_free(pEnc->current->mbs); |
503 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
504 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
505 |
|
|
506 |
|
xvid_err_memory1a: |
507 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
508 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
509 |
} |
} |
542 |
int i; |
int i; |
543 |
|
|
544 |
/* B Frames specific */ |
/* B Frames specific */ |
|
if (pEnc->mbParam.max_bframes > 0) { |
|
|
|
|
545 |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes+1; i++) { |
|
|
|
546 |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->queue[i].image, pEnc->mbParam.edged_width, |
547 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
548 |
} |
} |
|
xvid_free(pEnc->queue); |
|
|
} |
|
549 |
|
|
550 |
|
xvid_free(pEnc->queue); |
551 |
|
|
552 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
553 |
|
|
558 |
|
|
559 |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
560 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
|
|
561 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
|
|
|
562 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
563 |
} |
} |
564 |
|
|
576 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
577 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
578 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
579 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
580 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.edged_height); |
|
|
|
|
581 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
582 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
583 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
622 |
xvid_free(pEnc->plugins); |
xvid_free(pEnc->plugins); |
623 |
} |
} |
624 |
|
|
625 |
|
xvid_free(pEnc->mbParam.mpeg_quant_matrices); |
626 |
|
|
627 |
if (pEnc->num_plugins>0) |
if (pEnc->num_plugins>0) |
628 |
xvid_free(pEnc->zones); |
xvid_free(pEnc->zones); |
629 |
|
|
658 |
data.mb_height = pEnc->mbParam.mb_height; |
data.mb_height = pEnc->mbParam.mb_height; |
659 |
data.fincr = frame->fincr; |
data.fincr = frame->fincr; |
660 |
data.fbase = pEnc->mbParam.fbase; |
data.fbase = pEnc->mbParam.fbase; |
661 |
|
data.bquant_ratio = pEnc->mbParam.bquant_ratio; |
662 |
|
data.bquant_offset = pEnc->mbParam.bquant_offset; |
663 |
|
|
664 |
for (i=0; i<3; i++) { |
for (i=0; i<3; i++) { |
665 |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
data.min_quant[i] = pEnc->mbParam.min_quant[i]; |
666 |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
data.max_quant[i] = pEnc->mbParam.max_quant[i]; |
667 |
} |
} |
668 |
|
|
669 |
data.reference.csp = XVID_CSP_USER; |
data.reference.csp = XVID_CSP_PLANAR; |
670 |
data.reference.plane[0] = pEnc->reference->image.y; |
data.reference.plane[0] = pEnc->reference->image.y; |
671 |
data.reference.plane[1] = pEnc->reference->image.u; |
data.reference.plane[1] = pEnc->reference->image.u; |
672 |
data.reference.plane[2] = pEnc->reference->image.v; |
data.reference.plane[2] = pEnc->reference->image.v; |
674 |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
data.reference.stride[1] = pEnc->mbParam.edged_width/2; |
675 |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
data.reference.stride[2] = pEnc->mbParam.edged_width/2; |
676 |
|
|
677 |
data.current.csp = XVID_CSP_USER; |
data.current.csp = XVID_CSP_PLANAR; |
678 |
data.current.plane[0] = frame->image.y; |
data.current.plane[0] = frame->image.y; |
679 |
data.current.plane[1] = frame->image.u; |
data.current.plane[1] = frame->image.u; |
680 |
data.current.plane[2] = frame->image.v; |
data.current.plane[2] = frame->image.v; |
685 |
data.frame_num = frame->frame_num; |
data.frame_num = frame->frame_num; |
686 |
|
|
687 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
688 |
data.type = XVID_TYPE_AUTO; |
data.type = *type; |
689 |
data.quant = 0; |
data.quant = *quant; |
690 |
|
|
691 |
|
data.vol_flags = frame->vol_flags; |
692 |
|
data.vop_flags = frame->vop_flags; |
693 |
|
data.motion_flags = frame->motion_flags; |
694 |
|
|
695 |
|
} else if (opt == XVID_PLG_FRAME) { |
696 |
|
data.type = coding2type(frame->coding_type); |
697 |
|
data.quant = frame->quant; |
698 |
|
|
699 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
700 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
702 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
703 |
} |
} |
704 |
|
|
|
/* todo: [vol,vop,motion]_flags*/ |
|
|
|
|
705 |
} else { /* XVID_PLG_AFTER */ |
} else { /* XVID_PLG_AFTER */ |
706 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
707 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_PLANAR; |
708 |
data.original.plane[0] = original->y; |
data.original.plane[0] = original->y; |
709 |
data.original.plane[1] = original->u; |
data.original.plane[1] = original->u; |
710 |
data.original.plane[2] = original->v; |
data.original.plane[2] = original->v; |
741 |
|
|
742 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
743 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
744 |
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; |
745 |
} |
} |
746 |
} |
} |
747 |
|
|
754 |
data.mblks = frame->sStat.mblks; |
data.mblks = frame->sStat.mblks; |
755 |
data.ublks = frame->sStat.ublks; |
data.ublks = frame->sStat.ublks; |
756 |
|
|
757 |
if (stats) { |
/* New code */ |
758 |
stats->type = coding2type(frame->coding_type); |
data.stats.type = coding2type(frame->coding_type); |
759 |
stats->quant = frame->quant; |
data.stats.quant = frame->quant; |
760 |
stats->vol_flags = frame->vol_flags; |
data.stats.vol_flags = frame->vol_flags; |
761 |
stats->vop_flags = frame->vop_flags; |
data.stats.vop_flags = frame->vop_flags; |
762 |
stats->length = frame->length; |
data.stats.length = frame->length; |
763 |
stats->hlength = frame->length - (frame->sStat.iTextBits / 8); |
data.stats.hlength = frame->length - (frame->sStat.iTextBits / 8); |
764 |
stats->kblks = frame->sStat.kblks; |
data.stats.kblks = frame->sStat.kblks; |
765 |
stats->mblks = frame->sStat.mblks; |
data.stats.mblks = frame->sStat.mblks; |
766 |
stats->ublks = frame->sStat.ublks; |
data.stats.ublks = frame->sStat.ublks; |
767 |
stats->sse_y = data.sse_y; |
data.stats.sse_y = data.sse_y; |
768 |
stats->sse_u = data.sse_u; |
data.stats.sse_u = data.sse_u; |
769 |
stats->sse_v = data.sse_v; |
data.stats.sse_v = data.sse_v; |
770 |
} |
|
771 |
|
if (stats) |
772 |
|
*stats = data.stats; |
773 |
} |
} |
774 |
|
|
775 |
/* call plugins */ |
/* call plugins */ |
776 |
for (i=0; i<pEnc->num_plugins;i++) { |
for (i=0; i<(unsigned int)pEnc->num_plugins;i++) { |
777 |
emms(); |
emms(); |
778 |
if (pEnc->plugins[i].func) { |
if (pEnc->plugins[i].func) { |
779 |
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) { |
788 |
*type = data.type; |
*type = data.type; |
789 |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
*quant = data.quant > 0 ? data.quant : 2; /* default */ |
790 |
|
|
791 |
|
frame->vol_flags = data.vol_flags; |
792 |
|
frame->vop_flags = data.vop_flags; |
793 |
|
frame->motion_flags = data.motion_flags; |
794 |
|
|
795 |
|
} else if (opt == XVID_PLG_FRAME) { |
796 |
|
|
797 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
798 |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
for (j=0; j<pEnc->mbParam.mb_height; j++) |
799 |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
for (i=0; i<pEnc->mbParam.mb_width; i++) { |
805 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
806 |
} |
} |
807 |
} |
} |
808 |
/* todo: [vol,vop,motion]_flags*/ |
frame->mbs[0].quant = data.quant; /* FRAME will not affect the quant in stats */ |
|
} |
|
809 |
} |
} |
810 |
|
|
811 |
|
|
812 |
|
} |
813 |
|
|
814 |
|
|
815 |
static __inline void inc_frame_num(Encoder * pEnc) |
static __inline void inc_frame_num(Encoder * pEnc) |
827 |
pEnc->m_framenum--; /* debug ticker */ |
pEnc->m_framenum--; /* debug ticker */ |
828 |
} |
} |
829 |
|
|
830 |
|
static __inline void |
831 |
|
MBSetDquant(MACROBLOCK * pMB, int x, int y, MBParam * mbParam) |
832 |
|
{ |
833 |
|
if (pMB->cbp == 0) { |
834 |
|
/* we want to code dquant but the quantizer value will not be used yet |
835 |
|
let's find out if we can postpone dquant to next MB |
836 |
|
*/ |
837 |
|
if (x == mbParam->mb_width-1 && y == mbParam->mb_height-1) { |
838 |
|
pMB->dquant = 0; /* it's the last MB of all, the easiest case */ |
839 |
|
return; |
840 |
|
} else { |
841 |
|
MACROBLOCK * next = pMB + 1; |
842 |
|
const MACROBLOCK * prev = pMB - 1; |
843 |
|
if (next->mode != MODE_INTER4V && next->mode != MODE_NOT_CODED) |
844 |
|
/* mode allows dquant change in the future */ |
845 |
|
if (abs(next->quant - prev->quant) <= 2) { |
846 |
|
/* quant change is not out of range */ |
847 |
|
pMB->quant = prev->quant; |
848 |
|
pMB->dquant = 0; |
849 |
|
next->dquant = next->quant - prev->quant; |
850 |
|
return; |
851 |
|
} |
852 |
|
} |
853 |
|
} |
854 |
|
/* couldn't skip this dquant */ |
855 |
|
pMB->mode = MODE_INTER_Q; |
856 |
|
} |
857 |
|
|
858 |
|
|
859 |
|
|
860 |
static __inline void |
static __inline void |
864 |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
pCur->ticks = (int32_t)pCur->stamp % time_base; |
865 |
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) ; |
866 |
|
|
867 |
/* HEAVY DEBUG OUTPUT remove when timecodes prove to be stable */ |
#if 0 /* HEAVY DEBUG OUTPUT */ |
868 |
|
fprintf(stderr,"WriteVop: %d - %d \n", |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
|
869 |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
870 |
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", |
871 |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
872 |
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", |
873 |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
874 |
|
#endif |
875 |
|
} |
876 |
|
|
877 |
*/ |
static int |
878 |
|
gcd(int a, int b) |
879 |
|
{ |
880 |
|
int r ; |
881 |
|
|
882 |
|
if (b > a) { |
883 |
|
r = a; |
884 |
|
a = b; |
885 |
|
b = r; |
886 |
} |
} |
887 |
|
|
888 |
|
while ((r = a % b)) { |
889 |
|
a = b; |
890 |
|
b = r; |
891 |
|
} |
892 |
|
return b; |
893 |
|
} |
894 |
|
|
895 |
|
static void |
896 |
|
simplify_par(int *par_width, int *par_height) |
897 |
|
{ |
898 |
|
|
899 |
|
int _par_width = (!*par_width) ? 1 : (*par_width<0) ? -*par_width: *par_width; |
900 |
|
int _par_height = (!*par_height) ? 1 : (*par_height<0) ? -*par_height: *par_height; |
901 |
|
int divisor = gcd(_par_width, _par_height); |
902 |
|
|
903 |
|
_par_width /= divisor; |
904 |
|
_par_height /= divisor; |
905 |
|
|
906 |
|
/* 2^8 precision maximum */ |
907 |
|
if (_par_width>255 || _par_height>255) { |
908 |
|
float div; |
909 |
|
emms(); |
910 |
|
if (_par_width>_par_height) |
911 |
|
div = (float)_par_width/255; |
912 |
|
else |
913 |
|
div = (float)_par_height/255; |
914 |
|
|
915 |
|
_par_width = (int)((float)_par_width/div); |
916 |
|
_par_height = (int)((float)_par_height/div); |
917 |
|
} |
918 |
|
|
919 |
|
*par_width = _par_width; |
920 |
|
*par_height = _par_height; |
921 |
|
|
922 |
|
return; |
923 |
|
} |
924 |
|
|
925 |
|
|
926 |
/***************************************************************************** |
/***************************************************************************** |
945 |
int type; |
int type; |
946 |
Bitstream bs; |
Bitstream bs; |
947 |
|
|
948 |
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 */ |
949 |
return XVID_ERR_VERSION; |
return XVID_ERR_VERSION; |
950 |
|
|
951 |
xFrame->out_flags = 0; |
xFrame->out_flags = 0; |
982 |
|
|
983 |
if (xFrame->quant_intra_matrix) |
if (xFrame->quant_intra_matrix) |
984 |
{ |
{ |
985 |
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)); |
986 |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
q->frame.quant_intra_matrix = q->quant_intra_matrix; |
987 |
} |
} |
988 |
|
|
989 |
if (xFrame->quant_inter_matrix) |
if (xFrame->quant_inter_matrix) |
990 |
{ |
{ |
991 |
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)); |
992 |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
q->frame.quant_inter_matrix = q->quant_inter_matrix; |
993 |
} |
} |
994 |
|
|
1043 |
tmp = pEnc->current->seconds; |
tmp = pEnc->current->seconds; |
1044 |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
pEnc->current->seconds = 0; /* force time_base = 0 */ |
1045 |
|
|
1046 |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0, pEnc->current->quant); |
1047 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1048 |
pEnc->current->seconds = tmp; |
pEnc->current->seconds = tmp; |
1049 |
|
|
1088 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1089 |
|
|
1090 |
/* convert B-VOP to P-VOP */ |
/* convert B-VOP to P-VOP */ |
1091 |
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; |
1092 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1093 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1094 |
|
|
1095 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
1096 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1100 |
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", |
1101 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1102 |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
1103 |
|
pEnc->mbParam.frame_drop_ratio = -1; /* it must be a coded vop */ |
1104 |
|
|
1105 |
FrameCodeP(pEnc, &bs, 1, 0); |
FrameCodeP(pEnc, &bs); |
1106 |
|
|
1107 |
|
|
1108 |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
if ((pEnc->mbParam.global_flags & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail==0) { |
1136 |
|
|
1137 |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
pEnc->current->fincr = pEnc->mbParam.fincr>0 ? pEnc->mbParam.fincr : frame->fincr; |
1138 |
inc_frame_num(pEnc); |
inc_frame_num(pEnc); |
1139 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; |
pEnc->current->vol_flags = frame->vol_flags; |
1140 |
pEnc->current->vop_flags = frame->vop_flags; |
pEnc->current->vop_flags = frame->vop_flags; |
1141 |
pEnc->current->motion_flags = frame->motion; |
pEnc->current->motion_flags = frame->motion; |
1142 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1166 |
}else{ |
}else{ |
1167 |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
type = MEanalysis(&pEnc->reference->image, pEnc->current, |
1168 |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
&pEnc->mbParam, pEnc->mbParam.iMaxKeyInterval, |
1169 |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold); |
pEnc->iFrameNum, pEnc->bframenum_tail, xFrame->bframe_threshold, |
1170 |
|
(pEnc->bframes) ? pEnc->bframes[pEnc->bframenum_head]->mbs: NULL); |
1171 |
} |
} |
1172 |
} |
} |
1173 |
|
|
1174 |
|
if (type != I_VOP) |
1175 |
|
pEnc->current->vol_flags = pEnc->mbParam.vol_flags; /* don't allow VOL changes here */ |
1176 |
|
|
1177 |
/* bframes buffer overflow check */ |
/* bframes buffer overflow check */ |
1178 |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
if (type == B_VOP && pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
1179 |
type = P_VOP; |
type = P_VOP; |
1183 |
|
|
1184 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1185 |
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, |
1186 |
"%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); |
1187 |
} |
} |
1188 |
|
|
1189 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
1258 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1259 |
|
|
1260 |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
if ((pEnc->current->vop_flags & XVID_VOP_DEBUG)) { |
1261 |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "CLOSED GOP BVOP->PVOP"); |
1262 |
} |
} |
1263 |
|
|
1264 |
/* convert B-VOP quant to P-VOP */ |
/* convert B-VOP quant to P-VOP */ |
1265 |
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; |
1266 |
|
pEnc->current->quant += pEnc->mbParam.bquant_ratio - 1; /* to avoid rouding issues */ |
1267 |
|
pEnc->current->quant /= pEnc->mbParam.bquant_ratio; |
1268 |
type = P_VOP; |
type = P_VOP; |
1269 |
} |
} |
1270 |
|
|
1283 |
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"); |
1284 |
} |
} |
1285 |
|
|
1286 |
|
pEnc->iFrameNum = 1; |
1287 |
|
|
1288 |
/* ---- update vol flags at IVOP ----------- */ |
/* ---- update vol flags at IVOP ----------- */ |
1289 |
pEnc->current->vol_flags = pEnc->mbParam.vol_flags = frame->vol_flags; |
pEnc->mbParam.vol_flags = pEnc->current->vol_flags; |
1290 |
|
|
1291 |
|
/* Aspect ratio */ |
1292 |
|
switch(frame->par) { |
1293 |
|
case XVID_PAR_11_VGA: |
1294 |
|
case XVID_PAR_43_PAL: |
1295 |
|
case XVID_PAR_43_NTSC: |
1296 |
|
case XVID_PAR_169_PAL: |
1297 |
|
case XVID_PAR_169_NTSC: |
1298 |
|
case XVID_PAR_EXT: |
1299 |
|
pEnc->mbParam.par = frame->par; |
1300 |
|
break; |
1301 |
|
default: |
1302 |
|
pEnc->mbParam.par = XVID_PAR_11_VGA; |
1303 |
|
break; |
1304 |
|
} |
1305 |
|
|
1306 |
|
/* For extended PAR only, we try to sanityse/simplify par values */ |
1307 |
|
if (pEnc->mbParam.par == XVID_PAR_EXT) { |
1308 |
|
pEnc->mbParam.par_width = frame->par_width; |
1309 |
|
pEnc->mbParam.par_height = frame->par_height; |
1310 |
|
simplify_par(&pEnc->mbParam.par_width, &pEnc->mbParam.par_height); |
1311 |
|
} |
1312 |
|
|
1313 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
if ((pEnc->mbParam.vol_flags & XVID_VOL_MPEGQUANT)) { |
1314 |
if (frame->quant_intra_matrix != NULL) |
if (frame->quant_intra_matrix != NULL) |
1315 |
set_intra_matrix(frame->quant_intra_matrix); |
set_intra_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_intra_matrix); |
1316 |
if (frame->quant_inter_matrix != NULL) |
if (frame->quant_inter_matrix != NULL) |
1317 |
set_inter_matrix(frame->quant_inter_matrix); |
set_inter_matrix(pEnc->mbParam.mpeg_quant_matrices, frame->quant_inter_matrix); |
1318 |
} |
} |
1319 |
|
|
1320 |
/* prevent vol/vop misuse */ |
/* prevent vol/vop misuse */ |
1321 |
|
|
|
if (!(pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
pEnc->current->vop_flags &= ~XVID_VOP_REDUCED; |
|
|
|
|
1322 |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
if (!(pEnc->current->vol_flags & XVID_VOL_INTERLACING)) |
1323 |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
pEnc->current->vop_flags &= ~(XVID_VOP_TOPFIELDFIRST|XVID_VOP_ALTERNATESCAN); |
1324 |
|
|
1351 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1352 |
} |
} |
1353 |
|
|
1354 |
FrameCodeP(pEnc, &bs, 1, 0); |
if ( FrameCodeP(pEnc, &bs) == 0 ) { |
1355 |
|
/* N-VOP, we mustn't code b-frames yet */ |
1356 |
|
call_plugins(pEnc, pEnc->current, &pEnc->sOriginal, XVID_PLG_AFTER, 0, 0, stats); |
1357 |
|
goto done; |
1358 |
|
} |
1359 |
} |
} |
1360 |
|
|
1361 |
|
|
1363 |
* on next enc_encode call we must flush bframes |
* on next enc_encode call we must flush bframes |
1364 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1365 |
|
|
1366 |
done_flush: |
/*done_flush:*/ |
1367 |
|
|
1368 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
1369 |
|
|
1393 |
|
|
1394 |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
static void SetMacroblockQuants(MBParam * const pParam, FRAMEINFO * frame) |
1395 |
{ |
{ |
1396 |
unsigned int i,j; |
unsigned int i; |
1397 |
int quant = frame->quant; |
MACROBLOCK * pMB = frame->mbs; |
1398 |
|
int quant = frame->mbs[0].quant; /* set by XVID_PLG_FRAME */ |
1399 |
|
if (quant > 31) |
1400 |
|
frame->quant = quant = 31; |
1401 |
|
else if (quant < 1) |
1402 |
|
frame->quant = quant = 1; |
1403 |
|
|
1404 |
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]; |
|
1405 |
quant += pMB->dquant; |
quant += pMB->dquant; |
1406 |
if (quant > 31) |
if (quant > 31) |
1407 |
quant = 31; |
quant = 31; |
1408 |
if (quant < 1) |
else if (quant < 1) |
1409 |
quant = 1; |
quant = 1; |
1410 |
pMB->quant = quant; |
pMB->quant = quant; |
1411 |
|
pMB++; |
1412 |
} |
} |
1413 |
} |
} |
1414 |
|
|
1446 |
|
|
1447 |
uint16_t x, y; |
uint16_t x, y; |
1448 |
|
|
|
if ((pEnc->current->vol_flags & XVID_VOL_REDUCED_ENABLE)) |
|
|
{ |
|
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
|
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
|
|
|
/* 16x16->8x8 downsample requires 1 additional edge pixel*/ |
|
|
/* XXX: setedges is overkill */ |
|
|
start_timer(); |
|
|
image_setedges(&pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, pEnc->mbParam.height); |
|
|
stop_edges_timer(); |
|
|
} |
|
|
|
|
1449 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1450 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1451 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1452 |
|
|
1453 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1454 |
|
|
1455 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1456 |
|
|
1457 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1458 |
|
|
1459 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1460 |
|
|
1461 |
BitstreamPadAlways(bs); |
BitstreamPad(bs); |
1462 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
|
1463 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1, pEnc->current->mbs[0].quant); |
1464 |
|
|
1465 |
pEnc->current->sStat.iTextBits = 0; |
pEnc->current->sStat.iTextBits = 0; |
1466 |
pEnc->current->sStat.kblks = mb_width * mb_height; |
pEnc->current->sStat.kblks = mb_width * mb_height; |
1481 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1482 |
|
|
1483 |
start_timer(); |
start_timer(); |
|
if (pEnc->current->vop_flags & XVID_VOP_GREYSCALE) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
1484 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
1485 |
stop_coding_timer(); |
stop_coding_timer(); |
1486 |
} |
} |
1487 |
|
|
|
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
|
|
16, 0); |
|
|
} |
|
1488 |
emms(); |
emms(); |
1489 |
|
|
1490 |
/* 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() */ |
1491 |
#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 |
|
1492 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
1493 |
|
|
1494 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1495 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1496 |
|
|
1497 |
/* XXX: hinted me |
pEnc->current->is_edged = 0; /* not edged */ |
1498 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
|
HintedMEGet(pEnc, 1); |
|
|
}*/ |
|
1499 |
|
|
1500 |
return 1; /* intra */ |
return 1; /* intra */ |
1501 |
} |
} |
1502 |
|
|
1503 |
|
static __inline void |
1504 |
|
updateFcode(Statistics * sStat, Encoder * pEnc) |
1505 |
|
{ |
1506 |
|
float fSigma; |
1507 |
|
int iSearchRange; |
1508 |
|
|
1509 |
#define INTRA_THRESHOLD 0.5 |
if (sStat->iMvCount == 0) |
1510 |
#define BFRAME_SKIP_THRESHHOLD 30 |
sStat->iMvCount = 1; |
1511 |
|
|
1512 |
|
fSigma = (float) sqrt((float) sStat->iMvSum / sStat->iMvCount); |
1513 |
|
|
1514 |
|
iSearchRange = 16 << pEnc->mbParam.m_fcode; |
1515 |
|
|
1516 |
|
if ((3.0 * fSigma > iSearchRange) && (pEnc->mbParam.m_fcode <= 5) ) |
1517 |
|
pEnc->mbParam.m_fcode++; |
1518 |
|
|
1519 |
|
else if ((5.0 * fSigma < iSearchRange) |
1520 |
|
&& (4.0 * pEnc->fMvPrevSigma < iSearchRange) |
1521 |
|
&& (pEnc->mbParam.m_fcode >= 2) ) |
1522 |
|
pEnc->mbParam.m_fcode--; |
1523 |
|
|
1524 |
|
pEnc->fMvPrevSigma = fSigma; |
1525 |
|
} |
1526 |
|
|
1527 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1528 |
|
|
1529 |
/* FrameCodeP also handles S(GMC)-VOPs */ |
/* FrameCodeP also handles S(GMC)-VOPs */ |
1530 |
static int |
static int |
1531 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1532 |
Bitstream * bs, |
Bitstream * bs) |
|
bool force_inter, |
|
|
bool vol_header) |
|
1533 |
{ |
{ |
|
float fSigma; |
|
1534 |
int bits = BitstreamPos(bs); |
int bits = BitstreamPos(bs); |
1535 |
|
|
1536 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1537 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1538 |
|
|
|
int mb_width = pEnc->mbParam.mb_width; |
|
|
int mb_height = pEnc->mbParam.mb_height; |
|
|
|
|
|
int iLimit; |
|
1539 |
int x, y, k; |
int x, y, k; |
1540 |
int iSearchRange; |
FRAMEINFO *const current = pEnc->current; |
1541 |
int bIntra, skip_possible; |
FRAMEINFO *const reference = pEnc->reference; |
1542 |
|
MBParam * const pParam = &pEnc->mbParam; |
1543 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
int mb_width = pParam->mb_width; |
1544 |
IMAGE *pRef = &pEnc->reference->image; |
int mb_height = pParam->mb_height; |
1545 |
|
int coded = 1; |
|
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
mb_width = (pEnc->mbParam.width + 31) / 32; |
|
|
mb_height = (pEnc->mbParam.height + 31) / 32; |
|
|
} |
|
1546 |
|
|
1547 |
|
IMAGE *pRef = &reference->image; |
1548 |
|
|
1549 |
|
if (!reference->is_edged) { |
1550 |
start_timer(); |
start_timer(); |
1551 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pParam->edged_width, pParam->edged_height, |
1552 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pParam->width, pParam->height, 0); |
1553 |
stop_edges_timer(); |
stop_edges_timer(); |
1554 |
|
reference->is_edged = 1; |
1555 |
|
} |
1556 |
|
|
1557 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pParam->m_rounding_type = 1 - pParam->m_rounding_type; |
1558 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
current->rounding_type = pParam->m_rounding_type; |
1559 |
/* pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; */ |
current->fcode = pParam->m_fcode; |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
|
|
|
|
|
if (!force_inter) |
|
|
iLimit = (int)(mb_width * mb_height * INTRA_THRESHOLD); |
|
|
else |
|
|
iLimit = mb_width * mb_height + 1; |
|
1560 |
|
|
1561 |
if ((pEnc->current->vop_flags & XVID_VOP_HALFPEL)) { |
if ((current->vop_flags & XVID_VOP_HALFPEL)) { |
1562 |
|
if (reference->is_interpolated != current->rounding_type) { |
1563 |
start_timer(); |
start_timer(); |
1564 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1565 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pParam->edged_width, |
1566 |
pEnc->mbParam.edged_height, |
pParam->edged_height, |
1567 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), |
(pParam->vol_flags & XVID_VOL_QUARTERPEL), |
1568 |
pEnc->current->rounding_type); |
current->rounding_type); |
1569 |
stop_inter_timer(); |
stop_inter_timer(); |
1570 |
|
reference->is_interpolated = current->rounding_type; |
1571 |
|
} |
1572 |
} |
} |
1573 |
|
|
1574 |
pEnc->current->coding_type = P_VOP; |
current->sStat.iTextBits = current->sStat.iMvSum = current->sStat.iMvCount = |
1575 |
|
current->sStat.kblks = current->sStat.mblks = current->sStat.ublks = 0; |
|
|
|
|
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
|
1576 |
|
|
1577 |
start_timer(); |
current->coding_type = P_VOP; |
|
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
|
|
HintedMESet(pEnc, &bIntra); |
|
|
else*/ |
|
|
bIntra = |
|
|
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
|
iLimit); |
|
1578 |
|
|
1579 |
stop_motion_timer(); |
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1580 |
|
|
1581 |
if (bIntra == 1) return FrameCodeI(pEnc, bs); |
SetMacroblockQuants(&pEnc->mbParam, current); |
1582 |
|
|
1583 |
if ( ( pEnc->current->vol_flags & XVID_VOL_GMC ) |
start_timer(); |
1584 |
&& ( (pEnc->current->warp.duv[1].x != 0) || (pEnc->current->warp.duv[1].y != 0) ) ) |
if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */ |
1585 |
{ |
{ int gmcval; |
1586 |
pEnc->current->coding_type = S_VOP; |
current->warp = GlobalMotionEst( current->mbs, pParam, current, reference, |
1587 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1588 |
|
|
1589 |
|
if (current->motion_flags & XVID_ME_GME_REFINE) { |
1590 |
|
gmcval = GlobalMotionEstRefine(¤t->warp, |
1591 |
|
current->mbs, pParam, |
1592 |
|
current, reference, |
1593 |
|
¤t->image, |
1594 |
|
&reference->image, |
1595 |
|
&pEnc->vInterH, |
1596 |
|
&pEnc->vInterV, |
1597 |
|
&pEnc->vInterHV); |
1598 |
|
} else { |
1599 |
|
gmcval = globalSAD(¤t->warp, pParam, current->mbs, |
1600 |
|
current, |
1601 |
|
&reference->image, |
1602 |
|
¤t->image, |
1603 |
|
pEnc->vGMC.y); |
1604 |
|
} |
1605 |
|
|
1606 |
|
gmcval += /*current->quant*/ 2 * (int)(pParam->mb_width*pParam->mb_height); |
1607 |
|
|
1608 |
|
/* 1st '3': 3 warpoints, 2nd '3': 16th pel res (2<<3) */ |
1609 |
|
generate_GMCparameters( 3, 3, ¤t->warp, |
1610 |
|
pParam->width, pParam->height, |
1611 |
|
¤t->new_gmc_data); |
1612 |
|
|
1613 |
|
if ( (gmcval<0) && ( (current->warp.duv[1].x != 0) || (current->warp.duv[1].y != 0) || |
1614 |
|
(current->warp.duv[2].x != 0) || (current->warp.duv[2].y != 0) ) ) |
1615 |
|
{ |
1616 |
|
current->coding_type = S_VOP; |
1617 |
|
|
1618 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1619 |
|
pParam->mb_width, pParam->mb_height, |
1620 |
|
pParam->edged_width, pParam->edged_width/2, |
1621 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1622 |
|
current->rounding_type, current->mbs, &pEnc->vGMC); |
1623 |
|
|
1624 |
generate_GMCparameters( 2, 16, &pEnc->current->warp, |
} else { |
|
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); |
|
1625 |
|
|
1626 |
|
generate_GMCimage(¤t->new_gmc_data, &reference->image, |
1627 |
|
pParam->mb_width, pParam->mb_height, |
1628 |
|
pParam->edged_width, pParam->edged_width/2, |
1629 |
|
pParam->m_fcode, ((pParam->vol_flags & XVID_VOL_QUARTERPEL)?1:0), 0, |
1630 |
|
current->rounding_type, current->mbs, NULL); /* no warping, just AMV */ |
1631 |
} |
} |
|
|
|
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
|
|
if (vol_header) |
|
|
{ BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
|
|
BitstreamPadAlways(bs); |
|
1632 |
} |
} |
1633 |
|
|
1634 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
MotionEstimation(&pEnc->mbParam, current, reference, |
1635 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1636 |
|
&pEnc->vGMC, 256*4096); |
1637 |
|
|
|
pEnc->current->sStat.iTextBits = pEnc->current->sStat.iMvSum = pEnc->current->sStat.iMvCount = |
|
|
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = pEnc->current->sStat.ublks = 0; |
|
1638 |
|
|
1639 |
|
stop_motion_timer(); |
1640 |
|
|
1641 |
for (y = 0; y < mb_height; y++) { |
set_timecodes(current,reference,pParam->fbase); |
|
for (x = 0; x < mb_width; x++) { |
|
|
MACROBLOCK *pMB = |
|
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1642 |
|
|
1643 |
/* Mode decision: Check, if the block should be INTRA / INTER or GMC-coded */ |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 1, current->mbs[0].quant); |
|
/* For a start, leave INTRA decision as is, only choose only between INTER/GMC - gruel, 9.1.2002 */ |
|
1644 |
|
|
1645 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
for (y = 0; y < mb_height; y++) { |
1646 |
|
for (x = 0; x < mb_width; x++) { |
1647 |
|
MACROBLOCK *pMB = ¤t->mbs[x + y * pParam->mb_width]; |
1648 |
|
int skip_possible; |
1649 |
|
|
1650 |
if (bIntra) { |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
1651 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1652 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantIntra(&pEnc->mbParam, current, pMB, x, y, |
1653 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1654 |
|
|
1655 |
start_timer(); |
start_timer(); |
1656 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(current, x, y, pParam->mb_width, qcoeff); |
1657 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1658 |
|
|
1659 |
pEnc->current->sStat.kblks++; |
current->sStat.kblks++; |
1660 |
|
|
1661 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
MBCoding(current, pMB, qcoeff, bs, ¤t->sStat); |
1662 |
stop_coding_timer(); |
stop_coding_timer(); |
1663 |
continue; |
continue; |
1664 |
} |
} |
1665 |
|
|
|
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 */ |
|
|
} |
|
|
|
|
1666 |
start_timer(); |
start_timer(); |
1667 |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
MBMotionCompensation(pMB, x, y, &reference->image, |
1668 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
1669 |
&pEnc->vInterHV, &pEnc->vGMC, |
&pEnc->vInterHV, &pEnc->vGMC, |
1670 |
&pEnc->current->image, |
¤t->image, |
1671 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pParam->width, |
1672 |
pEnc->mbParam.height, |
pParam->height, |
1673 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1674 |
(pEnc->current->vol_flags & XVID_VOL_QUARTERPEL), |
(current->vol_flags & XVID_VOL_QUARTERPEL), |
1675 |
(pEnc->current->vop_flags & XVID_VOP_REDUCED), |
current->rounding_type); |
|
pEnc->current->rounding_type); |
|
1676 |
|
|
1677 |
stop_comp_timer(); |
stop_comp_timer(); |
1678 |
|
|
|
if (pMB->dquant != 0) { |
|
|
pMB->mode = MODE_INTER_Q; |
|
|
} |
|
|
|
|
1679 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1680 |
|
|
1681 |
if (pMB->mode != MODE_NOT_CODED) |
if (pMB->cbp != 0) { |
1682 |
{ pMB->cbp = |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, current, pMB, x, y, |
|
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
|
1683 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1684 |
} |
} |
1685 |
|
|
1686 |
|
if (pMB->dquant != 0) |
1687 |
|
MBSetDquant(pMB, x, y, &pEnc->mbParam); |
1688 |
|
|
1689 |
|
|
1690 |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1691 |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1692 |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
1693 |
pEnc->current->sStat.mblks++; |
current->sStat.mblks++; |
1694 |
} else { |
} else { |
1695 |
pEnc->current->sStat.ublks++; |
current->sStat.ublks++; |
1696 |
} |
} |
1697 |
|
|
1698 |
start_timer(); |
start_timer(); |
1699 |
|
|
1700 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1701 |
|
|
1702 |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER) && |
skip_possible = (pMB->cbp == 0) && (pMB->mode == MODE_INTER); |
|
(pMB->dquant == 0); |
|
1703 |
|
|
1704 |
if (pEnc->current->coding_type == S_VOP) |
if (current->coding_type == S_VOP) |
1705 |
skip_possible &= (pMB->mcsel == 1); |
skip_possible &= (pMB->mcsel == 1); |
1706 |
else if (pEnc->current->coding_type == P_VOP) { |
else { /* PVOP */ |
1707 |
if ((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) |
const VECTOR * const mv = (pParam->vol_flags & XVID_VOL_QUARTERPEL) ? |
1708 |
skip_possible &= ( (pMB->qmvs[0].x == 0) && (pMB->qmvs[0].y == 0) ); |
pMB->qmvs : pMB->mvs; |
1709 |
else |
skip_possible &= ((mv->x|mv->y) == 0); |
|
skip_possible &= ( (pMB->mvs[0].x == 0) && (pMB->mvs[0].y == 0) ); |
|
1710 |
} |
} |
1711 |
|
|
1712 |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
|
|
|
1713 |
/* 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 */ |
|
|
|
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
|
|
{ |
|
1714 |
int bSkip = 1; |
int bSkip = 1; |
1715 |
|
|
1716 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
if (current->coding_type == P_VOP) { /* special rule for P-VOP's SKIP */ |
|
{ |
|
|
int iSAD; |
|
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
|
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
|
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
|
|
{ bSkip = 0; |
|
|
break; |
|
|
} |
|
|
} |
|
1717 |
|
|
1718 |
if (!bSkip) { /* no SKIP, but trivial block */ |
for (k = pEnc->bframenum_head; k < pEnc->bframenum_tail; k++) { |
1719 |
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
int iSAD; |
1720 |
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
iSAD = sad16(reference->image.y + 16*y*pParam->edged_width + 16*x, |
1721 |
|
pEnc->bframes[k]->image.y + 16*y*pParam->edged_width + 16*x, |
1722 |
|
pParam->edged_width, BFRAME_SKIP_THRESHHOLD * pMB->quant); |
1723 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) { |
1724 |
|
bSkip = 0; /* could not SKIP */ |
1725 |
|
if (pParam->vol_flags & XVID_VOL_QUARTERPEL) { |
1726 |
|
VECTOR predMV = get_qpmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
1727 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1728 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1729 |
} |
} else { |
1730 |
else { |
VECTOR predMV = get_pmv2(current->mbs, pParam->mb_width, 0, x, y, 0); |
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
|
1731 |
pMB->pmvs[0].x = - predMV.x; |
pMB->pmvs[0].x = - predMV.x; |
1732 |
pMB->pmvs[0].y = - predMV.y; |
pMB->pmvs[0].y = - predMV.y; |
1733 |
} |
} |
1734 |
pMB->mode = MODE_INTER; |
pMB->mode = MODE_INTER; |
1735 |
pMB->cbp = 0; |
pMB->cbp = 0; |
1736 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
break; |
1737 |
stop_coding_timer(); |
} |
|
|
|
|
continue; /* next MB */ |
|
1738 |
} |
} |
1739 |
} |
} |
|
/* do SKIP */ |
|
1740 |
|
|
1741 |
|
if (bSkip) { |
1742 |
|
/* do SKIP */ |
1743 |
pMB->mode = MODE_NOT_CODED; |
pMB->mode = MODE_NOT_CODED; |
1744 |
MBSkip(bs); |
MBSkip(bs); |
1745 |
stop_coding_timer(); |
stop_coding_timer(); |
1746 |
continue; /* next MB */ |
continue; /* next MB */ |
1747 |
} |
} |
|
/* ordinary case: normal coded INTER/INTER4V block */ |
|
|
|
|
|
if ((pEnc->current->vop_flags & XVID_VOP_GREYSCALE)) |
|
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
|
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
|
|
qcoeff[5*64+0]=0; |
|
|
} |
|
|
|
|
|
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
|
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
|
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; |
|
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.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); |
|
|
} else { |
|
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
|
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; |
|
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.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); |
|
|
} |
|
|
|
|
|
|
|
|
if (pMB->mode == MODE_INTER4V) |
|
|
{ int k; |
|
|
for (k=1;k<4;k++) |
|
|
{ |
|
|
if((pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL)) { |
|
|
VECTOR predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
|
|
pMB->pmvs[k].x = pMB->qmvs[k].x - predMV.x; |
|
|
pMB->pmvs[k].y = pMB->qmvs[k].y - predMV.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); |
|
|
} else { |
|
|
VECTOR predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, k); |
|
|
pMB->pmvs[k].x = pMB->mvs[k].x - predMV.x; |
|
|
pMB->pmvs[k].y = pMB->mvs[k].y - predMV.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); |
|
1748 |
} |
} |
1749 |
|
|
1750 |
} |
/* ordinary case: normal coded INTER/INTER4V block */ |
1751 |
} |
MBCoding(current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
|
|
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->current->sStat); |
|
1752 |
stop_coding_timer(); |
stop_coding_timer(); |
|
|
|
|
} |
|
1753 |
} |
} |
|
|
|
|
if ((pEnc->current->vop_flags & XVID_VOP_REDUCED)) |
|
|
{ |
|
|
image_deblock_rrv(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
|
pEnc->current->mbs, mb_width, mb_height, pEnc->mbParam.mb_width, |
|
|
16, 0); |
|
1754 |
} |
} |
1755 |
|
|
1756 |
emms(); |
emms(); |
1757 |
|
updateFcode(¤t->sStat, pEnc); |
|
/* XXX: hinted me |
|
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
|
HintedMEGet(pEnc, 0); |
|
|
}*/ |
|
|
|
|
|
if (pEnc->current->sStat.iMvCount == 0) |
|
|
pEnc->current->sStat.iMvCount = 1; |
|
|
|
|
|
fSigma = (float) sqrt((float) pEnc->current->sStat.iMvSum / pEnc->current->sStat.iMvCount); |
|
|
|
|
|
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
|
|
|
|
|
if ((fSigma > iSearchRange / 3) |
|
|
&& (pEnc->mbParam.m_fcode <= (3 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* maximum search range 128 */ |
|
|
{ |
|
|
pEnc->mbParam.m_fcode++; |
|
|
iSearchRange *= 2; |
|
|
} else if ((fSigma < iSearchRange / 6) |
|
|
&& (pEnc->fMvPrevSigma >= 0) |
|
|
&& (pEnc->fMvPrevSigma < iSearchRange / 6) |
|
|
&& (pEnc->mbParam.m_fcode >= (2 + (pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL?1:0) ))) /* minimum search range 16 */ |
|
|
{ |
|
|
pEnc->mbParam.m_fcode--; |
|
|
iSearchRange /= 2; |
|
|
} |
|
|
|
|
|
pEnc->fMvPrevSigma = fSigma; |
|
1758 |
|
|
1759 |
/* frame drop code */ |
/* frame drop code */ |
1760 |
#if 0 |
#if 0 |
1761 |
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); |
1762 |
#endif |
#endif |
1763 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (current->sStat.kblks + current->sStat.mblks <= |
1764 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pParam->frame_drop_ratio * mb_width * mb_height) / 100 && |
1765 |
|
( (pEnc->bframenum_head >= pEnc->bframenum_tail) || !(pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP)) ) |
1766 |
{ |
{ |
1767 |
pEnc->current->sStat.kblks = pEnc->current->sStat.mblks = 0; |
current->sStat.kblks = current->sStat.mblks = 0; |
1768 |
pEnc->current->sStat.ublks = mb_width * mb_height; |
current->sStat.ublks = mb_width * mb_height; |
1769 |
|
|
1770 |
BitstreamReset(bs); |
BitstreamReset(bs); |
1771 |
|
|
1772 |
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(current,reference,pParam->fbase); |
1773 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, current, 0, current->mbs[0].quant); |
1774 |
|
|
1775 |
/* copy reference frame details into the current frame */ |
/* copy reference frame details into the current frame */ |
1776 |
pEnc->current->quant = pEnc->reference->quant; |
current->quant = reference->quant; |
1777 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
current->motion_flags = reference->motion_flags; |
1778 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
current->rounding_type = reference->rounding_type; |
1779 |
/* pEnc->current->quarterpel = pEnc->reference->quarterpel; */ |
current->fcode = reference->fcode; |
1780 |
pEnc->current->fcode = pEnc->reference->fcode; |
current->bcode = reference->bcode; |
1781 |
pEnc->current->bcode = pEnc->reference->bcode; |
current->stamp = reference->stamp; |
1782 |
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); |
1783 |
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
memcpy(current->mbs, reference->mbs, sizeof(MACROBLOCK) * mb_width * mb_height); |
1784 |
|
coded = 0; |
1785 |
|
|
1786 |
|
} else { |
1787 |
|
|
1788 |
|
pEnc->current->is_edged = 0; /* not edged */ |
1789 |
|
pEnc->current->is_interpolated = -1; /* not interpolated (fake rounding -1) */ |
1790 |
|
|
1791 |
|
/* what was this frame's interpolated reference will become |
1792 |
|
forward (past) reference in b-frame coding */ |
1793 |
|
|
1794 |
|
image_swap(&pEnc->vInterH, &pEnc->f_refh); |
1795 |
|
image_swap(&pEnc->vInterV, &pEnc->f_refv); |
1796 |
|
image_swap(&pEnc->vInterHV, &pEnc->f_refhv); |
1797 |
} |
} |
1798 |
|
|
1799 |
/* XXX: debug |
/* XXX: debug |
1800 |
{ |
{ |
1801 |
char s[100]; |
char s[100]; |
1802 |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_cur.pgm", pEnc->m_framenum); |
1803 |
image_dump_yuvpgm(&pEnc->current->image, |
image_dump_yuvpgm(¤t->image, |
1804 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1805 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1806 |
|
|
1807 |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
sprintf(s, "\\%05i_ref.pgm", pEnc->m_framenum); |
1808 |
image_dump_yuvpgm(&pEnc->reference->image, |
image_dump_yuvpgm(&reference->image, |
1809 |
pEnc->mbParam.edged_width, |
pParam->edged_width, |
1810 |
pEnc->mbParam.width, pEnc->mbParam.height, s); |
pParam->width, pParam->height, s); |
1811 |
} |
} |
1812 |
*/ |
*/ |
1813 |
|
|
1814 |
/* 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 |
|
1815 |
|
|
1816 |
pEnc->current->length = (BitstreamPos(bs) - bits) / 8; |
current->length = (BitstreamPos(bs) - bits) / 8; |
1817 |
|
|
1818 |
return 0; /* inter */ |
return coded; |
1819 |
} |
} |
1820 |
|
|
1821 |
|
|
1839 |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1840 |
} |
} |
1841 |
|
|
|
/* XXX: pEnc->current->global_flags &= ~XVID_VOP_REDUCED; reduced resoltion not yet supported */ |
|
|
|
|
1842 |
if (!first){ |
if (!first){ |
1843 |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1844 |
} |
} |
1845 |
#endif |
#endif |
1846 |
|
|
|
/* frame->quarterpel = pEnc->mbParam.m_quarterpel; */ |
|
|
|
|
1847 |
/* forward */ |
/* forward */ |
1848 |
|
if (!pEnc->reference->is_edged) { |
1849 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1850 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1851 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1852 |
|
pEnc->current->is_edged = 1; |
1853 |
|
} |
1854 |
|
|
1855 |
|
if (pEnc->reference->is_interpolated != 0) { |
1856 |
start_timer(); |
start_timer(); |
1857 |
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, |
1858 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1859 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1860 |
stop_inter_timer(); |
stop_inter_timer(); |
1861 |
|
pEnc->reference->is_interpolated = 0; |
1862 |
|
} |
1863 |
|
|
1864 |
/* backward */ |
/* backward */ |
1865 |
|
if (!pEnc->current->is_edged) { |
1866 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1867 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1868 |
pEnc->mbParam.height); |
pEnc->mbParam.height, 0); |
1869 |
|
pEnc->current->is_edged = 1; |
1870 |
|
} |
1871 |
|
|
1872 |
|
if (pEnc->current->is_interpolated != 0) { |
1873 |
start_timer(); |
start_timer(); |
1874 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1875 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1876 |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
(pEnc->mbParam.vol_flags & XVID_VOL_QUARTERPEL), 0); |
1877 |
stop_inter_timer(); |
stop_inter_timer(); |
1878 |
|
pEnc->current->is_interpolated = 0; |
1879 |
|
} |
1880 |
|
|
1881 |
start_timer(); |
frame->coding_type = B_VOP; |
1882 |
|
call_plugins(pEnc, pEnc->current, NULL, XVID_PLG_FRAME, NULL, NULL, NULL); |
1883 |
|
|
1884 |
|
start_timer(); |
1885 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1886 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
1887 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
1889 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1890 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
1891 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
|
1892 |
stop_motion_timer(); |
stop_motion_timer(); |
1893 |
|
|
|
/* |
|
|
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; |
|
|
|
|
1894 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1895 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1, frame->quant); |
1896 |
|
|
1897 |
frame->sStat.iTextBits = 0; |
frame->sStat.iTextBits = 0; |
1898 |
frame->sStat.iMvSum = 0; |
frame->sStat.iMvSum = 0; |
1904 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1905 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1906 |
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; |
|
1907 |
|
|
1908 |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
/* decoder ignores mb when refence block is INTER(0,0), CBP=0 */ |
1909 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1910 |
/* mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; */ |
if (pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1911 |
|
MBMotionCompensation(mb, x, y, f_ref, NULL, f_ref, NULL, NULL, &frame->image, |
1912 |
|
NULL, 0, 0, pEnc->mbParam.edged_width, 0, 0); |
1913 |
|
} |
1914 |
continue; |
continue; |
1915 |
} |
} |
1916 |
|
|
1917 |
if (mb->mode != MODE_DIRECT_NONE_MV || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
mb->quant = frame->quant; |
1918 |
|
|
1919 |
|
if (mb->cbp != 0 || pEnc->mbParam.plugin_flags & XVID_REQORIGINAL) { |
1920 |
|
/* we have to motion-compensate, transfer etc, |
1921 |
|
because there might be blocks to code */ |
1922 |
|
|
1923 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1924 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1925 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1926 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1927 |
dct_codes); |
dct_codes); |
1928 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
|
mb->quant = frame->quant; |
|
|
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) |
|
1929 |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
1930 |
|
} |
1931 |
|
|
1932 |
|
if (mb->mode == MODE_DIRECT_NO4V) |
1933 |
|
mb->mode = MODE_DIRECT; |
1934 |
|
|
1935 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if (mb->mode == MODE_DIRECT && (mb->cbp | mb->pmvs[3].x | mb->pmvs[3].y) == 0) |
|
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
|
1936 |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
mb->mode = MODE_DIRECT_NONE_MV; /* skipped */ |
1937 |
} |
else |
1938 |
} |
if (frame->vop_flags & XVID_VOP_GREYSCALE) |
1939 |
|
/* keep only bits 5-2 -- Chroma blocks will just be skipped by MBCodingBVOP */ |
1940 |
|
mb->cbp &= 0x3C; |
1941 |
|
|
|
#ifdef BFRAMES_DEC_DEBUG |
|
|
BFRAME_DEBUG |
|
|
#endif |
|
1942 |
start_timer(); |
start_timer(); |
1943 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(frame, mb, qcoeff, frame->fcode, frame->bcode, bs, |
1944 |
&frame->sStat, direction); |
&frame->sStat); |
1945 |
stop_coding_timer(); |
stop_coding_timer(); |
1946 |
} |
} |
1947 |
} |
} |
1948 |
|
|
1949 |
emms(); |
emms(); |
1950 |
|
|
1951 |
/* TODO: dynamic fcode/bcode ??? */ |
BitstreamPadAlways(bs); /* next_start_code() at the end of VideoObjectPlane() */ |
|
|
|
|
BitstreamPadAlways(bs); |
|
1952 |
frame->length = (BitstreamPos(bs) - bits) / 8; |
frame->length = (BitstreamPos(bs) - bits) / 8; |
1953 |
|
|
1954 |
#ifdef BFRAMES_DEC_DEBUG |
#ifdef BFRAMES_DEC_DEBUG |