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