3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Encoder main module - |
* - Encoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* Copyright(C) 2002 Michael Militzer <isibaar@xvid.org> |
7 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
* 2002-2003 Peter Ross <pross@xvid.org> |
8 |
* to use this software module in hardware or software products are |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
44 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
45 |
#include "utils/emms.h" |
#include "utils/emms.h" |
46 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
|
#include "quant/adapt_quant.h" |
|
47 |
#include "quant/quant_matrix.h" |
#include "quant/quant_matrix.h" |
48 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
49 |
|
|
205 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
206 |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
pEnc->temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * |
207 |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
208 |
|
if (pEnc->temp_dquants==NULL) |
209 |
|
goto xvid_err_memory1a; |
210 |
} |
} |
|
/* XXX: error checking */ |
|
211 |
|
|
212 |
/* bframes */ |
/* bframes */ |
213 |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
pEnc->mbParam.max_bframes = MAX(create->max_bframes, 0); |
225 |
|
|
226 |
/* max keyframe interval */ |
/* max keyframe interval */ |
227 |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
pEnc->mbParam.iMaxKeyInterval = create->max_key_interval <= 0 ? |
228 |
10 * pEnc->mbParam.fbase / pEnc->mbParam.fincr : create->max_key_interval; |
(10 * pEnc->mbParam.fbase) / pEnc->mbParam.fincr : create->max_key_interval; |
|
|
|
|
/* Bitrate allocator defaults |
|
|
|
|
|
if ((create->min_quantizer <= 0) || (create->min_quantizer > 31)) |
|
|
create->min_quantizer = 1; |
|
|
|
|
|
if ((create->max_quantizer <= 0) || (create->max_quantizer > 31)) |
|
|
create->max_quantizer = 31; |
|
|
|
|
|
if (create->max_quantizer < create->min_quantizer) |
|
|
create->max_quantizer = create->min_quantizer; */ |
|
|
|
|
229 |
|
|
230 |
/* allocate working frame-image memory */ |
/* allocate working frame-image memory */ |
231 |
|
|
498 |
xvid_free(pEnc->current); |
xvid_free(pEnc->current); |
499 |
xvid_free(pEnc->reference); |
xvid_free(pEnc->reference); |
500 |
|
|
501 |
|
xvid_err_memory1a: |
502 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
503 |
xvid_free(pEnc->temp_dquants); |
xvid_free(pEnc->temp_dquants); |
504 |
} |
} |
687 |
data.frame_num = frame->frame_num; |
data.frame_num = frame->frame_num; |
688 |
|
|
689 |
if (opt == XVID_PLG_BEFORE) { |
if (opt == XVID_PLG_BEFORE) { |
690 |
data.type = XVID_TYPE_AUTO; |
data.type = *type; |
691 |
data.quant = 0; |
data.quant = *quant; |
692 |
|
|
693 |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQDQUANTS)) { |
694 |
data.dquant = pEnc->temp_dquants; |
data.dquant = pEnc->temp_dquants; |
696 |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
memset(data.dquant, 0, data.mb_width*data.mb_height); |
697 |
} |
} |
698 |
|
|
699 |
/* todo: [vol,vop,motion]_flags*/ |
data.vol_flags = frame->vol_flags; |
700 |
|
data.vop_flags = frame->vop_flags; |
701 |
|
data.motion_flags = frame->motion_flags; |
702 |
|
|
703 |
} else { // XVID_PLG_AFTER |
} else { /* XVID_PLG_AFTER */ |
704 |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
if ((pEnc->mbParam.plugin_flags & XVID_REQORIGINAL)) { |
705 |
data.original.csp = XVID_CSP_USER; |
data.original.csp = XVID_CSP_USER; |
706 |
data.original.plane[0] = original->y; |
data.original.plane[0] = original->y; |
795 |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
frame->mbs[j*pEnc->mbParam.mb_width + i].dquant = 0; |
796 |
} |
} |
797 |
} |
} |
798 |
/* todo: [vol,vop,motion]_flags*/ |
|
799 |
|
frame->vol_flags = data.vol_flags; |
800 |
|
frame->vop_flags = data.vop_flags; |
801 |
|
frame->motion_flags = data.motion_flags; |
802 |
} |
} |
803 |
} |
} |
804 |
|
|
1037 |
goto done; /* nothing to encode yet; encoder lag */ |
goto done; /* nothing to encode yet; encoder lag */ |
1038 |
} |
} |
1039 |
|
|
1040 |
// the current FRAME becomes the reference |
/* the current FRAME becomes the reference */ |
1041 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->reference); |
1042 |
|
|
1043 |
// remove frame from encoding-queue (head), and move it into the current |
/* remove frame from encoding-queue (head), and move it into the current */ |
1044 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1045 |
frame = &pEnc->queue[pEnc->queue_head].frame; |
frame = &pEnc->queue[pEnc->queue_head].frame; |
1046 |
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
pEnc->queue_head = (pEnc->queue_head + 1) % (pEnc->mbParam.max_bframes+1); |
1156 |
|
|
1157 |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
if (type == I_VOP && (pEnc->mbParam.global_flags & XVID_GLOBAL_CLOSED_GOP) && pEnc->bframenum_tail > 0) { |
1158 |
|
|
1159 |
// place this frame back on the encoding-queue (head) |
/* place this frame back on the encoding-queue (head) */ |
1160 |
// we will deal with it next time |
/* we will deal with it next time */ |
1161 |
dec_frame_num(pEnc); |
dec_frame_num(pEnc); |
1162 |
pEnc->iFrameNum--; |
pEnc->iFrameNum--; |
1163 |
|
|
1165 |
pEnc->queue_size++; |
pEnc->queue_size++; |
1166 |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
image_swap(&pEnc->current->image, &pEnc->queue[pEnc->queue_head].image); |
1167 |
|
|
1168 |
// grab the last frame from the bframe-queue |
/* grab the last frame from the bframe-queue */ |
1169 |
|
|
1170 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
1171 |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
SWAP(FRAMEINFO*, pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
1227 |
* encode this frame as an p-vop |
* encode this frame as an p-vop |
1228 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
1229 |
|
|
1230 |
} else { // (type == P_VOP || type == S_VOP) |
} else { /* (type == P_VOP || type == S_VOP) */ |
1231 |
|
|
1232 |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
DPRINTF(XVID_DEBUG_DEBUG,"*** PFRAME bf: head=%i tail=%i queue: head=%i tail=%i size=%i\n", |
1233 |
pEnc->bframenum_head, pEnc->bframenum_tail, |
pEnc->bframenum_head, pEnc->bframenum_tail, |
1407 |
pEnc->fMvPrevSigma = -1; |
pEnc->fMvPrevSigma = -1; |
1408 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1409 |
|
|
|
/* XXX: hinted me |
|
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
|
HintedMEGet(pEnc, 1); |
|
|
}*/ |
|
|
|
|
1410 |
return 1; /* intra */ |
return 1; /* intra */ |
1411 |
} |
} |
1412 |
|
|
1453 |
|
|
1454 |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
1455 |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
|
//pEnc->current->quarterpel = pEnc->mbParam.m_quarterpel; |
|
1456 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1457 |
|
|
1458 |
if (!force_inter) |
if (!force_inter) |
1476 |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
SetMacroblockQuants(&pEnc->mbParam, pEnc->current); |
1477 |
|
|
1478 |
start_timer(); |
start_timer(); |
1479 |
/*if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
bIntra = MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
HintedMESet(pEnc, &bIntra); |
|
|
else*/ |
|
|
bIntra = |
|
|
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
|
1480 |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1481 |
iLimit); |
iLimit); |
1482 |
|
|
1721 |
|
|
1722 |
emms(); |
emms(); |
1723 |
|
|
|
/* XXX: hinted me |
|
|
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
|
HintedMEGet(pEnc, 0); |
|
|
}*/ |
|
|
|
|
1724 |
if (pEnc->current->sStat.iMvCount == 0) |
if (pEnc->current->sStat.iMvCount == 0) |
1725 |
pEnc->current->sStat.iMvCount = 1; |
pEnc->current->sStat.iMvCount = 1; |
1726 |
|
|
1745 |
pEnc->fMvPrevSigma = fSigma; |
pEnc->fMvPrevSigma = fSigma; |
1746 |
|
|
1747 |
/* frame drop code */ |
/* frame drop code */ |
1748 |
//DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
#if 0 |
1749 |
|
DPRINTF(XVID_DEBUG_DEBUG, "kmu %i %i %i\n", pEnc->current->sStat.kblks, pEnc->current->sStat.mblks, pEnc->current->sStat.ublks); |
1750 |
|
#endif |
1751 |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
if (pEnc->current->sStat.kblks + pEnc->current->sStat.mblks < |
1752 |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
(pEnc->mbParam.frame_drop_ratio * mb_width * mb_height) / 100) |
1753 |
{ |
{ |
1763 |
pEnc->current->quant = pEnc->reference->quant; |
pEnc->current->quant = pEnc->reference->quant; |
1764 |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
pEnc->current->motion_flags = pEnc->reference->motion_flags; |
1765 |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
pEnc->current->rounding_type = pEnc->reference->rounding_type; |
|
//pEnc->current->quarterpel = pEnc->reference->quarterpel; |
|
1766 |
pEnc->current->fcode = pEnc->reference->fcode; |
pEnc->current->fcode = pEnc->reference->fcode; |
1767 |
pEnc->current->bcode = pEnc->reference->bcode; |
pEnc->current->bcode = pEnc->reference->bcode; |
1768 |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->current->image, &pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
1828 |
} |
} |
1829 |
#endif |
#endif |
1830 |
|
|
|
//frame->quarterpel = pEnc->mbParam.m_quarterpel; |
|
|
|
|
1831 |
/* forward */ |
/* forward */ |
1832 |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1833 |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1849 |
stop_inter_timer(); |
stop_inter_timer(); |
1850 |
|
|
1851 |
start_timer(); |
start_timer(); |
|
|
|
1852 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
1853 |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
((int32_t)(pEnc->current->stamp - frame->stamp)), /* time_bp */ |
1854 |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
((int32_t)(pEnc->current->stamp - pEnc->reference->stamp)), /* time_pp */ |
1856 |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1857 |
pEnc->current, b_ref, &pEnc->vInterH, |
pEnc->current, b_ref, &pEnc->vInterH, |
1858 |
&pEnc->vInterV, &pEnc->vInterHV); |
&pEnc->vInterV, &pEnc->vInterHV); |
|
|
|
|
|
|
1859 |
stop_motion_timer(); |
stop_motion_timer(); |
1860 |
|
|
|
/* |
|
|
if (test_quant_type(&pEnc->mbParam, pEnc->current)) { |
|
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
|
|
} |
|
|
*/ |
|
|
|
|
1861 |
frame->coding_type = B_VOP; |
frame->coding_type = B_VOP; |
1862 |
|
|
1863 |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |
set_timecodes(frame, pEnc->reference,pEnc->mbParam.fbase); |