32 |
* |
* |
33 |
* History |
* History |
34 |
* |
* |
35 |
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 20.06.2002 bframe patch |
38 |
* 08.05.2002 fix some problem in DEBUG mode; |
* 08.05.2002 fix some problem in DEBUG mode; |
39 |
* MinChen <chenm001@163.com> |
* MinChen <chenm001@163.com> |
40 |
* 14.04.2002 added FrameCodeB() |
* 14.04.2002 added FrameCodeB() |
41 |
* |
* |
42 |
* $Id: encoder.c,v 1.43 2002-06-14 13:21:35 Isibaar Exp $ |
* $Id: encoder.c,v 1.76.2.15 2002-11-02 16:12:27 chl Exp $ |
43 |
* |
* |
44 |
****************************************************************************/ |
****************************************************************************/ |
45 |
|
|
46 |
|
|
47 |
#include <stdlib.h> |
#include <stdlib.h> |
48 |
#include <stdio.h> |
#include <stdio.h> |
49 |
#include <math.h> |
#include <math.h> |
54 |
#include "global.h" |
#include "global.h" |
55 |
#include "utils/timer.h" |
#include "utils/timer.h" |
56 |
#include "image/image.h" |
#include "image/image.h" |
57 |
|
#include "image/font.h" |
58 |
|
#include "motion/sad.h" |
59 |
#include "motion/motion.h" |
#include "motion/motion.h" |
60 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
61 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
89 |
bool force_inter, |
bool force_inter, |
90 |
bool vol_header); |
bool vol_header); |
91 |
|
|
|
#ifdef BFRAMES |
|
92 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
93 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
94 |
Bitstream * bs, |
Bitstream * bs, |
95 |
uint32_t * pBits); |
uint32_t * pBits); |
|
#endif |
|
96 |
|
|
97 |
/***************************************************************************** |
/***************************************************************************** |
98 |
* Local data |
* Local data |
136 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
137 |
{ |
{ |
138 |
Encoder *pEnc; |
Encoder *pEnc; |
139 |
uint32_t i; |
int i; |
140 |
|
|
141 |
pParam->handle = NULL; |
pParam->handle = NULL; |
142 |
|
|
204 |
|
|
205 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
206 |
|
|
207 |
if (pParam->max_key_interval == 0) |
if (pParam->max_key_interval <= 0) |
208 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
209 |
|
|
|
|
|
210 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
211 |
if (pEnc == NULL) |
if (pEnc == NULL) |
212 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
262 |
|
|
263 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
264 |
|
|
265 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
266 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
267 |
#endif |
#endif |
268 |
#ifdef BFRAMES |
|
269 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
270 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
271 |
image_null(&pEnc->f_refhv); |
image_null(&pEnc->f_refhv); |
272 |
#endif |
|
273 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
274 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
275 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
278 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
279 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
280 |
|
|
281 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
282 |
if (image_create |
if (image_create |
283 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
284 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
285 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
286 |
#endif |
#endif |
287 |
#ifdef BFRAMES |
|
288 |
if (image_create |
if (image_create |
289 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
290 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
297 |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
298 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
299 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
300 |
#endif |
|
301 |
if (image_create |
if (image_create |
302 |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
303 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
330 |
|
|
331 |
|
|
332 |
/* B Frames specific init */ |
/* B Frames specific init */ |
|
#ifdef BFRAMES |
|
333 |
|
|
334 |
|
pEnc->global = pParam->global; |
335 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
336 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
337 |
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
338 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
339 |
|
|
340 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
377 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
378 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
379 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
380 |
|
pEnc->bframenum_dx50bvop = -1; |
381 |
|
|
382 |
pEnc->mbParam.m_seconds = 0; |
pEnc->queue = NULL; |
383 |
pEnc->mbParam.m_ticks = 0; |
|
384 |
#endif |
|
385 |
|
if (pEnc->mbParam.max_bframes > 0) { |
386 |
|
int n; |
387 |
|
|
388 |
|
pEnc->queue = |
389 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
390 |
|
CACHE_LINE); |
391 |
|
|
392 |
|
if (pEnc->queue == NULL) |
393 |
|
goto xvid_err_memory4; |
394 |
|
|
395 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
396 |
|
image_null(&pEnc->queue[n]); |
397 |
|
|
398 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
399 |
|
if (image_create |
400 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
401 |
|
pEnc->mbParam.edged_height) < 0) |
402 |
|
goto xvid_err_memory5; |
403 |
|
|
404 |
|
} |
405 |
|
} |
406 |
|
|
407 |
|
pEnc->queue_head = 0; |
408 |
|
pEnc->queue_tail = 0; |
409 |
|
pEnc->queue_size = 0; |
410 |
|
|
411 |
|
pEnc->mbParam.m_stamp = 0; |
412 |
|
|
413 |
|
pEnc->m_framenum = 0; |
414 |
|
pEnc->current->stamp = 0; |
415 |
|
pEnc->reference->stamp = 0; |
416 |
|
|
417 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
418 |
|
|
431 |
/* |
/* |
432 |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
* We handle all XVID_ERR_MEMORY here, this makes the code lighter |
433 |
*/ |
*/ |
434 |
#ifdef BFRAMES |
|
435 |
|
xvid_err_memory5: |
436 |
|
|
437 |
|
|
438 |
|
if (pEnc->mbParam.max_bframes > 0) { |
439 |
|
|
440 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
441 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
442 |
|
pEnc->mbParam.edged_height); |
443 |
|
} |
444 |
|
xvid_free(pEnc->queue); |
445 |
|
} |
446 |
|
|
447 |
xvid_err_memory4: |
xvid_err_memory4: |
448 |
|
|
449 |
|
if (pEnc->mbParam.max_bframes > 0) { |
450 |
|
|
451 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
452 |
|
|
453 |
if (pEnc->bframes[i] == NULL) |
if (pEnc->bframes[i] == NULL) |
463 |
} |
} |
464 |
|
|
465 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
466 |
|
} |
|
#endif |
|
467 |
|
|
468 |
xvid_err_memory3: |
xvid_err_memory3: |
469 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
470 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
471 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
472 |
#endif |
#endif |
473 |
|
|
|
#ifdef BFRAMES |
|
474 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
475 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
476 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
477 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
478 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
479 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
|
#endif |
|
480 |
|
|
481 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
482 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
521 |
int |
int |
522 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
523 |
{ |
{ |
524 |
|
int i; |
525 |
|
|
526 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
527 |
|
|
528 |
/* B Frames specific */ |
/* B Frames specific */ |
529 |
#ifdef BFRAMES |
if (pEnc->mbParam.max_bframes > 0) { |
530 |
int i; |
|
531 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
532 |
|
|
533 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
534 |
|
pEnc->mbParam.edged_height); |
535 |
|
} |
536 |
|
xvid_free(pEnc->queue); |
537 |
|
} |
538 |
|
|
539 |
|
|
540 |
|
if (pEnc->mbParam.max_bframes > 0) { |
541 |
|
|
542 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
543 |
|
|
550 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
551 |
|
|
552 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
553 |
} |
} |
554 |
|
|
555 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
556 |
|
|
557 |
#endif |
} |
558 |
|
|
559 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
560 |
|
|
572 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
573 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
574 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
575 |
#ifdef BFRAMES |
|
576 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
577 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
578 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
579 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
580 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
581 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
582 |
#endif |
|
583 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
584 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
585 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
586 |
#endif |
#endif |
598 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
599 |
} |
} |
600 |
|
|
601 |
|
|
602 |
|
static __inline void inc_frame_num(Encoder * pEnc) |
603 |
|
{ |
604 |
|
pEnc->current->stamp = pEnc->mbParam.m_stamp; // first frame is zero |
605 |
|
pEnc->mbParam.m_stamp += pEnc->mbParam.fincr; |
606 |
|
} |
607 |
|
|
608 |
|
|
609 |
|
static __inline void |
610 |
|
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
611 |
|
{ |
612 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
613 |
|
{ |
614 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
615 |
|
return; |
616 |
|
} |
617 |
|
|
618 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
619 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
620 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
621 |
|
|
622 |
|
|
623 |
|
start_timer(); |
624 |
|
if (image_input |
625 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
626 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
627 |
|
return; |
628 |
|
stop_conv_timer(); |
629 |
|
|
630 |
|
pEnc->queue_size++; |
631 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
632 |
|
} |
633 |
|
|
634 |
|
static __inline void |
635 |
|
set_timecodes(FRAMEINFO* pCur,FRAMEINFO *pRef, int32_t time_base) |
636 |
|
{ |
637 |
|
|
638 |
|
pCur->ticks = (int32_t)pCur->stamp % time_base; |
639 |
|
pCur->seconds = ((int32_t)pCur->stamp / time_base) - ((int32_t)pRef->stamp / time_base) ; |
640 |
|
|
641 |
|
//HEAVY DEBUG OUTPUT remove when timecodes prove to be stable |
642 |
|
|
643 |
|
/* fprintf(stderr,"WriteVop: %d - %d \n", |
644 |
|
((int32_t)pCur->stamp / time_base), ((int32_t)pRef->stamp / time_base)); |
645 |
|
fprintf(stderr,"set_timecodes: VOP %1d stamp=%lld ref_stamp=%lld base=%d\n", |
646 |
|
pCur->coding_type, pCur->stamp, pRef->stamp, time_base); |
647 |
|
fprintf(stderr,"set_timecodes: VOP %1d seconds=%d ticks=%d (ref-sec=%d ref-tick=%d)\n", |
648 |
|
pCur->coding_type, pCur->seconds, pCur->ticks, pRef->seconds, pRef->ticks); |
649 |
|
|
650 |
|
*/ |
651 |
|
} |
652 |
|
|
653 |
|
|
654 |
|
|
655 |
|
|
656 |
|
|
657 |
/***************************************************************************** |
/***************************************************************************** |
658 |
* Frame encoder entry point |
* IPB frame encoder entry point |
|
* |
|
|
* At this moment 2 versions coexist : one for IPB compatible encoder, |
|
|
* another one for the old IP encoder. |
|
659 |
* |
* |
660 |
* Returned values : |
* Returned values : |
661 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
663 |
* format |
* format |
664 |
****************************************************************************/ |
****************************************************************************/ |
665 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
666 |
int |
int |
667 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
668 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
669 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
670 |
{ |
{ |
671 |
uint16_t x, y; |
uint16_t x, y; |
672 |
Bitstream bs; |
Bitstream bs; |
673 |
uint32_t bits; |
uint32_t bits, mode; |
674 |
|
|
675 |
|
int input_valid = 1; |
676 |
|
|
677 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
678 |
float psnr; |
float psnr; |
679 |
char temp[128]; |
char temp[128]; |
680 |
#endif |
#endif |
681 |
|
|
682 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
683 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
684 |
|
ENC_CHECK(pFrame->image); |
685 |
|
|
686 |
start_global_timer(); |
start_global_timer(); |
687 |
|
|
688 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
689 |
|
|
690 |
|
ipvop_loop: |
691 |
|
|
692 |
/* |
/* |
693 |
* bframe "flush" code |
* bframe "flush" code |
694 |
*/ |
*/ |
703 |
* frame as a pframe |
* frame as a pframe |
704 |
*/ |
*/ |
705 |
|
|
706 |
/* ToDo : remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
707 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
708 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
709 |
*/ |
|
710 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
711 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
712 |
|
|
716 |
|
|
717 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
718 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
719 |
pFrame->intra = 0; |
pFrame->intra = 0; |
720 |
|
|
721 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
722 |
} |
} |
723 |
|
|
724 |
/* ToDo : remove dprintf calls */ |
|
725 |
/* |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
726 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
727 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
728 |
|
|
729 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
730 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
731 |
|
|
|
|
|
732 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
733 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
734 |
pFrame->intra = 0; |
pFrame->intra = 0; |
735 |
|
|
736 |
|
if (input_valid) |
737 |
|
queue_image(pEnc, pFrame); |
738 |
|
|
739 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
740 |
} |
} |
741 |
|
|
742 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
743 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
744 |
pFrame->input_consumed = 1; |
|
745 |
|
/* write an empty marker to the bitstream. |
746 |
|
|
747 |
|
for divx5 decoder compatibility, this marker must consist |
748 |
|
of a not-coded p-vop, with a time_base of zero, and time_increment |
749 |
|
indentical to the future-referece frame. |
750 |
|
*/ |
751 |
|
|
752 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
753 |
|
int tmp; |
754 |
|
|
755 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
756 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
757 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
758 |
|
|
759 |
|
BitstreamPad(&bs); |
760 |
|
|
761 |
|
tmp = pEnc->current->seconds; |
762 |
|
pEnc->current->seconds = 0; /* force time_base = 0 */ |
763 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
764 |
|
pEnc->current->seconds = tmp; |
765 |
|
|
766 |
|
pFrame->length = BitstreamLength(&bs); |
767 |
pFrame->intra = 0; |
pFrame->intra = 0; |
768 |
|
|
769 |
|
if (input_valid) |
770 |
|
queue_image(pEnc, pFrame); |
771 |
|
|
772 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
773 |
} |
} |
774 |
|
} |
775 |
|
|
776 |
if (pEnc->bframenum_head > 0) { |
|
777 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
778 |
|
|
779 |
|
if (pEnc->bframenum_dx50bvop != -1) |
780 |
|
{ |
781 |
|
|
782 |
|
SWAP(pEnc->current, pEnc->reference); |
783 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
784 |
|
|
785 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
786 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
787 |
} |
} |
788 |
|
|
789 |
pEnc->flush_bframes = 0; |
if (input_valid) |
790 |
|
{ |
791 |
|
queue_image(pEnc, pFrame); |
792 |
|
input_valid = 0; |
793 |
|
} |
794 |
|
|
795 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
} else if (input_valid) { |
|
* Well there was a separation here so i put it in ANSI C |
|
|
* comment style :-) |
|
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
|
796 |
|
|
797 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
798 |
|
|
799 |
|
start_timer(); |
800 |
|
if (image_input |
801 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
802 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
803 |
|
return XVID_ERR_FORMAT; |
804 |
|
stop_conv_timer(); |
805 |
|
|
806 |
|
// queue input frame, and dequue next image |
807 |
|
if (pEnc->queue_size > 0) |
808 |
|
{ |
809 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
810 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
811 |
|
{ |
812 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
813 |
|
} |
814 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
815 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
816 |
|
} |
817 |
|
|
818 |
|
} else if (pEnc->queue_size > 0) { |
819 |
|
|
820 |
|
SWAP(pEnc->current, pEnc->reference); |
821 |
|
|
822 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
823 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
824 |
|
pEnc->queue_size--; |
825 |
|
|
826 |
|
} else { |
827 |
|
|
828 |
|
/* if nothing was encoded, write an 'ignore this frame' flag |
829 |
|
to the bitstream */ |
830 |
|
|
831 |
|
if (BitstreamPos(&bs) == 0) { |
832 |
|
|
833 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
834 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
835 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
836 |
|
|
837 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
838 |
|
pFrame->intra = 0; |
839 |
|
} |
840 |
|
|
841 |
|
pFrame->length = BitstreamLength(&bs); |
842 |
|
return XVID_ERR_OK; |
843 |
|
} |
844 |
|
|
845 |
|
pEnc->flush_bframes = 0; |
846 |
|
|
847 |
emms(); |
emms(); |
848 |
|
|
849 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
850 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
851 |
|
{ |
852 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
853 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
854 |
else |
else |
855 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
856 |
|
|
857 |
if (pEnc->current->quant < 1) |
/* if (pEnc->current->quant < 1) |
858 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
859 |
|
|
860 |
if (pEnc->current->quant > 31) |
if (pEnc->current->quant > 31) |
861 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
862 |
|
*/ |
863 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
864 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
865 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
866 |
/* ToDo : dynamic fcode (in both directions) */ |
/* ToDo : dynamic fcode (in both directions) */ |
867 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
868 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
869 |
|
|
870 |
start_timer(); |
inc_frame_num(pEnc); |
|
if (image_input |
|
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
|
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
871 |
|
|
872 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
873 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
874 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
875 |
#endif |
#endif |
876 |
|
|
877 |
emms(); |
emms(); |
878 |
|
|
879 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
880 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
881 |
|
"%i if:%i st:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->stamp); |
882 |
|
} |
883 |
|
|
884 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
885 |
* Luminance masking |
* Luminance masking |
886 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
910 |
} |
} |
911 |
|
|
912 |
#undef OFFSET |
#undef OFFSET |
|
|
|
913 |
} |
} |
914 |
|
|
915 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
916 |
} |
} |
917 |
|
|
918 |
|
} |
919 |
|
|
920 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
921 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
922 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
923 |
|
|
924 |
|
|
925 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
926 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
927 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
928 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| /*image_mad(&pEnc->reference->image, &pEnc->current->image, |
929 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
930 |
pEnc->mbParam.height) > 30) { |
pEnc->mbParam.height) > 30) {*/ |
931 |
|
2 == (mode = MEanalysis(&pEnc->reference->image, &pEnc->current->image, |
932 |
|
&pEnc->mbParam, pEnc->current->mbs, pEnc->current->fcode))) { |
933 |
|
|
934 |
/* |
/* |
935 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
936 |
*/ |
*/ |
937 |
|
|
938 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
939 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
940 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
*/ |
|
941 |
|
|
942 |
FrameCodeI(pEnc, &bs, &bits); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
943 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
944 |
|
} |
945 |
|
|
946 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
947 |
|
|
948 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
949 |
|
|
950 |
|
pEnc->bframenum_tail--; |
951 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
952 |
|
|
953 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
954 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
955 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
956 |
|
} |
957 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
958 |
|
|
959 |
|
pFrame->intra = 0; |
960 |
|
|
961 |
|
} else { |
962 |
|
|
963 |
|
FrameCodeI(pEnc, &bs, &bits); |
964 |
pFrame->intra = 1; |
pFrame->intra = 1; |
965 |
|
|
966 |
|
pEnc->bframenum_dx50bvop = -1; |
967 |
|
} |
968 |
|
|
969 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
970 |
|
|
971 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
972 |
|
BitstreamPadAlways(&bs); |
973 |
|
input_valid = 0; |
974 |
|
goto ipvop_loop; |
975 |
|
} |
976 |
|
|
977 |
/* |
/* |
978 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
979 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
980 |
*/ |
*/ |
981 |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes || mode != 0) { |
982 |
/* |
/* |
983 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
984 |
*/ |
*/ |
985 |
|
|
986 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
987 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
988 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
989 |
*/ |
|
990 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
991 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
992 |
|
} |
993 |
|
|
994 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
995 |
pFrame->intra = 0; |
pFrame->intra = 0; |
996 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
997 |
|
|
998 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
999 |
|
BitstreamPadAlways(&bs); |
1000 |
|
input_valid = 0; |
1001 |
|
goto ipvop_loop; |
1002 |
|
} |
1003 |
|
|
1004 |
} else { |
} else { |
1005 |
/* |
/* |
1006 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1007 |
*/ |
*/ |
1008 |
|
|
1009 |
/* ToDo : Remove dprintf calls */ |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1010 |
/* |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1011 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
} |
|
pEnc->bframenum_head, |
|
|
pEnc->bframenum_tail); |
|
|
*/ |
|
1012 |
|
|
1013 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1014 |
pEnc->current->quant = |
pEnc->current->quant = |
1017 |
} else { |
} else { |
1018 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1019 |
} |
} |
1020 |
|
if (pEnc->current->quant < 1) |
1021 |
|
pEnc->current->quant = 1; |
1022 |
|
|
1023 |
|
if (pEnc->current->quant > 31) |
1024 |
|
pEnc->current->quant = 31; |
1025 |
|
|
1026 |
|
|
1027 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1028 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1029 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1030 |
|
|
1031 |
|
|
1032 |
|
|
1033 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1034 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1038 |
|
|
1039 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1040 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
1041 |
|
|
1042 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
input_valid = 0; |
1043 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
goto bvop_loop; |
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
1044 |
} |
} |
1045 |
|
|
1046 |
return XVID_ERR_OK; |
pEnc->iFrameNum++; |
|
} |
|
1047 |
|
|
1048 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1049 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1058 |
|
|
1059 |
emms(); |
emms(); |
1060 |
|
|
1061 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1062 |
psnr = |
psnr = |
1063 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1064 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1073 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1074 |
} |
} |
1075 |
|
|
|
pEnc->iFrameNum++; |
|
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
|
|
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
|
} |
|
|
pFrame->input_consumed = 1; |
|
|
|
|
1076 |
stop_global_timer(); |
stop_global_timer(); |
1077 |
write_timer(); |
write_timer(); |
1078 |
|
|
1079 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1080 |
} |
} |
1081 |
#else |
|
1082 |
|
|
1083 |
|
|
1084 |
/***************************************************************************** |
/***************************************************************************** |
1085 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1086 |
|
* |
1087 |
|
* Returned values : |
1088 |
|
* - XVID_ERR_OK - no errors |
1089 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1090 |
|
* format |
1091 |
****************************************************************************/ |
****************************************************************************/ |
1092 |
|
|
1093 |
int |
int |
1094 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1095 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1100 |
uint32_t bits; |
uint32_t bits; |
1101 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1102 |
|
|
1103 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1104 |
float psnr; |
float psnr; |
1105 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1106 |
#endif |
#endif |
1118 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1119 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1120 |
|
|
1121 |
|
inc_frame_num(pEnc); |
1122 |
|
|
1123 |
|
/* disable alternate scan flag if interlacing is not enabled */ |
1124 |
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
1125 |
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
1126 |
|
{ |
1127 |
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
1128 |
|
} |
1129 |
|
|
1130 |
start_timer(); |
start_timer(); |
1131 |
if (image_input |
if (image_input |
1132 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
1134 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1135 |
stop_conv_timer(); |
stop_conv_timer(); |
1136 |
|
|
1137 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1138 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1139 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1140 |
#endif |
#endif |
1149 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
1150 |
} |
} |
1151 |
|
|
1152 |
|
if ((pEnc->current->global_flags & XVID_QUARTERPEL)) |
1153 |
|
pEnc->mbParam.m_quarterpel = 1; |
1154 |
|
else |
1155 |
|
pEnc->mbParam.m_quarterpel = 0; |
1156 |
|
|
1157 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1158 |
int *temp_dquants = |
int *temp_dquants = |
1159 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
1249 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1250 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1251 |
} |
} |
1252 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1253 |
psnr = |
psnr = |
1254 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1255 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1266 |
|
|
1267 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1268 |
} |
} |
|
#endif |
|
1269 |
|
|
1270 |
|
|
1271 |
static __inline void |
static __inline void |
1335 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1336 |
MVBLOCKHINT *bhint = |
MVBLOCKHINT *bhint = |
1337 |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1338 |
VECTOR pred[4]; |
VECTOR pred; |
1339 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1340 |
int vec; |
int vec; |
1341 |
|
|
1342 |
pMB->mode = |
pMB->mode = |
1356 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1357 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1358 |
|
|
1359 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,0); |
|
0, pred, dummy); |
|
1360 |
|
|
1361 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1362 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1363 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1364 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1365 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
1366 |
} |
} |
1367 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1368 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1375 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1376 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1377 |
|
|
1378 |
get_pmvdata(pEnc->current->mbs, x, y, |
pred = get_pmv2(pEnc->current->mbs,pEnc->mbParam.mb_width,0,x,y,vec); |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
|
1379 |
|
|
1380 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1381 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1382 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1383 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1384 |
} |
} |
1385 |
} else // intra / stuffing / not_coded |
} else // intra / stuffing / not_coded |
1386 |
{ |
{ |
1507 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
1508 |
pEnc->mbParam.m_rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
1509 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1510 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1511 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1512 |
|
|
1513 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1514 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
1515 |
|
#define DIVX501B481P "DivX501b481p" |
1516 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1517 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1518 |
|
} |
1519 |
|
|
1520 |
|
#define XVID_ID "XviD" XVID_BS_VERSION |
1521 |
|
BitstreamWriteUserData(bs, XVID_ID, strlen(XVID_ID)); |
1522 |
|
|
1523 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1524 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1525 |
|
|
1526 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1527 |
|
|
1544 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1545 |
|
|
1546 |
start_timer(); |
start_timer(); |
1547 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1548 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1549 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1550 |
|
qcoeff[5*64+0]=0; |
1551 |
|
} |
1552 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1553 |
stop_coding_timer(); |
stop_coding_timer(); |
1554 |
} |
} |
1570 |
|
|
1571 |
|
|
1572 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1573 |
|
#define BFRAME_SKIP_THRESHHOLD 30 |
1574 |
|
|
1575 |
static int |
static int |
1576 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1585 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1586 |
|
|
1587 |
int iLimit; |
int iLimit; |
1588 |
uint32_t x, y; |
int x, y, k; |
1589 |
int iSearchRange; |
int iSearchRange; |
1590 |
int bIntra; |
int bIntra, skip_possible; |
1591 |
|
|
1592 |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
/* IMAGE *pCurrent = &pEnc->current->image; */ |
1593 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1594 |
|
|
1595 |
start_timer(); |
start_timer(); |
1596 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1597 |
pEnc->mbParam.width, pEnc->mbParam.height, |
pEnc->mbParam.width, pEnc->mbParam.height); |
|
pEnc->current->global_flags & XVID_INTERLACING); |
|
1598 |
stop_edges_timer(); |
stop_edges_timer(); |
1599 |
|
|
1600 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1601 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
1602 |
|
pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
1603 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1604 |
|
|
1605 |
if (!force_inter) |
if (!force_inter) |
1614 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1615 |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1616 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
1617 |
|
pEnc->mbParam.m_quarterpel, |
1618 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1619 |
stop_inter_timer(); |
stop_inter_timer(); |
1620 |
} |
} |
1621 |
|
|
1622 |
|
if (pEnc->current->global_flags & XVID_GMC) { |
1623 |
|
// printf("Global Motion = %d %d quarterpel=%d\n", pEnc->current->GMC_MV.x, pEnc->current->GMC_MV.y,pEnc->current->quarterpel); |
1624 |
|
pEnc->current->coding_type = S_VOP; |
1625 |
|
} else |
1626 |
|
pEnc->current->coding_type = P_VOP; |
1627 |
|
|
1628 |
start_timer(); |
start_timer(); |
1629 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1630 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1631 |
|
if (bIntra == 0) { |
1632 |
|
pEnc->current->fcode = FindFcode(&pEnc->mbParam, pEnc->current); |
1633 |
|
MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1634 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
1635 |
|
} |
1636 |
|
|
1637 |
} else { |
} else { |
1638 |
|
|
1639 |
bIntra = |
bIntra = |
1640 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1641 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1643 |
} |
} |
1644 |
stop_motion_timer(); |
stop_motion_timer(); |
1645 |
|
|
1646 |
if (bIntra == 1) { |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
1647 |
return FrameCodeI(pEnc, bs, pBits); |
|
1648 |
} |
if ( (pEnc->current->GMC_MV.x == 0) && (pEnc->current->GMC_MV.y == 0) ) |
1649 |
|
pEnc->current->coding_type = P_VOP; /* no global motion -> no GMC */ |
1650 |
|
|
|
pEnc->current->coding_type = P_VOP; |
|
1651 |
|
|
1652 |
if (vol_header) |
if (vol_header) |
1653 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1654 |
|
|
1655 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
|
1656 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1657 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1658 |
|
|
1659 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1660 |
|
|
1661 |
pEnc->sStat.iTextBits = 0; |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
|
pEnc->sStat.iMvSum = 0; |
|
|
pEnc->sStat.iMvCount = 0; |
|
1662 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1663 |
|
|
1664 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1676 |
dct_codes, pEnc->mbParam.width, |
dct_codes, pEnc->mbParam.width, |
1677 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1678 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1679 |
|
pEnc->mbParam.m_quarterpel, |
1680 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1681 |
stop_comp_timer(); |
stop_comp_timer(); |
1682 |
|
|
1694 |
|
|
1695 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1696 |
|
|
1697 |
|
if (pMB->mode != MODE_NOT_CODED) |
1698 |
pMB->cbp = |
pMB->cbp = |
1699 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1700 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1719 |
} |
} |
1720 |
|
|
1721 |
start_timer(); |
start_timer(); |
1722 |
|
|
1723 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1724 |
|
|
1725 |
|
skip_possible = (pMB->cbp == 0) & (pMB->mode == MODE_INTER) & |
1726 |
|
(pMB->dquant == NO_CHANGE); |
1727 |
|
|
1728 |
|
if(pEnc->mbParam.m_quarterpel) |
1729 |
|
{ skip_possible &= (pMB->qmvs[0].x == pEnc->current->GMC_MV.x) & (pMB->qmvs[0].y == pEnc->current->GMC_MV.y); |
1730 |
|
} |
1731 |
|
else |
1732 |
|
{ skip_possible &= (pMB->mvs[0].x == pEnc->current->GMC_MV.x) & (pMB->mvs[0].y == pEnc->current->GMC_MV.y); |
1733 |
|
} |
1734 |
|
|
1735 |
|
if ( (pMB->mode == MODE_NOT_CODED) || (skip_possible)) { |
1736 |
|
|
1737 |
|
/* This is a candidate for SKIPping, but for P-VOPs check intermediate B-frames first */ |
1738 |
|
int bSkip = 1; |
1739 |
|
|
1740 |
|
if (pEnc->current->coding_type == P_VOP) /* special rule for P-VOP's SKIP */ |
1741 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1742 |
|
{ |
1743 |
|
int iSAD; |
1744 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1745 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1746 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1747 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
1748 |
|
{ bSkip = 0; |
1749 |
|
break; |
1750 |
|
} |
1751 |
|
} |
1752 |
|
|
1753 |
|
if (!bSkip) |
1754 |
|
{ |
1755 |
|
VECTOR predMV; |
1756 |
|
if(pEnc->mbParam.m_quarterpel) { |
1757 |
|
predMV = get_qpmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1758 |
|
pMB->pmvs[0].x = pMB->qmvs[0].x - predMV.x; /* with GMC, qmvs doesn't have to be (0,0)! */ |
1759 |
|
pMB->pmvs[0].y = pMB->qmvs[0].y - predMV.y; |
1760 |
|
} |
1761 |
|
else { |
1762 |
|
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
1763 |
|
pMB->pmvs[0].x = pMB->mvs[0].x - predMV.x; /* with GMC, mvs doesn't have to be (0,0)! */ |
1764 |
|
pMB->pmvs[0].y = pMB->mvs[0].y - predMV.y; |
1765 |
|
} |
1766 |
|
pMB->mode = MODE_INTER; |
1767 |
|
pMB->cbp = 0; |
1768 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1769 |
|
} |
1770 |
|
else |
1771 |
|
{ |
1772 |
|
pMB->mode = MODE_NOT_CODED; |
1773 |
|
MBSkip(bs); |
1774 |
|
} |
1775 |
|
|
1776 |
|
} else { |
1777 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1778 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1779 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1780 |
|
qcoeff[5*64+0]=0; |
1781 |
|
} |
1782 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1783 |
|
} |
1784 |
|
|
1785 |
stop_coding_timer(); |
stop_coding_timer(); |
1786 |
} |
} |
1787 |
} |
} |
1800 |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
1801 |
|
|
1802 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
1803 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= (3 + pEnc->mbParam.m_quarterpel))) // maximum search range 128 |
1804 |
{ |
{ |
1805 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1806 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1807 |
} else if ((fSigma < iSearchRange / 6) |
} else if ((fSigma < iSearchRange / 6) |
1808 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1809 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1810 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= (2 + pEnc->mbParam.m_quarterpel))) // minimum search range 16 |
1811 |
{ |
{ |
1812 |
pEnc->mbParam.m_fcode--; |
pEnc->mbParam.m_fcode--; |
1813 |
iSearchRange /= 2; |
iSearchRange /= 2; |
1815 |
|
|
1816 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1817 |
|
|
1818 |
|
#ifdef FRAMEDROP |
1819 |
|
/* frame drop code */ |
1820 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1821 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1822 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1823 |
|
{ |
1824 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1825 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1826 |
|
|
1827 |
|
BitstreamReset(bs); |
1828 |
|
|
1829 |
|
set_timecodes(pEnc->current,pEnc->reference,pEnc->mbParam.fbase); |
1830 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1831 |
|
|
1832 |
|
// copy reference frame details into the current frame |
1833 |
|
pEnc->current->quant = pEnc->reference->quant; |
1834 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1835 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1836 |
|
pEnc->current->quarterpel = pEnc->reference->quarterpel; |
1837 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1838 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1839 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1840 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1841 |
|
|
1842 |
|
} |
1843 |
|
#endif |
1844 |
|
|
1845 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1846 |
|
|
1847 |
return 0; // inter |
return 0; // inter |
1848 |
} |
} |
1849 |
|
|
1850 |
|
|
1851 |
#ifdef BFRAMES |
static __inline void |
|
static void |
|
1852 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1853 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1854 |
Bitstream * bs, |
Bitstream * bs, |
1857 |
int16_t dct_codes[6 * 64]; |
int16_t dct_codes[6 * 64]; |
1858 |
int16_t qcoeff[6 * 64]; |
int16_t qcoeff[6 * 64]; |
1859 |
uint32_t x, y; |
uint32_t x, y; |
|
VECTOR forward; |
|
|
VECTOR backward; |
|
1860 |
|
|
1861 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1862 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1863 |
|
|
1864 |
|
#ifdef BFRAMES_DEC_DEBUG |
1865 |
|
FILE *fp; |
1866 |
|
static char first=0; |
1867 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1868 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1869 |
|
} |
1870 |
|
|
1871 |
|
if (!first){ |
1872 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1873 |
|
} |
1874 |
|
#endif |
1875 |
|
|
1876 |
|
frame->quarterpel = pEnc->mbParam.m_quarterpel; |
1877 |
|
|
1878 |
// forward |
// forward |
1879 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1880 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1881 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1882 |
start_timer(); |
start_timer(); |
1883 |
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, |
1884 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1885 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1886 |
stop_inter_timer(); |
stop_inter_timer(); |
1887 |
|
|
1888 |
// backward |
// backward |
1889 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1890 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1891 |
pEnc->mbParam.height, |
pEnc->mbParam.height); |
|
frame->global_flags & XVID_INTERLACING); |
|
1892 |
start_timer(); |
start_timer(); |
1893 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1894 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1895 |
0); |
pEnc->mbParam.m_quarterpel, 0); |
1896 |
stop_inter_timer(); |
stop_inter_timer(); |
1897 |
|
|
1898 |
start_timer(); |
start_timer(); |
1899 |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
|
1900 |
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1901 |
|
((int32_t)(pEnc->current->stamp - frame->stamp)), // time_bp |
1902 |
|
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), // time_pp |
1903 |
|
pEnc->reference->mbs, f_ref, |
1904 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1905 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1906 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1914 |
} */ |
} */ |
1915 |
|
|
1916 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1917 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
|
1918 |
|
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
1919 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1920 |
|
|
1921 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1922 |
|
|
1927 |
|
|
1928 |
|
|
1929 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
// reset prediction |
|
|
|
|
|
forward.x = 0; |
|
|
forward.y = 0; |
|
|
backward.x = 0; |
|
|
backward.y = 0; |
|
|
|
|
1930 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1931 |
MACROBLOCK *f_mb = |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1932 |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
|
MACROBLOCK *b_mb = |
|
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
|
MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1933 |
|
|
1934 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1935 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1936 |
mb->mvs[0].x = 0; |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
|
mb->mvs[0].y = 0; |
|
1937 |
continue; |
continue; |
1938 |
} |
} |
1939 |
|
|
1940 |
|
if (mb->mode != MODE_DIRECT_NONE_MV) { |
1941 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1942 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1943 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1944 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1945 |
dct_codes); |
dct_codes); |
1946 |
|
|
1947 |
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
1948 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
|
mb->cbp = |
|
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
|
|
qcoeff); |
|
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
|
1949 |
|
|
1950 |
|
mb->cbp = |
1951 |
|
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
1952 |
|
|
1953 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1954 |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
1955 |
mb->mode = 5; // skipped |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
1956 |
} |
} |
|
|
|
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
|
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
|
|
forward.x = mb->mvs[0].x; |
|
|
forward.y = mb->mvs[0].y; |
|
1957 |
} |
} |
1958 |
|
|
1959 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
#ifdef BFRAMES_DEC_DEBUG |
1960 |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
BFRAME_DEBUG |
1961 |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
#endif |
|
backward.x = mb->b_mvs[0].x; |
|
|
backward.y = mb->b_mvs[0].y; |
|
|
} |
|
|
// printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); |
|
|
|
|
1962 |
start_timer(); |
start_timer(); |
1963 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1964 |
&pEnc->sStat); |
&pEnc->sStat, direction); |
1965 |
stop_coding_timer(); |
stop_coding_timer(); |
1966 |
} |
} |
1967 |
} |
} |
1971 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1972 |
|
|
1973 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1974 |
|
|
1975 |
|
#ifdef BFRAMES_DEC_DEBUG |
1976 |
|
if (!first){ |
1977 |
|
first=1; |
1978 |
|
if (fp) |
1979 |
|
fclose(fp); |
1980 |
} |
} |
1981 |
#endif |
#endif |
1982 |
|
} |
1983 |
|
|
1984 |
|
|
1985 |
|
/* in case internal output is needed somewhere... */ |
1986 |
|
/* { |
1987 |
|
FILE *filehandle; |
1988 |
|
filehandle=fopen("last-b.pgm","wb"); |
1989 |
|
if (filehandle) |
1990 |
|
{ |
1991 |
|
fprintf(filehandle,"P5\n\n"); // |
1992 |
|
fprintf(filehandle,"%d %d 255\n",pEnc->mbParam.edged_width,pEnc->mbParam.edged_height); |
1993 |
|
fwrite(frame->image.y,pEnc->mbParam.edged_width,pEnc->mbParam.edged_height,filehandle); |
1994 |
|
fclose(filehandle); |
1995 |
|
} |
1996 |
|
} |
1997 |
|
*/ |