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 |
|
|
* |
|
|
* 10.07.2002 added BFRAMES_DEC_DEBUG support |
|
|
* MinChen <chenm001@163.com> |
|
|
* 20.06.2002 bframe patch |
|
|
* 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> |
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 |
****************************************************************************/ |
****************************************************************************/ |
80 |
bool force_inter, |
bool force_inter, |
81 |
bool vol_header); |
bool vol_header); |
82 |
|
|
83 |
|
#ifdef BFRAMES |
84 |
static void FrameCodeB(Encoder * pEnc, |
static void FrameCodeB(Encoder * pEnc, |
85 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
86 |
Bitstream * bs, |
Bitstream * bs, |
87 |
uint32_t * pBits); |
uint32_t * pBits); |
88 |
|
#endif |
89 |
|
|
90 |
/***************************************************************************** |
/***************************************************************************** |
91 |
* Local data |
* Local data |
224 |
|
|
225 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
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 */ |
262 |
#ifdef _DEBUG_PSNR |
#ifdef _DEBUG_PSNR |
263 |
image_null(&pEnc->sOriginal); |
image_null(&pEnc->sOriginal); |
264 |
#endif |
#endif |
265 |
|
#ifdef BFRAMES |
266 |
image_null(&pEnc->f_refh); |
image_null(&pEnc->f_refh); |
267 |
image_null(&pEnc->f_refv); |
image_null(&pEnc->f_refv); |
268 |
image_null(&pEnc->f_refhv); |
image_null(&pEnc->f_refhv); |
269 |
|
#endif |
270 |
image_null(&pEnc->current->image); |
image_null(&pEnc->current->image); |
271 |
image_null(&pEnc->reference->image); |
image_null(&pEnc->reference->image); |
272 |
image_null(&pEnc->vInterH); |
image_null(&pEnc->vInterH); |
281 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
282 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
283 |
#endif |
#endif |
284 |
|
#ifdef BFRAMES |
285 |
if (image_create |
if (image_create |
286 |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
287 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
294 |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
295 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
296 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
297 |
|
#endif |
298 |
if (image_create |
if (image_create |
299 |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
300 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
327 |
|
|
328 |
|
|
329 |
/* B Frames specific init */ |
/* B Frames specific init */ |
330 |
|
#ifdef BFRAMES |
331 |
|
|
332 |
pEnc->global = pParam->global; |
pEnc->global = pParam->global; |
333 |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
pEnc->mbParam.max_bframes = pParam->max_bframes; |
412 |
pEnc->m_framenum = 0; |
pEnc->m_framenum = 0; |
413 |
pEnc->last_pframe = 0; |
pEnc->last_pframe = 0; |
414 |
pEnc->last_sync = 0; |
pEnc->last_sync = 0; |
415 |
|
#endif |
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: |
xvid_err_memory5: |
436 |
|
|
437 |
|
|
465 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
466 |
} |
} |
467 |
|
|
468 |
|
#endif |
469 |
|
|
470 |
xvid_err_memory3: |
xvid_err_memory3: |
471 |
#ifdef _DEBUG_PSNR |
#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 |
475 |
|
|
476 |
|
#ifdef BFRAMES |
477 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
478 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
479 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
480 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
481 |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
482 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
483 |
|
#endif |
484 |
|
|
485 |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
486 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
525 |
int |
int |
526 |
encoder_destroy(Encoder * pEnc) |
encoder_destroy(Encoder * pEnc) |
527 |
{ |
{ |
528 |
|
#ifdef BFRAMES |
529 |
int i; |
int i; |
530 |
|
#endif |
531 |
|
|
532 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
533 |
|
|
534 |
/* B Frames specific */ |
/* B Frames specific */ |
535 |
|
#ifdef BFRAMES |
536 |
if (pEnc->mbParam.max_bframes > 0) { |
if (pEnc->mbParam.max_bframes > 0) { |
537 |
|
|
538 |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
562 |
xvid_free(pEnc->bframes); |
xvid_free(pEnc->bframes); |
563 |
|
|
564 |
} |
} |
565 |
|
#endif |
566 |
|
|
567 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
568 |
|
|
580 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
581 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
582 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
583 |
|
#ifdef BFRAMES |
584 |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
585 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
586 |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
587 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
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 |
591 |
#ifdef _DEBUG_PSNR |
#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); |
607 |
} |
} |
608 |
|
|
609 |
|
|
610 |
static __inline void inc_frame_num(Encoder * pEnc) |
void inc_frame_num(Encoder * pEnc) |
611 |
{ |
{ |
612 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
613 |
|
|
614 |
|
#ifdef BFRAMES |
615 |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
pEnc->mbParam.m_ticks = pEnc->mbParam.m_ticks % pEnc->mbParam.fbase; |
616 |
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
if (pEnc->mbParam.m_ticks < pEnc->last_sync) |
617 |
pEnc->mbParam.m_seconds = 1; |
pEnc->mbParam.m_seconds = 1; // more than 1 second since last I or P is not supported. |
|
// more than 1 second since last I or P is not supported. |
|
618 |
else |
else |
619 |
pEnc->mbParam.m_seconds = 0; |
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 |
static __inline void |
#ifdef BFRAMES |
634 |
queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
void queue_image(Encoder * pEnc, XVID_ENC_FRAME * pFrame) |
635 |
{ |
{ |
636 |
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
if (pEnc->queue_size >= pEnc->mbParam.max_bframes) |
637 |
{ |
{ |
654 |
pEnc->queue_size++; |
pEnc->queue_size++; |
655 |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
pEnc->queue_tail = (pEnc->queue_tail + 1) % pEnc->mbParam.max_bframes; |
656 |
} |
} |
657 |
|
#endif |
658 |
|
|
659 |
|
|
660 |
|
#ifdef BFRAMES |
661 |
/***************************************************************************** |
/***************************************************************************** |
662 |
* IPB frame encoder entry point |
* IPB frame encoder entry point |
663 |
* |
* |
838 |
|
|
839 |
pEnc->flush_bframes = 0; |
pEnc->flush_bframes = 0; |
840 |
|
|
841 |
|
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
842 |
|
* Well there was a separation here so i put it in ANSI C |
843 |
|
* comment style :-) |
844 |
|
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
845 |
|
|
846 |
emms(); |
emms(); |
847 |
|
|
848 |
// only inc frame num, adapt quant, etc. if we havent seen it before |
// only inc frame num, adapt quant, etc. if we havent seen it before |
1079 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1080 |
} |
} |
1081 |
|
|
1082 |
|
#endif |
1083 |
|
|
1084 |
|
|
1085 |
|
|
1086 |
/***************************************************************************** |
/***************************************************************************** |
1122 |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
pEnc->current->ticks = pEnc->mbParam.m_ticks; |
1123 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
1124 |
|
|
|
/* disable alternate scan flag if interlacing is not enabled */ |
|
|
if ((pEnc->current->global_flags & XVID_ALTERNATESCAN) && |
|
|
!(pEnc->current->global_flags & XVID_INTERLACING)) |
|
|
{ |
|
|
pEnc->current->global_flags -= XVID_ALTERNATESCAN; |
|
|
} |
|
|
|
|
1125 |
start_timer(); |
start_timer(); |
1126 |
if (image_input |
if (image_input |
1127 |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
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 |
|
#ifdef BFRAMES |
1505 |
#define DIVX501B481P "DivX501b481p" |
#define DIVX501B481P "DivX501b481p" |
1506 |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
if ((pEnc->global & XVID_GLOBAL_PACKED)) { |
1507 |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
BitstreamWriteUserData(bs, DIVX501B481P, strlen(DIVX501B481P)); |
1508 |
} |
} |
1509 |
|
#endif |
1510 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current, 1); |
1511 |
|
|
1512 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1547 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1548 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1549 |
|
|
|
pEnc->last_pframe = pEnc->current->ticks; |
|
|
pEnc->last_sync = pEnc->current->ticks; |
|
|
|
|
1550 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
1551 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1552 |
} |
} |
1556 |
|
|
1557 |
|
|
1558 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1559 |
#define BFRAME_SKIP_THRESHHOLD 30 |
#define BFRAME_SKIP_THRESHHOLD 16 |
1560 |
|
|
1561 |
static int |
static int |
1562 |
FrameCodeP(Encoder * pEnc, |
FrameCodeP(Encoder * pEnc, |
1580 |
|
|
1581 |
start_timer(); |
start_timer(); |
1582 |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1583 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height, |
1584 |
|
pEnc->current->global_flags & XVID_INTERLACING); |
1585 |
stop_edges_timer(); |
stop_edges_timer(); |
1586 |
|
|
1587 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1607 |
start_timer(); |
start_timer(); |
1608 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
1609 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
|
if (bIntra == 0) MotionEstimationHinted(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
|
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
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, |
1625 |
} |
} |
1626 |
stop_motion_timer(); |
stop_motion_timer(); |
1627 |
|
|
1628 |
if (bIntra == 1) return FrameCodeI(pEnc, bs, pBits); |
if (bIntra == 1) { |
1629 |
|
return FrameCodeI(pEnc, bs, pBits); |
1630 |
|
} |
1631 |
|
|
1632 |
pEnc->current->coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
1633 |
|
|
1638 |
|
|
1639 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
1640 |
|
|
1641 |
pEnc->sStat.iTextBits = pEnc->sStat.iMvSum = pEnc->sStat.iMvCount = |
pEnc->sStat.iTextBits = 0; |
1642 |
|
pEnc->sStat.iMvSum = 0; |
1643 |
|
pEnc->sStat.iMvCount = 0; |
1644 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1645 |
|
|
1646 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1675 |
|
|
1676 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1677 |
|
|
|
if (pMB->mode != MODE_NOT_CODED) |
|
1678 |
pMB->cbp = |
pMB->cbp = |
1679 |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1680 |
dct_codes, qcoeff); |
dct_codes, qcoeff); |
1702 |
|
|
1703 |
/* Finished processing the MB, now check if to CODE or SKIP */ |
/* Finished processing the MB, now check if to CODE or SKIP */ |
1704 |
|
|
1705 |
if ((pMB->mode == MODE_NOT_CODED) || |
if (pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
1706 |
(pMB->cbp == 0 && pMB->mode == MODE_INTER && pMB->mvs[0].x == 0 && |
pMB->mvs[0].y == 0) { |
|
pMB->mvs[0].y == 0 && pMB->dquant == NO_CHANGE)) { |
|
1707 |
|
|
1708 |
/* This is a candidate for SKIPping, but check intermediate B-frames first */ |
/* 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; |
int bSkip = 1; |
|
pMB->mode = MODE_NOT_CODED; |
|
1713 |
|
|
1714 |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
for (k=pEnc->bframenum_head; k< pEnc->bframenum_tail; k++) |
1715 |
{ |
{ |
|
int iSAD; |
|
1716 |
iSAD = sad16(pEnc->reference->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
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, |
pEnc->bframes[k]->image.y + 16*y*pEnc->mbParam.edged_width + 16*x, |
1718 |
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
pEnc->mbParam.edged_width,BFRAME_SKIP_THRESHHOLD); |
1719 |
if (iSAD >= BFRAME_SKIP_THRESHHOLD * pMB->quant) |
if (iSAD >= BFRAME_SKIP_THRESHHOLD) |
1720 |
{ bSkip = 0; |
{ bSkip = 0; |
1721 |
break; |
break; |
1722 |
} |
} |
1723 |
} |
} |
1724 |
if (!bSkip) |
if (!bSkip) |
1725 |
{ |
{ |
1726 |
VECTOR predMV; |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
1727 |
predMV = get_pmv2(pEnc->current->mbs, pEnc->mbParam.mb_width, 0, x, y, 0); |
{ pMB->cbp &= 0x3C; /* keep only bits 5-2 */ |
1728 |
pMB->pmvs[0].x = -predMV.x; pMB->pmvs[0].y = -predMV.y; |
qcoeff[4*64+0]=0; /* zero, because DC for INTRA MBs DC value is saved */ |
1729 |
pMB->mode = MODE_INTER; |
qcoeff[5*64+0]=0; |
1730 |
pMB->cbp = 0; |
} |
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 MBSkip(bs); |
else |
1735 |
|
MBSkip(bs); |
1736 |
|
|
1737 |
|
#else |
1738 |
|
MBSkip(bs); /* without B-frames, no precautions are needed */ |
1739 |
|
|
1740 |
|
#endif |
1741 |
|
|
1742 |
} else { |
} else { |
1743 |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
if (pEnc->current->global_flags & XVID_GREYSCALE) |
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) % |
pEnc->time_pp = ((int32_t)pEnc->mbParam.fbase - (int32_t)pEnc->last_pframe + (int32_t)pEnc->current->ticks) % |
1812 |
(int32_t)pEnc->mbParam.fbase; |
(int32_t)pEnc->mbParam.fbase; |
1813 |
pEnc->last_pframe = pEnc->current->ticks; |
pEnc->last_pframe = pEnc->current->ticks; |
1814 |
|
#endif |
1815 |
|
|
1816 |
return 0; // inter |
return 0; // inter |
1817 |
} |
} |
1818 |
|
|
1819 |
|
|
1820 |
static __inline void |
#ifdef BFRAMES |
1821 |
|
static void |
1822 |
FrameCodeB(Encoder * pEnc, |
FrameCodeB(Encoder * pEnc, |
1823 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
1824 |
Bitstream * bs, |
Bitstream * bs, |
1827 |
int16_t dct_codes[6 * 64]; |
int16_t dct_codes[6 * 64]; |
1828 |
int16_t qcoeff[6 * 64]; |
int16_t qcoeff[6 * 64]; |
1829 |
uint32_t x, y; |
uint32_t x, y; |
1830 |
|
VECTOR forward; |
1831 |
|
VECTOR backward; |
1832 |
|
|
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; |
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, |
1851 |
pEnc->mbParam.height); |
pEnc->mbParam.height, |
1852 |
|
frame->global_flags & XVID_INTERLACING); |
1853 |
start_timer(); |
start_timer(); |
1854 |
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, |
1855 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1859 |
// backward |
// backward |
1860 |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1861 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1862 |
pEnc->mbParam.height); |
pEnc->mbParam.height, |
1863 |
|
frame->global_flags & XVID_INTERLACING); |
1864 |
start_timer(); |
start_timer(); |
1865 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1866 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1896 |
|
|
1897 |
|
|
1898 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1899 |
|
// reset prediction |
1900 |
|
|
1901 |
|
forward.x = 0; |
1902 |
|
forward.y = 0; |
1903 |
|
backward.x = 0; |
1904 |
|
backward.y = 0; |
1905 |
|
|
1906 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1907 |
MACROBLOCK * const mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *f_mb = |
1908 |
int direction = pEnc->global & XVID_ALTERNATESCAN ? 2 : 0; |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
1909 |
|
MACROBLOCK *b_mb = |
1910 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1911 |
|
MACROBLOCK *mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1912 |
|
|
1913 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1914 |
if (mb->mode == MODE_NOT_CODED) { |
if (mb->mode == MODE_NOT_CODED) { |
1915 |
//mb->mvs[0].x = mb->mvs[0].y = mb->cbp = 0; |
mb->mvs[0].x = 0; |
1916 |
|
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 |
|
|
|
if (mb->mode != MODE_DIRECT_NONE_MV) { |
|
1925 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1926 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1927 |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1928 |
&pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterV, &pEnc->vInterHV, |
1929 |
dct_codes); |
dct_codes); |
1930 |
|
|
|
if (mb->mode == MODE_DIRECT_NO4V) mb->mode = MODE_DIRECT; |
|
1931 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
|
|
|
1932 |
mb->cbp = |
mb->cbp = |
1933 |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
MBTransQuantInterBVOP(&pEnc->mbParam, frame, mb, dct_codes, qcoeff); |
1934 |
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1935 |
|
|
1936 |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
if ( (mb->mode == MODE_DIRECT) && (mb->cbp == 0) |
1937 |
&& (mb->pmvs[3].x == 0) && (mb->pmvs[3].y == 0) ) { |
&& (mb->deltamv.x == 0) && (mb->deltamv.y == 0) ) { |
1938 |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
mb->mode = MODE_DIRECT_NONE_MV; // skipped |
1939 |
} |
} |
1940 |
|
|
1941 |
|
/* update predictors for forward and backward vectors */ |
1942 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
1943 |
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1944 |
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1945 |
|
forward.x = mb->mvs[0].x; |
1946 |
|
forward.y = mb->mvs[0].y; |
1947 |
|
} |
1948 |
|
|
1949 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
1950 |
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1951 |
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1952 |
|
backward.x = mb->b_mvs[0].x; |
1953 |
|
backward.y = mb->b_mvs[0].y; |
1954 |
} |
} |
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 |
#ifdef BFRAMES_DEC_DEBUG |
1959 |
BFRAME_DEBUG |
BFRAME_DEBUG |
1960 |
#endif |
#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, direction); |
&pEnc->sStat); |
1964 |
stop_coding_timer(); |
stop_coding_timer(); |
1965 |
} |
} |
1966 |
} |
} |
1979 |
} |
} |
1980 |
#endif |
#endif |
1981 |
} |
} |
1982 |
|
#endif |
1983 |
|
|
1984 |
|
|
1985 |
/* in case internal output is needed somewhere... */ |
/* in case internal output is needed somewhere... */ |