3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
|
* Copyright(C) 2002 Michael Militzer |
7 |
|
* |
8 |
* This program is an implementation of a part of one or more MPEG-4 |
* This program is an implementation of a part of one or more MPEG-4 |
9 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
* to use this software module in hardware or software products are |
* to use this software module in hardware or software products are |
30 |
* |
* |
31 |
****************************************************************************/ |
****************************************************************************/ |
32 |
|
|
|
/***************************************************************************** |
|
|
* |
|
|
* History |
|
|
* |
|
|
* 08.05.2002 fix some problem in DEBUG mode; |
|
|
* MinChen <chenm001@163.com> |
|
|
* 14.04.2002 added FrameCodeB() |
|
|
* |
|
|
* $Id$ |
|
|
* |
|
|
****************************************************************************/ |
|
|
|
|
33 |
#include <stdlib.h> |
#include <stdlib.h> |
34 |
#include <stdio.h> |
#include <stdio.h> |
35 |
#include <math.h> |
#include <math.h> |
36 |
|
#include <string.h> |
37 |
|
|
38 |
#include "encoder.h" |
#include "encoder.h" |
39 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
40 |
#include "global.h" |
#include "global.h" |
41 |
#include "utils/timer.h" |
#include "utils/timer.h" |
42 |
#include "image/image.h" |
#include "image/image.h" |
43 |
|
#ifdef BFRAMES |
44 |
|
#include "image/font.h" |
45 |
|
#include "motion/sad.h" |
46 |
|
#endif |
47 |
#include "motion/motion.h" |
#include "motion/motion.h" |
48 |
#include "bitstream/cbp.h" |
#include "bitstream/cbp.h" |
49 |
#include "utils/mbfunctions.h" |
#include "utils/mbfunctions.h" |
56 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
57 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
58 |
|
|
59 |
|
#ifdef _SMP |
60 |
|
#include "motion/smp_motion_est.h" |
61 |
|
#endif |
62 |
/***************************************************************************** |
/***************************************************************************** |
63 |
* Local macros |
* Local macros |
64 |
****************************************************************************/ |
****************************************************************************/ |
129 |
encoder_create(XVID_ENC_PARAM * pParam) |
encoder_create(XVID_ENC_PARAM * pParam) |
130 |
{ |
{ |
131 |
Encoder *pEnc; |
Encoder *pEnc; |
132 |
uint32_t i; |
int i; |
133 |
|
|
134 |
pParam->handle = NULL; |
pParam->handle = NULL; |
135 |
|
|
197 |
|
|
198 |
/* 1 keyframe each 10 seconds */ |
/* 1 keyframe each 10 seconds */ |
199 |
|
|
200 |
if (pParam->max_key_interval == 0) |
if (pParam->max_key_interval <= 0) |
201 |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
202 |
|
|
|
|
|
203 |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE); |
204 |
if (pEnc == NULL) |
if (pEnc == NULL) |
205 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
206 |
|
|
207 |
|
/* Zero the Encoder Structure */ |
208 |
|
|
209 |
|
memset(pEnc, 0, sizeof(Encoder)); |
210 |
|
|
211 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
212 |
|
|
213 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
222 |
pEnc->mbParam.fbase = pParam->fbase; |
pEnc->mbParam.fbase = pParam->fbase; |
223 |
pEnc->mbParam.fincr = pParam->fincr; |
pEnc->mbParam.fincr = pParam->fincr; |
224 |
|
|
225 |
|
pEnc->mbParam.m_quant_type = H263_QUANT; |
226 |
|
|
227 |
|
#ifdef _SMP |
228 |
|
pEnc->mbParam.num_threads = MIN(pParam->num_threads, MAXNUMTHREADS); |
229 |
|
#endif |
230 |
|
|
231 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
232 |
|
|
233 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
259 |
|
|
260 |
/* try to allocate image memory */ |
/* try to allocate image memory */ |
261 |
|
|
262 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
263 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
264 |
#endif |
#endif |
265 |
#ifdef BFRAMES |
#ifdef BFRAMES |
275 |
image_null(&pEnc->vInterHV); |
image_null(&pEnc->vInterHV); |
276 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
277 |
|
|
278 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
279 |
if (image_create |
if (image_create |
280 |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
281 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
329 |
/* B Frames specific init */ |
/* B Frames specific init */ |
330 |
#ifdef BFRAMES |
#ifdef BFRAMES |
331 |
|
|
332 |
|
pEnc->global = pParam->global; |
333 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
334 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
335 |
|
pEnc->frame_drop_ratio = pParam->frame_drop_ratio; |
336 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
337 |
|
|
338 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
375 |
pEnc->bframenum_head = 0; |
pEnc->bframenum_head = 0; |
376 |
pEnc->bframenum_tail = 0; |
pEnc->bframenum_tail = 0; |
377 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
378 |
|
pEnc->bframenum_dx50bvop = -1; |
379 |
|
|
380 |
|
pEnc->queue = NULL; |
381 |
|
|
382 |
|
|
383 |
|
if (pEnc->mbParam.max_bframes > 0) { |
384 |
|
int n; |
385 |
|
|
386 |
|
pEnc->queue = |
387 |
|
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(IMAGE), |
388 |
|
CACHE_LINE); |
389 |
|
|
390 |
|
if (pEnc->queue == NULL) |
391 |
|
goto xvid_err_memory4; |
392 |
|
|
393 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
394 |
|
image_null(&pEnc->queue[n]); |
395 |
|
|
396 |
|
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
397 |
|
if (image_create |
398 |
|
(&pEnc->queue[n], pEnc->mbParam.edged_width, |
399 |
|
pEnc->mbParam.edged_height) < 0) |
400 |
|
goto xvid_err_memory5; |
401 |
|
|
402 |
|
} |
403 |
|
} |
404 |
|
|
405 |
|
pEnc->queue_head = 0; |
406 |
|
pEnc->queue_tail = 0; |
407 |
|
pEnc->queue_size = 0; |
408 |
|
|
409 |
|
|
410 |
pEnc->mbParam.m_seconds = 0; |
pEnc->mbParam.m_seconds = 0; |
411 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
412 |
|
pEnc->m_framenum = 0; |
413 |
|
pEnc->last_pframe = 0; |
414 |
|
pEnc->last_sync = 0; |
415 |
#endif |
#endif |
416 |
|
|
417 |
pParam->handle = (void *) pEnc; |
pParam->handle = (void *) pEnc; |
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 |
#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 |
|
} |
467 |
|
|
468 |
#endif |
#endif |
469 |
|
|
470 |
xvid_err_memory3: |
xvid_err_memory3: |
471 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
472 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
473 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
474 |
#endif |
#endif |
525 |
int |
int |
526 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
527 |
{ |
{ |
528 |
|
#ifdef BFRAMES |
529 |
|
int i; |
530 |
|
#endif |
531 |
|
|
532 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
533 |
|
|
534 |
/* B Frames specific */ |
/* B Frames specific */ |
535 |
#ifdef BFRAMES |
#ifdef BFRAMES |
536 |
int i; |
if (pEnc->mbParam.max_bframes > 0) { |
537 |
|
|
538 |
|
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
539 |
|
|
540 |
|
image_destroy(&pEnc->queue[i], pEnc->mbParam.edged_width, |
541 |
|
pEnc->mbParam.edged_height); |
542 |
|
} |
543 |
|
xvid_free(pEnc->queue); |
544 |
|
} |
545 |
|
|
546 |
|
|
547 |
|
if (pEnc->mbParam.max_bframes > 0) { |
548 |
|
|
549 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
550 |
|
|
557 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
558 |
|
|
559 |
xvid_free(pEnc->bframes[i]); |
xvid_free(pEnc->bframes[i]); |
|
|
|
560 |
} |
} |
561 |
|
|
562 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
563 |
|
|
564 |
|
} |
565 |
#endif |
#endif |
566 |
|
|
567 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
588 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
589 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
590 |
#endif |
#endif |
591 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
592 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
593 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
594 |
#endif |
#endif |
606 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
607 |
} |
} |
608 |
|
|
609 |
|
|
610 |
|
void inc_frame_num(Encoder * pEnc) |
611 |
|
{ |
612 |
|
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
613 |
|
|
614 |
|
#ifdef BFRAMES |
615 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
616 |
|
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
617 |
|
pEnc->mbParam.m_seconds = 1; // more than 1 second since last I or P is not supported. |
618 |
|
else |
619 |
|
pEnc->mbParam.m_seconds = 0; |
620 |
|
|
621 |
|
if (pEnc->current->coding_type != B_VOP) |
622 |
|
pEnc->last_sync = pEnc->mbParam.m_ticks; |
623 |
|
#else |
624 |
|
|
625 |
|
pEnc->mbParam.m_seconds = pEnc->mbParam.m_ticks / pEnc->mbParam.fbase; |
626 |
|
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
627 |
|
|
628 |
|
#endif |
629 |
|
|
630 |
|
} |
631 |
|
|
632 |
|
|
633 |
|
#ifdef BFRAMES |
634 |
|
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
635 |
|
{ |
636 |
|
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
637 |
|
{ |
638 |
|
DPRINTF(DPRINTF_DEBUG,"FATAL: QUEUE FULL"); |
639 |
|
return; |
640 |
|
} |
641 |
|
|
642 |
|
DPRINTF(DPRINTF_DEBUG,"*** QUEUE bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
643 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
644 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
645 |
|
|
646 |
|
|
647 |
|
start_timer(); |
648 |
|
if (image_input |
649 |
|
(&pEnc->queue[pEnc->queue_tail], pEnc->mbParam.width, pEnc->mbParam.height, |
650 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
651 |
|
return; |
652 |
|
stop_conv_timer(); |
653 |
|
|
654 |
|
pEnc->queue_size++; |
655 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
656 |
|
} |
657 |
|
#endif |
658 |
|
|
659 |
|
|
660 |
|
#ifdef BFRAMES |
661 |
/***************************************************************************** |
/***************************************************************************** |
662 |
* 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. |
|
663 |
* |
* |
664 |
* Returned values : |
* Returned values : |
665 |
* - XVID_ERR_OK - no errors |
* - XVID_ERR_OK - no errors |
667 |
* format |
* format |
668 |
****************************************************************************/ |
****************************************************************************/ |
669 |
|
|
|
|
|
|
#ifdef BFRAMES |
|
|
/***************************************************************************** |
|
|
* Frame encoder entry point for IPB capable encoder |
|
|
****************************************************************************/ |
|
670 |
int |
int |
671 |
encoder_encode(Encoder * pEnc, |
encoder_encode_bframes(Encoder * pEnc, |
672 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
673 |
XVID_ENC_STATS * pResult) |
XVID_ENC_STATS * pResult) |
674 |
{ |
{ |
676 |
Bitstream bs; |
Bitstream bs; |
677 |
uint32_t bits; |
uint32_t bits; |
678 |
|
|
679 |
#ifdef _DEBUG |
int input_valid = 1; |
680 |
|
|
681 |
|
#ifdef _DEBUG_PSNR |
682 |
float psnr; |
float psnr; |
683 |
char temp[128]; |
char temp[128]; |
684 |
#endif |
#endif |
685 |
|
|
686 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
687 |
ENC_CHECK(pFrame); |
ENC_CHECK(pFrame); |
688 |
|
ENC_CHECK(pFrame->image); |
689 |
|
|
690 |
start_global_timer(); |
start_global_timer(); |
691 |
|
|
692 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
693 |
|
|
694 |
|
ipvop_loop: |
695 |
|
|
696 |
/* |
/* |
697 |
* bframe "flush" code |
* bframe "flush" code |
698 |
*/ |
*/ |
707 |
* frame as a pframe |
* frame as a pframe |
708 |
*/ |
*/ |
709 |
|
|
710 |
/* ToDo : remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (final frame) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
711 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
712 |
dprintf("--- PFRAME (final frame correction) --- "); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
713 |
*/ |
|
714 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
715 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
716 |
|
|
720 |
|
|
721 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
722 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
723 |
pFrame->intra = 0; |
pFrame->intra = 0; |
724 |
|
|
725 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
726 |
} |
} |
727 |
|
|
728 |
/* ToDo : remove dprintf calls */ |
|
729 |
/* |
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (flush) bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
730 |
dprintf("--- BFRAME (flush) --- "); |
pEnc->bframenum_head, pEnc->bframenum_tail, |
731 |
*/ |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
732 |
|
|
733 |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
734 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
735 |
|
|
|
|
|
736 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
737 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
|
pFrame->input_consumed = 0; |
|
738 |
pFrame->intra = 0; |
pFrame->intra = 0; |
739 |
|
|
740 |
|
if (input_valid) |
741 |
|
queue_image(pEnc, pFrame); |
742 |
|
|
743 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
744 |
} |
} |
745 |
|
|
746 |
if (pFrame->image == NULL) { |
if (pEnc->bframenum_head > 0) { |
747 |
pFrame->length = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
748 |
pFrame->input_consumed = 1; |
|
749 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
750 |
|
|
751 |
|
DPRINTF(DPRINTF_DEBUG,"*** EMPTY bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
752 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
753 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
754 |
|
|
755 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); |
756 |
|
BitstreamPad(&bs); |
757 |
|
BitstreamPutBits(&bs, 0x7f, 8); |
758 |
|
|
759 |
|
pFrame->length = BitstreamLength(&bs); |
760 |
pFrame->intra = 0; |
pFrame->intra = 0; |
761 |
|
|
762 |
|
if (input_valid) |
763 |
|
queue_image(pEnc, pFrame); |
764 |
|
|
765 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
766 |
} |
} |
767 |
|
} |
768 |
|
|
769 |
if (pEnc->bframenum_head > 0) { |
|
770 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
bvop_loop: |
771 |
|
|
772 |
|
if (pEnc->bframenum_dx50bvop != -1) |
773 |
|
{ |
774 |
|
|
775 |
|
SWAP(pEnc->current, pEnc->reference); |
776 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
777 |
|
|
778 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
779 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 IVOP"); |
780 |
|
} |
781 |
|
|
782 |
|
if (input_valid) |
783 |
|
{ |
784 |
|
queue_image(pEnc, pFrame); |
785 |
|
input_valid = 0; |
786 |
|
} |
787 |
|
|
788 |
|
} else if (input_valid) { |
789 |
|
|
790 |
|
SWAP(pEnc->current, pEnc->reference); |
791 |
|
|
792 |
|
start_timer(); |
793 |
|
if (image_input |
794 |
|
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
795 |
|
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
796 |
|
return XVID_ERR_FORMAT; |
797 |
|
stop_conv_timer(); |
798 |
|
|
799 |
|
// queue input frame, and dequue next image |
800 |
|
if (pEnc->queue_size > 0) |
801 |
|
{ |
802 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_tail]); |
803 |
|
if (pEnc->queue_head != pEnc->queue_tail) |
804 |
|
{ |
805 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
806 |
|
} |
807 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
808 |
|
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
809 |
|
} |
810 |
|
|
811 |
|
} else if (pEnc->queue_size > 0) { |
812 |
|
|
813 |
|
SWAP(pEnc->current, pEnc->reference); |
814 |
|
|
815 |
|
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head]); |
816 |
|
pEnc->queue_head = (pEnc->queue_head + 1) % pEnc->mbParam.max_bframes; |
817 |
|
pEnc->queue_size--; |
818 |
|
|
819 |
|
} else if (BitstreamPos(&bs) == 0) { |
820 |
|
|
821 |
|
DPRINTF(DPRINTF_DEBUG,"*** SKIP bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
822 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
823 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
824 |
|
|
825 |
|
pFrame->intra = 0; |
826 |
|
|
827 |
|
BitstreamWriteVopHeader(&bs, &pEnc->mbParam, pEnc->current, 0); // write N_VOP |
828 |
|
BitstreamPad(&bs); |
829 |
|
pFrame->length = BitstreamLength(&bs); |
830 |
|
|
831 |
|
return XVID_ERR_OK; |
832 |
|
|
833 |
|
} else { |
834 |
|
|
835 |
|
pFrame->length = BitstreamLength(&bs); |
836 |
|
return XVID_ERR_OK; |
837 |
} |
} |
838 |
|
|
839 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
843 |
* comment style :-) |
* comment style :-) |
844 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
845 |
|
|
846 |
SWAP(pEnc->current, pEnc->reference); |
emms(); |
|
|
|
|
EMMS(); |
|
847 |
|
|
848 |
|
// only inc frame num, adapt quant, etc. if we havent seen it before |
849 |
|
if (pEnc->bframenum_dx50bvop < 0 ) |
850 |
|
{ |
851 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
852 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
853 |
else |
else |
854 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
855 |
|
|
856 |
if (pEnc->current->quant < 1) |
/* if (pEnc->current->quant < 1) |
857 |
pEnc->current->quant = 1; |
pEnc->current->quant = 1; |
858 |
|
|
859 |
if (pEnc->current->quant > 31) |
if (pEnc->current->quant > 31) |
860 |
pEnc->current->quant = 31; |
pEnc->current->quant = 31; |
861 |
|
*/ |
862 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
863 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
864 |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
|
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
|
865 |
/* ToDo : dynamic fcode (in both directions) */ |
/* ToDo : dynamic fcode (in both directions) */ |
866 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
867 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
868 |
|
|
869 |
start_timer(); |
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
870 |
if (image_input |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
871 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
|
872 |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
inc_frame_num(pEnc); |
|
return XVID_ERR_FORMAT; |
|
|
stop_conv_timer(); |
|
873 |
|
|
874 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
875 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
876 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
877 |
#endif |
#endif |
878 |
|
|
879 |
EMMS(); |
emms(); |
880 |
|
|
881 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
882 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 5, |
883 |
|
"%i if:%i st:%i:%i", pEnc->m_framenum++, pEnc->iFrameNum, pEnc->current->seconds, pEnc->current->ticks); |
884 |
|
} |
885 |
|
|
886 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
887 |
* Luminance masking |
* Luminance masking |
912 |
} |
} |
913 |
|
|
914 |
#undef OFFSET |
#undef OFFSET |
|
|
|
915 |
} |
} |
916 |
|
|
917 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
918 |
} |
} |
919 |
|
|
920 |
|
} |
921 |
|
|
922 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
923 |
* ivop/pvop/bvop selection |
* ivop/pvop/bvop selection |
924 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
925 |
|
|
926 |
|
|
927 |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || pEnc->bframenum_dx50bvop >= 0 || |
928 |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
929 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
930 |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
934 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
935 |
*/ |
*/ |
936 |
|
|
937 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** IFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
938 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
939 |
dprintf("--- IFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
|
*/ |
|
940 |
|
|
941 |
FrameCodeI(pEnc, &bs, &bits); |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
942 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "IVOP"); |
943 |
|
} |
944 |
|
|
945 |
|
// when we reach an iframe in DX50BVOP mode, encode the last bframe as a pframe |
946 |
|
|
947 |
|
if ((pEnc->global & XVID_GLOBAL_DX50BVOP) && pEnc->bframenum_tail > 0) { |
948 |
|
|
949 |
|
pEnc->bframenum_tail--; |
950 |
|
pEnc->bframenum_dx50bvop = pEnc->bframenum_tail; |
951 |
|
|
952 |
|
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_dx50bvop]); |
953 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
954 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 100, "DX50 BVOP->PVOP"); |
955 |
|
} |
956 |
|
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
957 |
|
|
958 |
|
pFrame->intra = 0; |
959 |
|
|
960 |
|
} else { |
961 |
|
|
962 |
|
FrameCodeI(pEnc, &bs, &bits); |
963 |
pFrame->intra = 1; |
pFrame->intra = 1; |
964 |
|
|
965 |
|
pEnc->bframenum_dx50bvop = -1; |
966 |
|
} |
967 |
|
|
968 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
969 |
|
|
970 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED) && pEnc->bframenum_tail > 0) { |
971 |
|
BitstreamPad(&bs); |
972 |
|
input_valid = 0; |
973 |
|
goto ipvop_loop; |
974 |
|
} |
975 |
|
|
976 |
/* |
/* |
977 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
978 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
982 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
983 |
*/ |
*/ |
984 |
|
|
985 |
/* ToDo : Remove dprintf calls */ |
DPRINTF(DPRINTF_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i", |
986 |
/* |
pEnc->bframenum_head, pEnc->bframenum_tail, |
987 |
dprintf("--- PFRAME ---"); |
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size); |
988 |
*/ |
|
989 |
|
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
990 |
|
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "PVOP"); |
991 |
|
} |
992 |
|
|
993 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
994 |
pFrame->intra = 0; |
pFrame->intra = 0; |
995 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
996 |
|
|
997 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
998 |
|
BitstreamPad(&bs); |
999 |
|
input_valid = 0; |
1000 |
|
goto ipvop_loop; |
1001 |
|
} |
1002 |
|
|
1003 |
} else { |
} else { |
1004 |
/* |
/* |
1005 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
1006 |
*/ |
*/ |
1007 |
|
|
1008 |
/* ToDo : Remove dprintf calls */ |
if ((pEnc->global & XVID_GLOBAL_DEBUG)) { |
1009 |
/* |
image_printf(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.height, 5, 200, "BVOP"); |
1010 |
dprintf("--- BFRAME (store) --- head=%i tail=%i", |
} |
|
pEnc->bframenum_head, |
|
|
pEnc->bframenum_tail); |
|
|
*/ |
|
1011 |
|
|
1012 |
if (pFrame->bquant < 1) { |
if (pFrame->bquant < 1) { |
1013 |
pEnc->current->quant = |
pEnc->current->quant = |
1016 |
} else { |
} else { |
1017 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
1018 |
} |
} |
1019 |
|
if (pEnc->current->quant < 1) |
1020 |
|
pEnc->current->quant = 1; |
1021 |
|
|
1022 |
|
if (pEnc->current->quant > 31) |
1023 |
|
pEnc->current->quant = 31; |
1024 |
|
|
1025 |
|
|
1026 |
|
DPRINTF(DPRINTF_DEBUG,"*** BFRAME (store) bf: head=%i tail=%i queue: head=%i tail=%i size=%i quant=%i\n", |
1027 |
|
pEnc->bframenum_head, pEnc->bframenum_tail, |
1028 |
|
pEnc->queue_head, pEnc->queue_tail, pEnc->queue_size,pEnc->current->quant); |
1029 |
|
|
1030 |
|
|
1031 |
|
|
1032 |
/* store frame into bframe buffer & swap ref back to current */ |
/* store frame into bframe buffer & swap ref back to current */ |
1033 |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1037 |
|
|
1038 |
pFrame->intra = 0; |
pFrame->intra = 0; |
1039 |
pFrame->length = 0; |
pFrame->length = 0; |
|
pFrame->input_consumed = 1; |
|
1040 |
|
|
1041 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
input_valid = 0; |
1042 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
goto bvop_loop; |
|
pEnc->mbParam.m_seconds++; |
|
|
pEnc->mbParam.m_ticks = 0; |
|
1043 |
} |
} |
1044 |
|
|
1045 |
return XVID_ERR_OK; |
pEnc->iFrameNum++; |
|
} |
|
1046 |
|
|
1047 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1048 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
1055 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1056 |
} |
} |
1057 |
|
|
1058 |
EMMS(); |
emms(); |
1059 |
|
|
1060 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1061 |
psnr = |
psnr = |
1062 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1063 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1072 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1073 |
} |
} |
1074 |
|
|
|
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; |
|
1075 |
|
|
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 |
|
#endif |
1083 |
|
|
1084 |
|
|
1085 |
|
|
1086 |
/***************************************************************************** |
/***************************************************************************** |
1087 |
* Frame encoder entry point for IP capable encoder |
* "original" IP frame encoder entry point |
1088 |
|
* |
1089 |
|
* Returned values : |
1090 |
|
* - XVID_ERR_OK - no errors |
1091 |
|
* - XVID_ERR_FORMAT - the image subsystem reported the image had a wrong |
1092 |
|
* format |
1093 |
****************************************************************************/ |
****************************************************************************/ |
1094 |
|
|
1095 |
int |
int |
1096 |
encoder_encode(Encoder * pEnc, |
encoder_encode(Encoder * pEnc, |
1097 |
XVID_ENC_FRAME * pFrame, |
XVID_ENC_FRAME * pFrame, |
1102 |
uint32_t bits; |
uint32_t bits; |
1103 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
1104 |
|
|
1105 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1106 |
float psnr; |
float psnr; |
1107 |
uint8_t temp[128]; |
uint8_t temp[128]; |
1108 |
#endif |
#endif |
1118 |
|
|
1119 |
pEnc->current->global_flags = pFrame->general; |
pEnc->current->global_flags = pFrame->general; |
1120 |
pEnc->current->motion_flags = pFrame->motion; |
pEnc->current->motion_flags = pFrame->motion; |
1121 |
|
pEnc->current->seconds = pEnc->mbParam.m_seconds; |
1122 |
|
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1123 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1124 |
|
|
1125 |
start_timer(); |
start_timer(); |
1129 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
1130 |
stop_conv_timer(); |
stop_conv_timer(); |
1131 |
|
|
1132 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1133 |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
1134 |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1135 |
#endif |
#endif |
1136 |
|
|
1137 |
EMMS(); |
emms(); |
1138 |
|
|
1139 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
1140 |
|
|
1233 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
1234 |
} |
} |
1235 |
|
|
1236 |
EMMS(); |
emms(); |
1237 |
|
|
1238 |
if (pFrame->quant == 0) { |
if (pFrame->quant == 0) { |
1239 |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
1240 |
pFrame->length, pFrame->intra); |
pFrame->length, pFrame->intra); |
1241 |
} |
} |
1242 |
#ifdef _DEBUG |
#ifdef _DEBUG_PSNR |
1243 |
psnr = |
psnr = |
1244 |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
1245 |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
1249 |
DEBUG(temp); |
DEBUG(temp); |
1250 |
#endif |
#endif |
1251 |
|
|
1252 |
|
inc_frame_num(pEnc); |
1253 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
1254 |
|
|
1255 |
stop_global_timer(); |
stop_global_timer(); |
1257 |
|
|
1258 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1259 |
} |
} |
|
#endif |
|
1260 |
|
|
1261 |
|
|
1262 |
static __inline void |
static __inline void |
1326 |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1327 |
MVBLOCKHINT *bhint = |
MVBLOCKHINT *bhint = |
1328 |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1329 |
VECTOR pred[4]; |
VECTOR pred; |
1330 |
VECTOR tmp; |
VECTOR tmp; |
|
int32_t dummy[4]; |
|
1331 |
int vec; |
int vec; |
1332 |
|
|
1333 |
pMB->mode = |
pMB->mode = |
1347 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1348 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1349 |
|
|
1350 |
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); |
|
1351 |
|
|
1352 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1353 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1354 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1355 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred.x; |
1356 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred.y; |
1357 |
} |
} |
1358 |
} else if (pMB->mode == MODE_INTER4V) { |
} else if (pMB->mode == MODE_INTER4V) { |
1359 |
for (vec = 0; vec < 4; ++vec) { |
for (vec = 0; vec < 4; ++vec) { |
1366 |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
tmp.x -= (tmp.x >= high) ? high * 2 : 0; |
1367 |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
tmp.y -= (tmp.y >= high) ? high * 2 : 0; |
1368 |
|
|
1369 |
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); |
|
1370 |
|
|
1371 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1372 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1373 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred.x; |
1374 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred.y; |
1375 |
} |
} |
1376 |
} else // intra / stuffing / not_coded |
} else // intra / stuffing / not_coded |
1377 |
{ |
{ |
1501 |
pEnc->current->coding_type = I_VOP; |
pEnc->current->coding_type = I_VOP; |
1502 |
|
|
1503 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1504 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
#ifdef BFRAMES |
1505 |
|
#define DIVX501B481P "DivX501b481p" |
1506 |
|
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1507 |
|
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1508 |
|
} |
1509 |
|
#endif |
1510 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1511 |
|
|
1512 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1513 |
|
|
1530 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1531 |
|
|
1532 |
start_timer(); |
start_timer(); |
1533 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1534 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1535 |
|
qcoeff[4*64+0]=0; /* zero, because for INTRA MBs DC value is saved */ |
1536 |
|
qcoeff[5*64+0]=0; |
1537 |
|
} |
1538 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1539 |
stop_coding_timer(); |
stop_coding_timer(); |
1540 |
} |
} |
1556 |
|
|
1557 |
|
|
1558 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1559 |
|
#define BFRAME_SKIP_THRESHHOLD 16 |
1560 |
|
|
1561 |
static int |
static int |
1562 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1571 |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
1572 |
|
|
1573 |
int iLimit; |
int iLimit; |
1574 |
uint32_t x, y; |
int x, y, k; |
1575 |
int iSearchRange; |
int iSearchRange; |
1576 |
int bIntra; |
int bIntra; |
1577 |
|
|
1608 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1609 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1610 |
} else { |
} else { |
1611 |
|
|
1612 |
|
#ifdef _SMP |
1613 |
|
if (pEnc->mbParam.num_threads > 1) |
1614 |
|
bIntra = |
1615 |
|
SMP_MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1616 |
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1617 |
|
iLimit); |
1618 |
|
else |
1619 |
|
#endif |
1620 |
bIntra = |
bIntra = |
1621 |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1622 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1623 |
iLimit); |
iLimit); |
1624 |
|
|
1625 |
} |
} |
1626 |
stop_motion_timer(); |
stop_motion_timer(); |
1627 |
|
|
1634 |
if (vol_header) |
if (vol_header) |
1635 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
1636 |
|
|
1637 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1638 |
|
|
1639 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1640 |
|
|
1699 |
} |
} |
1700 |
|
|
1701 |
start_timer(); |
start_timer(); |
1702 |
|
|
1703 |
|
/* Finished processing the MB, now check if to CODE or SKIP */ |
1704 |
|
|
1705 |
|
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1706 |
|
pMB->mvs[0].y == 0) { |
1707 |
|
|
1708 |
|
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
1709 |
|
|
1710 |
|
#ifdef BFRAMES |
1711 |
|
int iSAD=BFRAME_SKIP_THRESHHOLD; |
1712 |
|
int bSkip=1; |
1713 |
|
|
1714 |
|
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1715 |
|
{ |
1716 |
|
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1717 |
|
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1718 |
|
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1719 |
|
if (iSAD >= BFRAME_SKIP_THRESHHOLD) |
1720 |
|
{ bSkip = 0; |
1721 |
|
break; |
1722 |
|
} |
1723 |
|
} |
1724 |
|
if (!bSkip) |
1725 |
|
{ |
1726 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1727 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1728 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1729 |
|
qcoeff[5*64+0]=0; |
1730 |
|
} |
1731 |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1732 |
|
pMB->cbp = 0x80; /* trick! so cbp!=0, but still nothing is written to bs */ |
1733 |
|
} |
1734 |
|
else |
1735 |
|
MBSkip(bs); |
1736 |
|
|
1737 |
|
#else |
1738 |
|
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1739 |
|
|
1740 |
|
#endif |
1741 |
|
|
1742 |
|
} else { |
1743 |
|
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1744 |
|
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1745 |
|
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1746 |
|
qcoeff[5*64+0]=0; |
1747 |
|
} |
1748 |
|
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
1749 |
|
} |
1750 |
|
|
1751 |
stop_coding_timer(); |
stop_coding_timer(); |
1752 |
} |
} |
1753 |
} |
} |
1781 |
|
|
1782 |
pEnc->sStat.fMvPrevSigma = fSigma; |
pEnc->sStat.fMvPrevSigma = fSigma; |
1783 |
|
|
1784 |
|
#ifdef BFRAMES |
1785 |
|
/* frame drop code */ |
1786 |
|
// DPRINTF(DPRINTF_DEBUG, "kmu %i %i %i", pEnc->sStat.kblks, pEnc->sStat.mblks, pEnc->sStat.ublks); |
1787 |
|
if (pEnc->sStat.kblks + pEnc->sStat.mblks < |
1788 |
|
(pEnc->frame_drop_ratio * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height) / 100) |
1789 |
|
{ |
1790 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = 0; |
1791 |
|
pEnc->sStat.ublks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; |
1792 |
|
|
1793 |
|
BitstreamReset(bs); |
1794 |
|
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 0); |
1795 |
|
|
1796 |
|
// copy reference frame details into the current frame |
1797 |
|
pEnc->current->quant = pEnc->reference->quant; |
1798 |
|
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1799 |
|
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
1800 |
|
pEnc->current->fcode = pEnc->reference->fcode; |
1801 |
|
pEnc->current->bcode = pEnc->reference->bcode; |
1802 |
|
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1803 |
|
memcpy(pEnc->current->mbs, pEnc->reference->mbs, sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); |
1804 |
|
|
1805 |
|
} |
1806 |
|
#endif |
1807 |
|
|
1808 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1809 |
|
|
1810 |
|
#ifdef BFRAMES |
1811 |
|
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
1812 |
|
(int32_t)pEnc->mbParam.fbase; |
1813 |
|
pEnc->last_pframe = pEnc->current->ticks; |
1814 |
|
#endif |
1815 |
|
|
1816 |
return 0; // inter |
return 0; // inter |
1817 |
} |
} |
1818 |
|
|
1833 |
IMAGE *f_ref = &pEnc->reference->image; |
IMAGE *f_ref = &pEnc->reference->image; |
1834 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1835 |
|
|
1836 |
|
#ifdef BFRAMES_DEC_DEBUG |
1837 |
|
FILE *fp; |
1838 |
|
static char first=0; |
1839 |
|
#define BFRAME_DEBUG if (!first && fp){ \ |
1840 |
|
fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mode,mb->cbp); \ |
1841 |
|
} |
1842 |
|
|
1843 |
|
if (!first){ |
1844 |
|
fp=fopen("C:\\XVIDDBGE.TXT","w"); |
1845 |
|
} |
1846 |
|
#endif |
1847 |
|
|
1848 |
// forward |
// forward |
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, |
1868 |
stop_inter_timer(); |
stop_inter_timer(); |
1869 |
|
|
1870 |
start_timer(); |
start_timer(); |
1871 |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1872 |
|
((int32_t)pEnc->mbParam.fbase + pEnc->last_pframe - frame->ticks) % pEnc->mbParam.fbase, |
1873 |
|
pEnc->time_pp, |
1874 |
|
pEnc->reference->mbs, f_ref, |
1875 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1876 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1877 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
1885 |
} */ |
} */ |
1886 |
|
|
1887 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1888 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, frame, 1); |
1889 |
|
|
1890 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1891 |
|
|
1914 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1915 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1916 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1917 |
|
|
1918 |
|
mb->cbp = 0; |
1919 |
|
#ifdef BFRAMES_DEC_DEBUG |
1920 |
|
BFRAME_DEBUG |
1921 |
|
#endif |
1922 |
continue; |
continue; |
1923 |
} |
} |
1924 |
|
|
1930 |
|
|
1931 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1932 |
mb->cbp = |
mb->cbp = |
1933 |
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
|
qcoeff); |
|
1934 |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1935 |
|
|
1936 |
|
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1937 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
&& (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) { |
1938 |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
|
mb->mode = 5; // skipped |
|
1939 |
} |
} |
1940 |
|
|
1941 |
|
/* update predictors for forward and backward vectors */ |
1942 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1943 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1944 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1952 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1953 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1954 |
} |
} |
|
// 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); |
|
1955 |
|
|
1956 |
|
// DPRINTF("%05i : [%i %i] M=%i CBP=%i MVS=%i,%i forward=%i,%i", pEnc->m_framenum, x, y, mb->mode, mb->cbp, mb->mvs[0].x, mb->mvs[0].y, forward.x, forward.y); |
1957 |
|
|
1958 |
|
#ifdef BFRAMES_DEC_DEBUG |
1959 |
|
BFRAME_DEBUG |
1960 |
|
#endif |
1961 |
start_timer(); |
start_timer(); |
1962 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1963 |
&pEnc->sStat); |
&pEnc->sStat); |
1970 |
// TODO: dynamic fcode/bcode ??? |
// TODO: dynamic fcode/bcode ??? |
1971 |
|
|
1972 |
*pBits = BitstreamPos(bs) - *pBits; |
*pBits = BitstreamPos(bs) - *pBits; |
1973 |
|
|
1974 |
|
#ifdef BFRAMES_DEC_DEBUG |
1975 |
|
if (!first){ |
1976 |
|
first=1; |
1977 |
|
if (fp) |
1978 |
|
fclose(fp); |
1979 |
} |
} |
1980 |
#endif |
#endif |
1981 |
|
} |
1982 |
|
#endif |
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 |
|
*/ |