Parent Directory | Revision Log
Revision 1569 - (view) (download)
1 : | edgomez | 1382 | /***************************************************************************** |
2 : | * | ||
3 : | * XVID MPEG-4 VIDEO CODEC | ||
4 : | * - Rate-Distortion Based Motion Estimation for P- and S- VOPs - | ||
5 : | * | ||
6 : | * Copyright(C) 2003 Radoslaw Czyz <xvid@syskin.cjb.net> | ||
7 : | * 2003 Michael Militzer <michael@xvid.org> | ||
8 : | * | ||
9 : | * This program is free software ; you can redistribute it and/or modify | ||
10 : | * it under the terms of the GNU General Public License as published by | ||
11 : | * the Free Software Foundation ; either version 2 of the License, or | ||
12 : | * (at your option) any later version. | ||
13 : | * | ||
14 : | * This program is distributed in the hope that it will be useful, | ||
15 : | * but WITHOUT ANY WARRANTY ; without even the implied warranty of | ||
16 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 : | * GNU General Public License for more details. | ||
18 : | * | ||
19 : | * You should have received a copy of the GNU General Public License | ||
20 : | * along with this program ; if not, write to the Free Software | ||
21 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
22 : | * | ||
23 : | syskin | 1569 | * $Id: estimation_rd_based.c,v 1.10 2004-12-09 04:58:12 syskin Exp $ |
24 : | edgomez | 1382 | * |
25 : | ****************************************************************************/ | ||
26 : | |||
27 : | /* RD mode decision and search */ | ||
28 : | |||
29 : | #include <assert.h> | ||
30 : | #include <stdio.h> | ||
31 : | #include <stdlib.h> | ||
32 : | #include <string.h> /* memcpy */ | ||
33 : | |||
34 : | #include "../encoder.h" | ||
35 : | #include "../bitstream/mbcoding.h" | ||
36 : | #include "../prediction/mbprediction.h" | ||
37 : | #include "../global.h" | ||
38 : | #include "../image/interpolate8x8.h" | ||
39 : | #include "estimation.h" | ||
40 : | #include "motion.h" | ||
41 : | #include "sad.h" | ||
42 : | #include "../bitstream/zigzag.h" | ||
43 : | #include "../quant/quant.h" | ||
44 : | #include "../bitstream/vlc_codes.h" | ||
45 : | #include "../dct/fdct.h" | ||
46 : | #include "motion_inlines.h" | ||
47 : | |||
48 : | /* rd = BITS_MULT*bits + LAMBDA*distortion */ | ||
49 : | #define LAMBDA ( (int)(BITS_MULT*1.0) ) | ||
50 : | |||
51 : | static __inline unsigned int | ||
52 : | Block_CalcBits( int16_t * const coeff, | ||
53 : | int16_t * const data, | ||
54 : | int16_t * const dqcoeff, | ||
55 : | const uint32_t quant, const int quant_type, | ||
56 : | uint32_t * cbp, | ||
57 : | const int block, | ||
58 : | const uint16_t * scan_table, | ||
59 : | syskin | 1506 | const unsigned int lambda, |
60 : | syskin | 1569 | const uint16_t * mpeg_quant_matrices, |
61 : | const unsigned int quant_sq) | ||
62 : | edgomez | 1382 | { |
63 : | int sum; | ||
64 : | int bits; | ||
65 : | int distortion = 0; | ||
66 : | |||
67 : | fdct(data); | ||
68 : | |||
69 : | if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); | ||
70 : | else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); | ||
71 : | |||
72 : | if (sum > 0) { | ||
73 : | *cbp |= 1 << (5 - block); | ||
74 : | bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); | ||
75 : | |||
76 : | if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); | ||
77 : | else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); | ||
78 : | |||
79 : | distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); | ||
80 : | } else { | ||
81 : | const static int16_t zero_block[64] = | ||
82 : | { | ||
83 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
84 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
85 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
86 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
87 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
88 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
89 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
90 : | 0, 0, 0, 0, 0, 0, 0, 0, | ||
91 : | }; | ||
92 : | bits = 0; | ||
93 : | distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); | ||
94 : | } | ||
95 : | |||
96 : | |||
97 : | syskin | 1569 | return bits + (lambda*distortion)/quant_sq; |
98 : | edgomez | 1382 | } |
99 : | |||
100 : | static __inline unsigned int | ||
101 : | Block_CalcBitsIntra(MACROBLOCK * pMB, | ||
102 : | const unsigned int x, | ||
103 : | const unsigned int y, | ||
104 : | const unsigned int mb_width, | ||
105 : | const uint32_t block, | ||
106 : | int16_t coeff[64], | ||
107 : | int16_t qcoeff[64], | ||
108 : | int16_t dqcoeff[64], | ||
109 : | int16_t predictors[8], | ||
110 : | const uint32_t quant, | ||
111 : | const int quant_type, | ||
112 : | unsigned int bits[2], | ||
113 : | unsigned int cbp[2], | ||
114 : | syskin | 1506 | unsigned int lambda, |
115 : | syskin | 1569 | const uint16_t * mpeg_quant_matrices, |
116 : | const unsigned int quant_sq) | ||
117 : | edgomez | 1382 | { |
118 : | int direction; | ||
119 : | int16_t *pCurrent; | ||
120 : | unsigned int i, coded; | ||
121 : | unsigned int distortion = 0; | ||
122 : | const uint32_t iDcScaler = get_dc_scaler(quant, block < 4); | ||
123 : | |||
124 : | fdct(coeff); | ||
125 : | |||
126 : | if (quant_type) { | ||
127 : | quant_h263_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); | ||
128 : | dequant_h263_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); | ||
129 : | } else { | ||
130 : | quant_mpeg_intra(qcoeff, coeff, quant, iDcScaler, mpeg_quant_matrices); | ||
131 : | dequant_mpeg_intra(dqcoeff, qcoeff, quant, iDcScaler, mpeg_quant_matrices); | ||
132 : | } | ||
133 : | |||
134 : | predict_acdc(pMB-(x+mb_width*y), x, y, mb_width, block, qcoeff, | ||
135 : | edgomez | 1472 | quant, iDcScaler, predictors, 0); |
136 : | edgomez | 1382 | |
137 : | direction = pMB->acpred_directions[block]; | ||
138 : | pCurrent = pMB->pred_values[block]; | ||
139 : | |||
140 : | /* store current coeffs to pred_values[] for future prediction */ | ||
141 : | pCurrent[0] = qcoeff[0] * iDcScaler; | ||
142 : | edgomez | 1472 | pCurrent[0] = CLIP(pCurrent[0], -2048, 2047); |
143 : | edgomez | 1382 | for (i = 1; i < 8; i++) { |
144 : | pCurrent[i] = qcoeff[i]; | ||
145 : | pCurrent[i + 7] = qcoeff[i * 8]; | ||
146 : | } | ||
147 : | |||
148 : | /* dc prediction */ | ||
149 : | qcoeff[0] = qcoeff[0] - predictors[0]; | ||
150 : | |||
151 : | if (block < 4) bits[1] = bits[0] = dcy_tab[qcoeff[0] + 255].len; | ||
152 : | else bits[1] = bits[0] = dcc_tab[qcoeff[0] + 255].len; | ||
153 : | |||
154 : | /* calc cost before ac prediction */ | ||
155 : | bits[0] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[0]); | ||
156 : | if (coded > 0) cbp[0] |= 1 << (5 - block); | ||
157 : | |||
158 : | /* apply ac prediction & calc cost*/ | ||
159 : | if (direction == 1) { | ||
160 : | for (i = 1; i < 8; i++) { | ||
161 : | qcoeff[i] -= predictors[i]; | ||
162 : | predictors[i] = qcoeff[i]; | ||
163 : | } | ||
164 : | } else { /* acpred_direction == 2 */ | ||
165 : | for (i = 1; i < 8; i++) { | ||
166 : | qcoeff[i*8] -= predictors[i]; | ||
167 : | predictors[i] = qcoeff[i*8]; | ||
168 : | } | ||
169 : | } | ||
170 : | |||
171 : | bits[1] += coded = CodeCoeffIntra_CalcBits(qcoeff, scan_tables[direction]); | ||
172 : | if (coded > 0) cbp[1] |= 1 << (5 - block); | ||
173 : | |||
174 : | distortion = sse8_16bit(coeff, dqcoeff, 8*sizeof(int16_t)); | ||
175 : | |||
176 : | syskin | 1569 | return (lambda*distortion)/quant_sq; |
177 : | edgomez | 1382 | } |
178 : | |||
179 : | |||
180 : | |||
181 : | static void | ||
182 : | CheckCandidateRD16(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
183 : | { | ||
184 : | |||
185 : | int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; | ||
186 : | int32_t rd = 0; | ||
187 : | VECTOR * current; | ||
188 : | const uint8_t * ptr; | ||
189 : | int i, t, xc, yc; | ||
190 : | unsigned cbp = 0; | ||
191 : | |||
192 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
193 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
194 : | |||
195 : | if (!data->qpel_precision) { | ||
196 : | ptr = GetReference(x, y, data); | ||
197 : | current = data->currentMV; | ||
198 : | xc = x; yc = y; | ||
199 : | } else { /* x and y are in 1/4 precision */ | ||
200 : | ptr = xvid_me_interpolate16x16qpel(x, y, 0, data); | ||
201 : | current = data->currentQMV; | ||
202 : | xc = x/2; yc = y/2; | ||
203 : | } | ||
204 : | |||
205 : | for(i = 0; i < 4; i++) { | ||
206 : | int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); | ||
207 : | transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); | ||
208 : | syskin | 1569 | rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, |
209 : | data->quant_type, &cbp, i, data->scan_table, data->lambda[i], | ||
210 : | data->mpeg_quant_matrices, data->quant_sq); | ||
211 : | edgomez | 1382 | } |
212 : | |||
213 : | syskin | 1564 | rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
214 : | edgomez | 1382 | |
215 : | if (data->temp[0] + t < data->iMinSAD[1]) { | ||
216 : | data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); } | ||
217 : | if (data->temp[1] < data->iMinSAD[2]) { | ||
218 : | data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | (cbp&16); } | ||
219 : | if (data->temp[2] < data->iMinSAD[3]) { | ||
220 : | data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | (cbp&8); } | ||
221 : | if (data->temp[3] < data->iMinSAD[4]) { | ||
222 : | data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); } | ||
223 : | |||
224 : | rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; | ||
225 : | |||
226 : | if (rd >= data->iMinSAD[0]) return; | ||
227 : | |||
228 : | /* chroma */ | ||
229 : | xc = (xc >> 1) + roundtab_79[xc & 0x3]; | ||
230 : | yc = (yc >> 1) + roundtab_79[yc & 0x3]; | ||
231 : | |||
232 : | /* chroma U */ | ||
233 : | ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); | ||
234 : | transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); | ||
235 : | syskin | 1569 | rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
236 : | &cbp, 4, data->scan_table, data->lambda[4], | ||
237 : | data->mpeg_quant_matrices, data->quant_sq); | ||
238 : | edgomez | 1382 | if (rd >= data->iMinSAD[0]) return; |
239 : | |||
240 : | /* chroma V */ | ||
241 : | ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); | ||
242 : | transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); | ||
243 : | syskin | 1569 | rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
244 : | &cbp, 5, data->scan_table, data->lambda[5], | ||
245 : | data->mpeg_quant_matrices, data->quant_sq); | ||
246 : | edgomez | 1382 | |
247 : | rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; | ||
248 : | |||
249 : | if (rd < data->iMinSAD[0]) { | ||
250 : | data->iMinSAD[0] = rd; | ||
251 : | current[0].x = x; current[0].y = y; | ||
252 : | data->dir = Direction; | ||
253 : | *data->cbp = cbp; | ||
254 : | } | ||
255 : | } | ||
256 : | |||
257 : | static void | ||
258 : | CheckCandidateRD8(const int x, const int y, SearchData * const data, const unsigned int Direction) | ||
259 : | { | ||
260 : | |||
261 : | int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; | ||
262 : | int32_t rd; | ||
263 : | VECTOR * current; | ||
264 : | const uint8_t * ptr; | ||
265 : | unsigned int cbp = 0; | ||
266 : | |||
267 : | if ( (x > data->max_dx) || (x < data->min_dx) | ||
268 : | || (y > data->max_dy) || (y < data->min_dy) ) return; | ||
269 : | |||
270 : | if (!data->qpel_precision) { | ||
271 : | ptr = GetReference(x, y, data); | ||
272 : | current = data->currentMV; | ||
273 : | } else { /* x and y are in 1/4 precision */ | ||
274 : | ptr = xvid_me_interpolate8x8qpel(x, y, 0, 0, data); | ||
275 : | current = data->currentQMV; | ||
276 : | } | ||
277 : | |||
278 : | transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth); | ||
279 : | syskin | 1569 | rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
280 : | &cbp, 5, data->scan_table, data->lambda[0], | ||
281 : | data->mpeg_quant_matrices, data->quant_sq); | ||
282 : | syskin | 1564 | rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
283 : | edgomez | 1382 | |
284 : | if (rd < data->iMinSAD[0]) { | ||
285 : | *data->cbp = cbp; | ||
286 : | data->iMinSAD[0] = rd; | ||
287 : | current[0].x = x; current[0].y = y; | ||
288 : | data->dir = Direction; | ||
289 : | } | ||
290 : | } | ||
291 : | |||
292 : | |||
293 : | static int | ||
294 : | findRD_inter(SearchData * const Data, | ||
295 : | const int x, const int y, | ||
296 : | const MBParam * const pParam, | ||
297 : | const uint32_t MotionFlags) | ||
298 : | { | ||
299 : | int i; | ||
300 : | int32_t bsad[5]; | ||
301 : | |||
302 : | if (Data->qpel) { | ||
303 : | for(i = 0; i < 5; i++) { | ||
304 : | Data->currentMV[i].x = Data->currentQMV[i].x/2; | ||
305 : | Data->currentMV[i].y = Data->currentQMV[i].y/2; | ||
306 : | } | ||
307 : | Data->qpel_precision = 1; | ||
308 : | CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, Data, 255); | ||
309 : | |||
310 : | if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */ | ||
311 : | for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i]; | ||
312 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
313 : | syskin | 1564 | pParam->width, pParam->height, Data->iFcode - Data->qpel, 1); |
314 : | edgomez | 1382 | Data->qpel_precision = 0; |
315 : | if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1) | ||
316 : | CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); | ||
317 : | } | ||
318 : | |||
319 : | } else { /* not qpel */ | ||
320 : | |||
321 : | CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255); | ||
322 : | } | ||
323 : | |||
324 : | if (MotionFlags&XVID_ME_EXTSEARCH_RD) | ||
325 : | xvid_me_SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, 255, CheckCandidateRD16); | ||
326 : | |||
327 : | if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) | ||
328 : | syskin | 1478 | xvid_me_SubpelRefine(Data->currentMV[0], Data, CheckCandidateRD16, 0); |
329 : | edgomez | 1382 | |
330 : | if (Data->qpel) { | ||
331 : | if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */ | ||
332 : | for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) { | ||
333 : | Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */ | ||
334 : | Data->currentQMV[i].y = 2 * Data->currentMV[i].y; | ||
335 : | } | ||
336 : | |||
337 : | /* preparing for qpel-precision search */ | ||
338 : | Data->qpel_precision = 1; | ||
339 : | get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, | ||
340 : | syskin | 1564 | pParam->width, pParam->height, Data->iFcode, 2); |
341 : | edgomez | 1382 | } |
342 : | syskin | 1441 | if (MotionFlags & XVID_ME_QUARTERPELREFINE16_RD) { |
343 : | if (MotionFlags & XVID_ME_FASTREFINE16) | ||
344 : | FullRefine_Fast(Data, CheckCandidateRD16, 0); | ||
345 : | else | ||
346 : | syskin | 1478 | xvid_me_SubpelRefine(Data->currentQMV[0], Data, CheckCandidateRD16, 0); |
347 : | syskin | 1441 | } |
348 : | edgomez | 1382 | } |
349 : | |||
350 : | if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */ | ||
351 : | VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV; | ||
352 : | if (!MVequal(Data->predMV, *v)) | ||
353 : | CheckCandidateRD16(Data->predMV.x, Data->predMV.y, Data, 255); | ||
354 : | } | ||
355 : | return Data->iMinSAD[0]; | ||
356 : | } | ||
357 : | |||
358 : | static int | ||
359 : | findRD_inter4v(SearchData * const Data, | ||
360 : | MACROBLOCK * const pMB, const MACROBLOCK * const pMBs, | ||
361 : | const int x, const int y, | ||
362 : | const MBParam * const pParam, const uint32_t MotionFlags, | ||
363 : | const VECTOR * const backup) | ||
364 : | { | ||
365 : | |||
366 : | unsigned int cbp = 0, bits = 0, t = 0, i; | ||
367 : | SearchData Data2, *Data8 = &Data2; | ||
368 : | int sumx = 0, sumy = 0; | ||
369 : | int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64; | ||
370 : | uint8_t * ptr; | ||
371 : | |||
372 : | memcpy(Data8, Data, sizeof(SearchData)); | ||
373 : | |||
374 : | for (i = 0; i < 4; i++) { /* for all luma blocks */ | ||
375 : | |||
376 : | *Data8->iMinSAD = *(Data->iMinSAD + i + 1); | ||
377 : | *Data8->currentMV = *(Data->currentMV + i + 1); | ||
378 : | *Data8->currentQMV = *(Data->currentQMV + i + 1); | ||
379 : | Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
380 : | Data8->RefP[0] = Data->RefP[0] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
381 : | Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
382 : | Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
383 : | Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
384 : | *Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; /* copy corresponding cbp bit */ | ||
385 : | syskin | 1506 | Data8->lambda[0] = Data->lambda[i]; |
386 : | edgomez | 1382 | |
387 : | if(Data->qpel) { | ||
388 : | Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i); | ||
389 : | if (i != 0) t = d_mv_bits( Data8->currentQMV->x, Data8->currentQMV->y, | ||
390 : | syskin | 1564 | Data8->predMV, Data8->iFcode, 0); |
391 : | edgomez | 1382 | } else { |
392 : | Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i); | ||
393 : | if (i != 0) t = d_mv_bits( Data8->currentMV->x, Data8->currentMV->y, | ||
394 : | syskin | 1564 | Data8->predMV, Data8->iFcode, 0); |
395 : | edgomez | 1382 | } |
396 : | |||
397 : | get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, | ||
398 : | syskin | 1564 | pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1); |
399 : | edgomez | 1382 | |
400 : | *Data8->iMinSAD += BITS_MULT*t; | ||
401 : | |||
402 : | Data8->qpel_precision = Data8->qpel; | ||
403 : | /* checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far) */ | ||
404 : | { | ||
405 : | VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV; | ||
406 : | if (!MVequal (*v, backup[i+1]) ) | ||
407 : | CheckCandidateRD8(backup[i+1].x, backup[i+1].y, Data8, 255); | ||
408 : | } | ||
409 : | |||
410 : | if (Data8->qpel) { | ||
411 : | syskin | 1441 | int bsad = Data8->iMinSAD[0]; |
412 : | int bx = Data8->currentQMV->x; | ||
413 : | int by = Data8->currentQMV->y; | ||
414 : | |||
415 : | Data8->currentMV->x = Data8->currentQMV->x/2; | ||
416 : | Data8->currentMV->y = Data8->currentQMV->y/2; | ||
417 : | |||
418 : | edgomez | 1382 | if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */ |
419 : | Data8->qpel_precision = 0; | ||
420 : | get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, | ||
421 : | syskin | 1564 | pParam->width, pParam->height, Data8->iFcode - 1, 1); |
422 : | edgomez | 1382 | |
423 : | if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1) | ||
424 : | CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, Data8, 255); | ||
425 : | |||
426 : | if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) | ||
427 : | xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); | ||
428 : | |||
429 : | if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) | ||
430 : | syskin | 1478 | xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); |
431 : | edgomez | 1382 | |
432 : | syskin | 1441 | if(bsad > *Data8->iMinSAD) { /* we have found a better match */ |
433 : | bx = Data8->currentQMV->x = 2*Data8->currentMV->x; | ||
434 : | by = Data8->currentQMV->y = 2*Data8->currentMV->y; | ||
435 : | bsad = Data8->iMinSAD[0]; | ||
436 : | edgomez | 1382 | } |
437 : | |||
438 : | Data8->qpel_precision = 1; | ||
439 : | get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3, | ||
440 : | syskin | 1564 | pParam->width, pParam->height, Data8->iFcode, 2); |
441 : | edgomez | 1382 | |
442 : | } | ||
443 : | |||
444 : | syskin | 1441 | if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) { |
445 : | if (MotionFlags & XVID_ME_FASTREFINE8) | ||
446 : | FullRefine_Fast(Data8, CheckCandidateRD8, 0); | ||
447 : | syskin | 1478 | else xvid_me_SubpelRefine(Data->currentQMV[0], Data8, CheckCandidateRD8, 0); |
448 : | syskin | 1441 | } |
449 : | |||
450 : | if (bsad <= Data->iMinSAD[0]) { | ||
451 : | /* we have not found a better match */ | ||
452 : | Data8->iMinSAD[0] = bsad; | ||
453 : | Data8->currentQMV->x = bx; | ||
454 : | Data8->currentQMV->y = by; | ||
455 : | } | ||
456 : | |||
457 : | edgomez | 1382 | } else { /* not qpel */ |
458 : | |||
459 : | if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */ | ||
460 : | xvid_me_SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255, CheckCandidateRD8); | ||
461 : | |||
462 : | if (MotionFlags & XVID_ME_HALFPELREFINE8_RD) | ||
463 : | syskin | 1478 | xvid_me_SubpelRefine(Data->currentMV[0], Data8, CheckCandidateRD8, 0); /* halfpel refinement */ |
464 : | edgomez | 1382 | } |
465 : | |||
466 : | /* checking vector equal to predicion */ | ||
467 : | if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) { | ||
468 : | const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV; | ||
469 : | if (!MVequal(*v, Data8->predMV)) | ||
470 : | CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, Data8, 255); | ||
471 : | } | ||
472 : | |||
473 : | bits += *Data8->iMinSAD; | ||
474 : | if (bits >= Data->iMinSAD[0]) return bits; /* no chances for INTER4V */ | ||
475 : | |||
476 : | /* MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else */ | ||
477 : | if(Data->qpel) { | ||
478 : | pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x; | ||
479 : | pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y; | ||
480 : | pMB->qmvs[i] = *Data8->currentQMV; | ||
481 : | sumx += Data8->currentQMV->x/2; | ||
482 : | sumy += Data8->currentQMV->y/2; | ||
483 : | } else { | ||
484 : | pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x; | ||
485 : | pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y; | ||
486 : | sumx += Data8->currentMV->x; | ||
487 : | sumy += Data8->currentMV->y; | ||
488 : | } | ||
489 : | pMB->mvs[i] = *Data8->currentMV; | ||
490 : | pMB->sad8[i] = 4 * *Data8->iMinSAD; | ||
491 : | if (Data8->cbp[0]) cbp |= 1 << (5 - i); | ||
492 : | |||
493 : | } /* end - for all luma blocks */ | ||
494 : | |||
495 : | bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; | ||
496 : | |||
497 : | /* let's check chroma */ | ||
498 : | sumx = (sumx >> 3) + roundtab_76[sumx & 0xf]; | ||
499 : | sumy = (sumy >> 3) + roundtab_76[sumy & 0xf]; | ||
500 : | |||
501 : | /* chroma U */ | ||
502 : | ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[4], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); | ||
503 : | transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2); | ||
504 : | syskin | 1569 | bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, |
505 : | Data->scan_table, Data->lambda[4], Data->mpeg_quant_matrices, Data->quant_sq); | ||
506 : | edgomez | 1382 | |
507 : | if (bits >= *Data->iMinSAD) return bits; | ||
508 : | |||
509 : | /* chroma V */ | ||
510 : | ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefP[5], 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding); | ||
511 : | transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2); | ||
512 : | syskin | 1569 | bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, |
513 : | Data->scan_table, Data->lambda[5], Data->mpeg_quant_matrices, Data->quant_sq); | ||
514 : | edgomez | 1382 | |
515 : | bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len; | ||
516 : | |||
517 : | *Data->cbp = cbp; | ||
518 : | return bits; | ||
519 : | } | ||
520 : | |||
521 : | static int | ||
522 : | findRD_intra(SearchData * const Data, MACROBLOCK * pMB, | ||
523 : | const int x, const int y, const int mb_width) | ||
524 : | { | ||
525 : | unsigned int cbp[2] = {0, 0}, bits[2], i; | ||
526 : | unsigned int bits1 = BITS_MULT*1, bits2 = BITS_MULT*1; /* this one is ac/dc prediction flag bit */ | ||
527 : | unsigned int distortion = 0; | ||
528 : | |||
529 : | int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64, * dqcoeff = Data->dctSpace + 128; | ||
530 : | const uint32_t iQuant = Data->iQuant; | ||
531 : | int16_t predictors[6][8]; | ||
532 : | |||
533 : | for(i = 0; i < 4; i++) { | ||
534 : | int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
535 : | transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth); | ||
536 : | |||
537 : | |||
538 : | distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, i, in, coeff, dqcoeff, | ||
539 : | syskin | 1569 | predictors[i], iQuant, Data->quant_type, bits, cbp, |
540 : | Data->lambda[i], Data->mpeg_quant_matrices, Data->quant_sq); | ||
541 : | edgomez | 1382 | bits1 += distortion + BITS_MULT * bits[0]; |
542 : | bits2 += distortion + BITS_MULT * bits[1]; | ||
543 : | |||
544 : | if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) | ||
545 : | return bits1; | ||
546 : | } | ||
547 : | |||
548 : | bits1 += BITS_MULT*xvid_cbpy_tab[cbp[0]>>2].len; | ||
549 : | bits2 += BITS_MULT*xvid_cbpy_tab[cbp[1]>>2].len; | ||
550 : | |||
551 : | /*chroma U */ | ||
552 : | transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2); | ||
553 : | distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 4, in, coeff, dqcoeff, | ||
554 : | syskin | 1569 | predictors[4], iQuant, Data->quant_type, bits, cbp, |
555 : | Data->lambda[4], Data->mpeg_quant_matrices, Data->quant_sq); | ||
556 : | edgomez | 1382 | bits1 += distortion + BITS_MULT * bits[0]; |
557 : | bits2 += distortion + BITS_MULT * bits[1]; | ||
558 : | |||
559 : | if (bits1 >= Data->iMinSAD[0] && bits2 >= Data->iMinSAD[0]) | ||
560 : | return bits1; | ||
561 : | |||
562 : | /* chroma V */ | ||
563 : | transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2); | ||
564 : | distortion = Block_CalcBitsIntra(pMB, x, y, mb_width, 5, in, coeff, dqcoeff, | ||
565 : | syskin | 1569 | predictors[5], iQuant, Data->quant_type, bits, cbp, |
566 : | Data->lambda[5], Data->mpeg_quant_matrices, Data->quant_sq); | ||
567 : | edgomez | 1382 | |
568 : | bits1 += distortion + BITS_MULT * bits[0]; | ||
569 : | bits2 += distortion + BITS_MULT * bits[1]; | ||
570 : | |||
571 : | bits1 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[0] & 3) << 3)].len; | ||
572 : | bits2 += BITS_MULT*mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp[1] & 3) << 3)].len; | ||
573 : | |||
574 : | *Data->cbp = bits1 <= bits2 ? cbp[0] : cbp[1]; | ||
575 : | |||
576 : | return MIN(bits1, bits2); | ||
577 : | } | ||
578 : | |||
579 : | |||
580 : | static int | ||
581 : | findRD_gmc(SearchData * const Data, const IMAGE * const vGMC, const int x, const int y) | ||
582 : | { | ||
583 : | int bits = BITS_MULT*1; /* this one is mcsel */ | ||
584 : | unsigned int cbp = 0, i; | ||
585 : | int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64; | ||
586 : | |||
587 : | for(i = 0; i < 4; i++) { | ||
588 : | int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth); | ||
589 : | transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth); | ||
590 : | syskin | 1569 | bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i, |
591 : | Data->scan_table, Data->lambda[i], Data->mpeg_quant_matrices, Data->quant_sq); | ||
592 : | edgomez | 1382 | if (bits >= Data->iMinSAD[0]) return bits; |
593 : | } | ||
594 : | |||
595 : | bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len; | ||
596 : | |||
597 : | /*chroma U */ | ||
598 : | transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); | ||
599 : | syskin | 1569 | bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4, |
600 : | Data->scan_table, Data->lambda[4], Data->mpeg_quant_matrices, Data->quant_sq); | ||
601 : | edgomez | 1382 | |
602 : | if (bits >= Data->iMinSAD[0]) return bits; | ||
603 : | |||
604 : | /* chroma V */ | ||
605 : | transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2); | ||
606 : | syskin | 1569 | bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5, |
607 : | Data->scan_table, Data->lambda[5], Data->mpeg_quant_matrices, Data->quant_sq); | ||
608 : | edgomez | 1382 | |
609 : | bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len; | ||
610 : | |||
611 : | *Data->cbp = cbp; | ||
612 : | |||
613 : | return bits; | ||
614 : | } | ||
615 : | |||
616 : | void | ||
617 : | xvid_me_ModeDecision_RD(SearchData * const Data, | ||
618 : | MACROBLOCK * const pMB, | ||
619 : | const MACROBLOCK * const pMBs, | ||
620 : | const int x, const int y, | ||
621 : | const MBParam * const pParam, | ||
622 : | const uint32_t MotionFlags, | ||
623 : | const uint32_t VopFlags, | ||
624 : | const uint32_t VolFlags, | ||
625 : | const IMAGE * const pCurrent, | ||
626 : | const IMAGE * const pRef, | ||
627 : | const IMAGE * const vGMC, | ||
628 : | const int coding_type) | ||
629 : | { | ||
630 : | int mode = MODE_INTER; | ||
631 : | int mcsel = 0; | ||
632 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
633 : | const uint32_t iQuant = pMB->quant; | ||
634 : | |||
635 : | int min_rd, intra_rd, i, cbp; | ||
636 : | VECTOR backup[5], *v; | ||
637 : | Data->iQuant = iQuant; | ||
638 : | syskin | 1569 | Data->quant_sq = iQuant*iQuant; |
639 : | edgomez | 1382 | Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? |
640 : | scan_tables[2] : scan_tables[0]; | ||
641 : | |||
642 : | pMB->mcsel = 0; | ||
643 : | |||
644 : | v = Data->qpel ? Data->currentQMV : Data->currentMV; | ||
645 : | for (i = 0; i < 5; i++) { | ||
646 : | Data->iMinSAD[i] = 256*4096; | ||
647 : | backup[i] = v[i]; | ||
648 : | } | ||
649 : | |||
650 : | syskin | 1506 | for (i = 0; i < 6; i++) { |
651 : | /* HVS models, anyone ? */ | ||
652 : | Data->lambda[i] = LAMBDA; | ||
653 : | } | ||
654 : | |||
655 : | edgomez | 1382 | min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); |
656 : | cbp = *Data->cbp; | ||
657 : | |||
658 : | if (coding_type == S_VOP) { | ||
659 : | int gmc_rd; | ||
660 : | *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ | ||
661 : | gmc_rd = findRD_gmc(Data, vGMC, x, y); | ||
662 : | if (gmc_rd < min_rd) { | ||
663 : | mcsel = 1; | ||
664 : | *Data->iMinSAD = min_rd = gmc_rd; | ||
665 : | mode = MODE_INTER; | ||
666 : | cbp = *Data->cbp; | ||
667 : | } | ||
668 : | } | ||
669 : | |||
670 : | if (inter4v) { | ||
671 : | int v4_rd; | ||
672 : | v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); | ||
673 : | if (v4_rd < min_rd) { | ||
674 : | Data->iMinSAD[0] = min_rd = v4_rd; | ||
675 : | mode = MODE_INTER4V; | ||
676 : | cbp = *Data->cbp; | ||
677 : | } | ||
678 : | } | ||
679 : | |||
680 : | intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); | ||
681 : | if (intra_rd < min_rd) { | ||
682 : | *Data->iMinSAD = min_rd = intra_rd; | ||
683 : | mode = MODE_INTRA; | ||
684 : | cbp = *Data->cbp; | ||
685 : | } | ||
686 : | |||
687 : | pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
688 : | pMB->cbp = cbp; | ||
689 : | |||
690 : | if (mode == MODE_INTER && mcsel == 0) { | ||
691 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; | ||
692 : | |||
693 : | if(Data->qpel) { | ||
694 : | pMB->qmvs[0] = pMB->qmvs[1] | ||
695 : | = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; | ||
696 : | pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; | ||
697 : | pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; | ||
698 : | } else { | ||
699 : | pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; | ||
700 : | pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; | ||
701 : | } | ||
702 : | |||
703 : | } else if (mode == MODE_INTER ) { /* but mcsel == 1 */ | ||
704 : | |||
705 : | pMB->mcsel = 1; | ||
706 : | if (Data->qpel) { | ||
707 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
708 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; | ||
709 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; | ||
710 : | } else | ||
711 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
712 : | |||
713 : | } else | ||
714 : | if (mode == MODE_INTER4V) ; /* anything here? */ | ||
715 : | else /* INTRA, NOT_CODED */ | ||
716 : | ZeroMacroblockP(pMB, 0); | ||
717 : | |||
718 : | pMB->mode = mode; | ||
719 : | } | ||
720 : | |||
721 : | void | ||
722 : | xvid_me_ModeDecision_Fast(SearchData * const Data, | ||
723 : | MACROBLOCK * const pMB, | ||
724 : | const MACROBLOCK * const pMBs, | ||
725 : | const int x, const int y, | ||
726 : | const MBParam * const pParam, | ||
727 : | const uint32_t MotionFlags, | ||
728 : | const uint32_t VopFlags, | ||
729 : | const uint32_t VolFlags, | ||
730 : | const IMAGE * const pCurrent, | ||
731 : | const IMAGE * const pRef, | ||
732 : | const IMAGE * const vGMC, | ||
733 : | const int coding_type) | ||
734 : | { | ||
735 : | int mode = MODE_INTER; | ||
736 : | int mcsel = 0; | ||
737 : | int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0); | ||
738 : | const uint32_t iQuant = pMB->quant; | ||
739 : | const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0); | ||
740 : | int sad; | ||
741 : | int min_rd = -1, intra_rd, i, cbp = 63; | ||
742 : | VECTOR backup[5], *v; | ||
743 : | int sad_backup[5]; | ||
744 : | int InterBias = MV16_INTER_BIAS; | ||
745 : | int thresh = 0; | ||
746 : | int top = 0, top_right = 0, left = 0; | ||
747 : | Data->scan_table = VopFlags & XVID_VOP_ALTERNATESCAN ? | ||
748 : | scan_tables[2] : scan_tables[0]; | ||
749 : | |||
750 : | pMB->mcsel = 0; | ||
751 : | syskin | 1569 | Data->iQuant = iQuant; |
752 : | Data->quant_sq = iQuant*iQuant; | ||
753 : | edgomez | 1382 | |
754 : | syskin | 1509 | for (i = 0; i < 6; i++) { |
755 : | /* HVS models, anyone ? */ | ||
756 : | Data->lambda[i] = LAMBDA; | ||
757 : | } | ||
758 : | |||
759 : | edgomez | 1382 | /* INTER <-> INTER4V decision */ |
760 : | if ((Data->iMinSAD[0] + 75 < Data->iMinSAD[1] + | ||
761 : | Data->iMinSAD[2] + Data->iMinSAD[3] + Data->iMinSAD[4])) { /* normal, fast, SAD-based mode decision */ | ||
762 : | if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
763 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) { | ||
764 : | mode = MODE_INTER; | ||
765 : | sad = Data->iMinSAD[0]; | ||
766 : | } else { | ||
767 : | mode = MODE_INTER4V; | ||
768 : | sad = Data->iMinSAD[1] + Data->iMinSAD[2] + | ||
769 : | Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant; | ||
770 : | Data->iMinSAD[0] = sad; | ||
771 : | } | ||
772 : | |||
773 : | /* final skip decision, a.k.a. "the vector you found, really that good?" */ | ||
774 : | if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) | ||
775 : | if ( (100*sad)/(pMB->sad16+1) > FINAL_SKIP_THRESH) | ||
776 : | syskin | 1564 | if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
777 : | edgomez | 1382 | mode = MODE_NOT_CODED; |
778 : | sad = 0; /* Compiler warning */ | ||
779 : | goto early_out; | ||
780 : | } | ||
781 : | |||
782 : | /* mcsel */ | ||
783 : | if (coding_type == S_VOP) { | ||
784 : | |||
785 : | int32_t iSAD = sad16(Data->Cur, | ||
786 : | vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); | ||
787 : | |||
788 : | if (Data->chroma) { | ||
789 : | iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
790 : | iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
791 : | } | ||
792 : | |||
793 : | if (iSAD <= sad) { /* mode decision GMC */ | ||
794 : | mode = MODE_INTER; | ||
795 : | mcsel = 1; | ||
796 : | sad = iSAD; | ||
797 : | } | ||
798 : | |||
799 : | } | ||
800 : | } else { /* Rate-Distortion INTER<->INTER4V */ | ||
801 : | Data->iQuant = iQuant; | ||
802 : | v = Data->qpel ? Data->currentQMV : Data->currentMV; | ||
803 : | |||
804 : | /* final skip decision, a.k.a. "the vector you found, really that good?" */ | ||
805 : | if (skip_possible && (pMB->sad16 < (int)iQuant * MAX_SAD00_FOR_SKIP)) | ||
806 : | if ( (100*Data->iMinSAD[0])/(pMB->sad16+1) > FINAL_SKIP_THRESH) | ||
807 : | syskin | 1564 | if (Data->chroma || xvid_me_SkipDecisionP(pCurrent, pRef, x, y, Data->iEdgedWidth/2, iQuant)) { |
808 : | edgomez | 1382 | mode = MODE_NOT_CODED; |
809 : | sad = 0; /* Compiler warning */ | ||
810 : | goto early_out; | ||
811 : | } | ||
812 : | |||
813 : | for (i = 0; i < 5; i++) { | ||
814 : | sad_backup[i] = Data->iMinSAD[i]; | ||
815 : | Data->iMinSAD[i] = 256*4096; | ||
816 : | backup[i] = v[i]; | ||
817 : | } | ||
818 : | |||
819 : | min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); | ||
820 : | cbp = *Data->cbp; | ||
821 : | sad = sad_backup[0]; | ||
822 : | |||
823 : | if (coding_type == S_VOP) { | ||
824 : | int gmc_rd; | ||
825 : | |||
826 : | *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ | ||
827 : | gmc_rd = findRD_gmc(Data, vGMC, x, y); | ||
828 : | if (gmc_rd < min_rd) { | ||
829 : | mcsel = 1; | ||
830 : | *Data->iMinSAD = min_rd = gmc_rd; | ||
831 : | mode = MODE_INTER; | ||
832 : | cbp = *Data->cbp; | ||
833 : | sad = sad16(Data->Cur, | ||
834 : | vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536); | ||
835 : | if (Data->chroma) { | ||
836 : | sad += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
837 : | sad += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2); | ||
838 : | } | ||
839 : | } | ||
840 : | } | ||
841 : | |||
842 : | if (inter4v) { | ||
843 : | int v4_rd; | ||
844 : | v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); | ||
845 : | if (v4_rd < min_rd) { | ||
846 : | Data->iMinSAD[0] = min_rd = v4_rd; | ||
847 : | mode = MODE_INTER4V; | ||
848 : | cbp = *Data->cbp; | ||
849 : | sad = sad_backup[1] + sad_backup[2] + | ||
850 : | sad_backup[3] + sad_backup[4] + IMV16X16 * (int32_t)iQuant; | ||
851 : | } | ||
852 : | } | ||
853 : | } | ||
854 : | |||
855 : | left = top = top_right = -1; | ||
856 : | thresh = 0; | ||
857 : | |||
858 : | if((x > 0) && (y > 0) && (x < (int32_t) pParam->mb_width)) { | ||
859 : | left = (&pMBs[(x-1) + y * pParam->mb_width])->sad16; /* left */ | ||
860 : | top = (&pMBs[x + (y-1) * pParam->mb_width])->sad16; /* top */ | ||
861 : | top_right = (&pMBs[(x+1) + (y-1) * pParam->mb_width])->sad16; /* top right */ | ||
862 : | |||
863 : | if(((&pMBs[(x-1) + y * pParam->mb_width])->mode != MODE_INTRA) && | ||
864 : | ((&pMBs[x + (y-1) * pParam->mb_width])->mode != MODE_INTRA) && | ||
865 : | ((&pMBs[(x+1) + (y-1) * pParam->mb_width])->mode != MODE_INTRA)) { | ||
866 : | thresh = MAX(MAX(top, left), top_right); | ||
867 : | } | ||
868 : | else | ||
869 : | thresh = MIN(MIN(top, left), top_right); | ||
870 : | } | ||
871 : | |||
872 : | /* INTRA <-> INTER decision */ | ||
873 : | if (sad < thresh) { /* normal, fast, SAD-based mode decision */ | ||
874 : | /* intra decision */ | ||
875 : | |||
876 : | if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */ | ||
877 : | if (y != 0) | ||
878 : | if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80; | ||
879 : | if (x != 0) | ||
880 : | if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80; | ||
881 : | |||
882 : | if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */ | ||
883 : | |||
884 : | if (InterBias < sad) { | ||
885 : | syskin | 1564 | int32_t deviation = dev16(Data->Cur, Data->iEdgedWidth); |
886 : | edgomez | 1382 | if (deviation < (sad - InterBias)) mode = MODE_INTRA; |
887 : | } | ||
888 : | |||
889 : | pMB->cbp = 63; | ||
890 : | } else { /* Rate-Distortion INTRA<->INTER */ | ||
891 : | if(min_rd < 0) { | ||
892 : | Data->iQuant = iQuant; | ||
893 : | v = Data->qpel ? Data->currentQMV : Data->currentMV; | ||
894 : | |||
895 : | for (i = 0; i < 5; i++) { | ||
896 : | Data->iMinSAD[i] = 256*4096; | ||
897 : | backup[i] = v[i]; | ||
898 : | } | ||
899 : | |||
900 : | if(mode == MODE_INTER) { | ||
901 : | min_rd = findRD_inter(Data, x, y, pParam, MotionFlags); | ||
902 : | cbp = *Data->cbp; | ||
903 : | |||
904 : | if (coding_type == S_VOP) { | ||
905 : | int gmc_rd; | ||
906 : | |||
907 : | *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */ | ||
908 : | gmc_rd = findRD_gmc(Data, vGMC, x, y); | ||
909 : | if (gmc_rd < min_rd) { | ||
910 : | mcsel = 1; | ||
911 : | *Data->iMinSAD = min_rd = gmc_rd; | ||
912 : | mode = MODE_INTER; | ||
913 : | cbp = *Data->cbp; | ||
914 : | } | ||
915 : | } | ||
916 : | } | ||
917 : | |||
918 : | if(mode == MODE_INTER4V) { | ||
919 : | int v4_rd; | ||
920 : | v4_rd = findRD_inter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup); | ||
921 : | if (v4_rd < min_rd) { | ||
922 : | Data->iMinSAD[0] = min_rd = v4_rd; | ||
923 : | mode = MODE_INTER4V; | ||
924 : | cbp = *Data->cbp; | ||
925 : | } | ||
926 : | } | ||
927 : | } | ||
928 : | |||
929 : | intra_rd = findRD_intra(Data, pMB, x, y, pParam->mb_width); | ||
930 : | if (intra_rd < min_rd) { | ||
931 : | *Data->iMinSAD = min_rd = intra_rd; | ||
932 : | mode = MODE_INTRA; | ||
933 : | } | ||
934 : | |||
935 : | pMB->cbp = cbp; | ||
936 : | } | ||
937 : | |||
938 : | early_out: | ||
939 : | pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad; | ||
940 : | |||
941 : | if (mode == MODE_INTER && mcsel == 0) { | ||
942 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0]; | ||
943 : | |||
944 : | if(Data->qpel) { | ||
945 : | pMB->qmvs[0] = pMB->qmvs[1] | ||
946 : | = pMB->qmvs[2] = pMB->qmvs[3] = Data->currentQMV[0]; | ||
947 : | pMB->pmvs[0].x = Data->currentQMV[0].x - Data->predMV.x; | ||
948 : | pMB->pmvs[0].y = Data->currentQMV[0].y - Data->predMV.y; | ||
949 : | } else { | ||
950 : | pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x; | ||
951 : | pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y; | ||
952 : | } | ||
953 : | |||
954 : | } else if (mode == MODE_INTER ) { /* but mcsel == 1 */ | ||
955 : | |||
956 : | pMB->mcsel = 1; | ||
957 : | if (Data->qpel) { | ||
958 : | pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv; | ||
959 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2; | ||
960 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2; | ||
961 : | } else | ||
962 : | pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv; | ||
963 : | |||
964 : | } else | ||
965 : | if (mode == MODE_INTER4V) ; /* anything here? */ | ||
966 : | else /* INTRA, NOT_CODED */ | ||
967 : | ZeroMacroblockP(pMB, 0); | ||
968 : | |||
969 : | pMB->mode = mode; | ||
970 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |