1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - ME-based Frame Type Decision - |
5 |
|
* |
6 |
|
* Copyright(C) 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net> |
7 |
|
* |
8 |
|
* This program is free software ; you can redistribute it and/or modify |
9 |
|
* it under the terms of the GNU General Public License as published by |
10 |
|
* the Free Software Foundation ; either version 2 of the License, or |
11 |
|
* (at your option) any later version. |
12 |
|
* |
13 |
|
* This program is distributed in the hope that it will be useful, |
14 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
15 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
* GNU General Public License for more details. |
17 |
|
* |
18 |
|
* You should have received a copy of the GNU General Public License |
19 |
|
* along with this program ; if not, write to the Free Software |
20 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
|
* |
22 |
|
* $Id$ |
23 |
|
* |
24 |
|
****************************************************************************/ |
25 |
|
|
26 |
|
#include "../encoder.h" |
27 |
|
#include "../prediction/mbprediction.h" |
28 |
|
#include "estimation.h" |
29 |
|
#include "motion.h" |
30 |
|
#include "sad.h" |
31 |
|
#include "gmc.h" |
32 |
|
#include "../utils/emms.h" |
33 |
|
#include "motion_inlines.h" |
34 |
|
|
35 |
|
|
36 |
|
#define INTRA_THRESH 2000 |
37 |
|
#define INTER_THRESH 40 |
38 |
|
#define INTRA_THRESH2 90 |
39 |
|
|
40 |
|
/* when we are in 1/I_SENS_TH before forced keyframe, we start to decrese i-frame threshold */ |
41 |
|
#define I_SENS_TH 3 |
42 |
|
|
43 |
|
/* how much we subtract from each p-frame threshold for 2nd, 3rd etc. b-frame in a row */ |
44 |
|
#define P_SENS_BIAS 18 |
45 |
|
|
46 |
|
/* .. but never below INTER_THRESH_MIN */ |
47 |
|
#define INTER_THRESH_MIN 5 |
48 |
|
|
49 |
|
static void |
50 |
|
CheckCandidate32I(const int x, const int y, SearchData * const data, const unsigned int Direction) |
51 |
|
{ |
52 |
|
/* maximum speed */ |
53 |
|
int32_t sad; |
54 |
|
|
55 |
|
if ( (x > data->max_dx) || (x < data->min_dx) |
56 |
|
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
57 |
|
|
58 |
|
sad = sad32v_c(data->Cur, data->RefP[0] + x + y*((int)data->iEdgedWidth), |
59 |
|
data->iEdgedWidth, data->temp); |
60 |
|
|
61 |
|
if (sad < *(data->iMinSAD)) { |
62 |
|
*(data->iMinSAD) = sad; |
63 |
|
data->currentMV[0].x = x; data->currentMV[0].y = y; |
64 |
|
data->dir = Direction; |
65 |
|
} |
66 |
|
if (data->temp[0] < data->iMinSAD[1]) { |
67 |
|
data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; } |
68 |
|
if (data->temp[1] < data->iMinSAD[2]) { |
69 |
|
data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; } |
70 |
|
if (data->temp[2] < data->iMinSAD[3]) { |
71 |
|
data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; } |
72 |
|
if (data->temp[3] < data->iMinSAD[4]) { |
73 |
|
data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; } |
74 |
|
} |
75 |
|
|
76 |
|
static __inline void |
77 |
|
MEanalyzeMB ( const uint8_t * const pRef, |
78 |
|
const uint8_t * const pCur, |
79 |
|
const int x, |
80 |
|
const int y, |
81 |
|
const MBParam * const pParam, |
82 |
|
MACROBLOCK * const pMBs, |
83 |
|
SearchData * const Data) |
84 |
|
{ |
85 |
|
|
86 |
|
int i; |
87 |
|
VECTOR pmv[3]; |
88 |
|
MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width]; |
89 |
|
|
90 |
|
unsigned int simplicity = 0; |
91 |
|
|
92 |
|
for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR; |
93 |
|
|
94 |
|
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, |
95 |
|
pParam->width, pParam->height, Data->iFcode - Data->qpel - 1, 0, 0); |
96 |
|
|
97 |
|
Data->Cur = pCur + (x + y * pParam->edged_width) * 16; |
98 |
|
Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16; |
99 |
|
|
100 |
|
pmv[0].x = pMB->mvs[0].x; |
101 |
|
pmv[0].y = pMB->mvs[0].y; |
102 |
|
|
103 |
|
CheckCandidate32I(pmv[0].x, pmv[0].y, Data, 0); |
104 |
|
|
105 |
|
if (*Data->iMinSAD > 200) { |
106 |
|
|
107 |
|
pmv[1].x = pmv[1].y = 0; |
108 |
|
|
109 |
|
/* median is only used as prediction. it doesn't have to be real */ |
110 |
|
if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0; |
111 |
|
else |
112 |
|
if (x == 1) /* left macroblock does not have any vector now */ |
113 |
|
Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */ |
114 |
|
else if (y == 1) /* top macroblock doesn't have it's vector */ |
115 |
|
Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */ |
116 |
|
else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */ |
117 |
|
|
118 |
|
pmv[2].x = Data->predMV.x; |
119 |
|
pmv[2].y = Data->predMV.y; |
120 |
|
|
121 |
|
if (!vector_repeats(pmv, 1)) |
122 |
|
CheckCandidate32I(pmv[1].x, pmv[1].y, Data, 1); |
123 |
|
if (!vector_repeats(pmv, 2)) |
124 |
|
CheckCandidate32I(pmv[2].x, pmv[2].y, Data, 2); |
125 |
|
|
126 |
|
if (*Data->iMinSAD > 500) { /* diamond only if needed */ |
127 |
|
unsigned int mask = make_mask(pmv, 3, Data->dir); |
128 |
|
xvid_me_DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate32I); |
129 |
|
} else simplicity++; |
130 |
|
|
131 |
|
if (*Data->iMinSAD > 500) /* refinement from 2-pixel to 1-pixel */ |
132 |
|
xvid_me_SubpelRefine(Data, CheckCandidate32I); |
133 |
|
else simplicity++; |
134 |
|
} else simplicity++; |
135 |
|
|
136 |
|
for (i = 0; i < 4; i++) { |
137 |
|
MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width]; |
138 |
|
MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i]; |
139 |
|
MB->mode = MODE_INTER; |
140 |
|
/* if we skipped some search steps, we have to assume that SAD would be lower with them */ |
141 |
|
MB->sad16 = Data->iMinSAD[i+1] - (simplicity<<7); |
142 |
|
if (MB->sad16 < 0) MB->sad16 = 0; |
143 |
|
} |
144 |
|
} |
145 |
|
|
146 |
|
int |
147 |
|
MEanalysis( const IMAGE * const pRef, |
148 |
|
const FRAMEINFO * const Current, |
149 |
|
const MBParam * const pParam, |
150 |
|
const int maxIntra, /* maximum number if non-I frames */ |
151 |
|
const int intraCount, /* number of non-I frames after last I frame; 0 if we force P/B frame */ |
152 |
|
const int bCount, /* number of B frames in a row */ |
153 |
|
const int b_thresh, |
154 |
|
const MACROBLOCK * const prev_mbs) |
155 |
|
{ |
156 |
|
uint32_t x, y, intra = 0; |
157 |
|
int sSAD = 0; |
158 |
|
MACROBLOCK * const pMBs = Current->mbs; |
159 |
|
const IMAGE * const pCurrent = &Current->image; |
160 |
|
int IntraThresh = INTRA_THRESH, |
161 |
|
InterThresh = INTER_THRESH + b_thresh, |
162 |
|
IntraThresh2 = INTRA_THRESH2; |
163 |
|
|
164 |
|
int blocks = 10; |
165 |
|
int complexity = 0; |
166 |
|
|
167 |
|
SearchData Data; |
168 |
|
Data.iEdgedWidth = pParam->edged_width; |
169 |
|
Data.iFcode = Current->fcode; |
170 |
|
Data.qpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0; |
171 |
|
Data.qpel_precision = 0; |
172 |
|
|
173 |
|
if (intraCount != 0) { |
174 |
|
if (intraCount < 30) { |
175 |
|
/* we're right after an I frame |
176 |
|
we increase thresholds to prevent consecutive i-frames */ |
177 |
|
if (intraCount < 10) IntraThresh += 15*(10 - intraCount)*(10 - intraCount); |
178 |
|
IntraThresh2 += 4*(30 - intraCount); |
179 |
|
} else if (I_SENS_TH*(maxIntra - intraCount) < maxIntra) { |
180 |
|
/* we're close to maximum. we decrease thresholds to catch any good keyframe */ |
181 |
|
IntraThresh -= IntraThresh*((maxIntra - I_SENS_TH*(maxIntra - intraCount))/maxIntra); |
182 |
|
IntraThresh2 -= IntraThresh2*((maxIntra - I_SENS_TH*(maxIntra - intraCount))/maxIntra); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
InterThresh -= P_SENS_BIAS * bCount; |
187 |
|
if (InterThresh < INTER_THRESH_MIN) InterThresh = INTER_THRESH_MIN; |
188 |
|
|
189 |
|
if (sadInit) (*sadInit) (); |
190 |
|
|
191 |
|
for (y = 1; y < pParam->mb_height-1; y += 2) { |
192 |
|
for (x = 1; x < pParam->mb_width-1; x += 2) { |
193 |
|
int i; |
194 |
|
blocks += 10; |
195 |
|
|
196 |
|
if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV; |
197 |
|
else { /* extrapolation of the vector found for last frame */ |
198 |
|
pMBs[x + y * pParam->mb_width].mvs[0].x = |
199 |
|
(prev_mbs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount; |
200 |
|
pMBs[x + y * pParam->mb_width].mvs[0].y = |
201 |
|
(prev_mbs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount; |
202 |
|
} |
203 |
|
|
204 |
|
MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data); |
205 |
|
|
206 |
|
for (i = 0; i < 4; i++) { |
207 |
|
int dev; |
208 |
|
MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width]; |
209 |
|
dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16, |
210 |
|
pParam->edged_width); |
211 |
|
|
212 |
|
complexity += MAX(dev, 300); |
213 |
|
if (dev + IntraThresh < pMB->sad16) { |
214 |
|
pMB->mode = MODE_INTRA; |
215 |
|
if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP; |
216 |
|
} |
217 |
|
|
218 |
|
if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) |
219 |
|
if (dev > 1000 && pMB->sad16 < 1000) |
220 |
|
sSAD += 512; |
221 |
|
|
222 |
|
sSAD += (dev < 3000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */ |
223 |
|
} |
224 |
|
} |
225 |
|
} |
226 |
|
complexity >>= 7; |
227 |
|
|
228 |
|
sSAD /= complexity + 4*blocks; |
229 |
|
|
230 |
|
if (sSAD > IntraThresh2) return I_VOP; |
231 |
|
if (sSAD > InterThresh) return P_VOP; |
232 |
|
emms(); |
233 |
|
return B_VOP; |
234 |
|
} |