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