1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Rate-Distortion Based Motion Estimation for B- VOPs - |
5 |
|
* |
6 |
|
* Copyright(C) 2004 Radoslaw Czyz <xvid@syskin.cjb.net> |
7 |
|
* Copyright(C) 2010 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 |
|
* $Id: estimation_rd_based_bvop.c,v 1.12 2010-12-24 13:21:35 Isibaar Exp $ |
24 |
|
* |
25 |
|
****************************************************************************/ |
26 |
|
|
27 |
#include <assert.h> |
#include <assert.h> |
28 |
#include <stdio.h> |
#include <stdio.h> |
46 |
/* rd = BITS_MULT*bits + LAMBDA*distortion */ |
/* rd = BITS_MULT*bits + LAMBDA*distortion */ |
47 |
#define LAMBDA ( (int)(BITS_MULT*1.0) ) |
#define LAMBDA ( (int)(BITS_MULT*1.0) ) |
48 |
|
|
|
|
|
49 |
static __inline unsigned int |
static __inline unsigned int |
50 |
Block_CalcBits_BVOP(int16_t * const coeff, |
Block_CalcBits_BVOP(int16_t * const coeff, |
51 |
int16_t * const data, |
int16_t * const data, |
55 |
const int block, |
const int block, |
56 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
57 |
const unsigned int lambda, |
const unsigned int lambda, |
58 |
const uint16_t * mpeg_quant_matrices) |
const uint16_t * mpeg_quant_matrices, |
59 |
|
const unsigned int quant_sq, |
60 |
|
int * const cbpcost, |
61 |
|
const unsigned int rel_var8, |
62 |
|
const unsigned int metric) |
63 |
{ |
{ |
64 |
int sum; |
int sum; |
65 |
int bits; |
int bits; |
66 |
int distortion = 0; |
int distortion = 0; |
67 |
|
|
68 |
fdct(data); |
fdct((short * const)data); |
69 |
|
|
70 |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
71 |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
73 |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) { |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] != 0)) { |
74 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
75 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
76 |
|
bits += *cbpcost; |
77 |
|
*cbpcost = 0; /* don't add cbp cost second time */ |
78 |
|
|
79 |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
80 |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
81 |
|
|
82 |
distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
if (metric) distortion = masked_sseh8_16bit(data, dqcoeff, rel_var8); |
83 |
|
else distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
84 |
|
|
85 |
} else { |
} else { |
86 |
const static int16_t zero_block[64] = |
const static int16_t zero_block[64] = |
87 |
{ |
{ |
95 |
0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, |
96 |
}; |
}; |
97 |
bits = 0; |
bits = 0; |
98 |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
|
99 |
|
if (metric) distortion = masked_sseh8_16bit(data, (int16_t * const) zero_block, rel_var8); |
100 |
|
else distortion = sse8_16bit(data, (int16_t * const) zero_block, 8*sizeof(int16_t)); |
101 |
|
|
102 |
} |
} |
103 |
|
|
104 |
return bits + (lambda*distortion)/(quant*quant); |
return bits + (lambda*distortion)/quant_sq; |
105 |
} |
} |
106 |
|
|
107 |
|
|
114 |
const int block, |
const int block, |
115 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
116 |
const unsigned int lambda, |
const unsigned int lambda, |
117 |
const uint16_t * mpeg_quant_matrices) |
const uint16_t * mpeg_quant_matrices, |
118 |
|
const unsigned int quant_sq, |
119 |
|
int * const cbpcost, |
120 |
|
const unsigned int rel_var8, |
121 |
|
const unsigned int metric) |
122 |
{ |
{ |
123 |
int sum; |
int sum; |
124 |
int bits; |
int bits; |
125 |
int distortion = 0; |
int distortion = 0; |
126 |
|
|
127 |
fdct(data); |
fdct((short * const)data); |
128 |
|
|
129 |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
if (quant_type) sum = quant_h263_inter(coeff, data, quant, mpeg_quant_matrices); |
130 |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
else sum = quant_mpeg_inter(coeff, data, quant, mpeg_quant_matrices); |
132 |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) { |
if ((sum >= 3) || (coeff[1] != 0) || (coeff[8] != 0) || (coeff[0] > 0) || (coeff[0] < -1)) { |
133 |
*cbp |= 1 << (5 - block); |
*cbp |= 1 << (5 - block); |
134 |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
bits = BITS_MULT * CodeCoeffInter_CalcBits(coeff, scan_table); |
135 |
|
bits += *cbpcost; |
136 |
|
*cbpcost = 0; |
137 |
|
|
138 |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
if (quant_type) dequant_h263_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
139 |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
else dequant_mpeg_inter(dqcoeff, coeff, quant, mpeg_quant_matrices); |
140 |
|
|
141 |
distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
if (metric) distortion = masked_sseh8_16bit(data, dqcoeff, rel_var8); |
142 |
|
else distortion = sse8_16bit(data, dqcoeff, 8*sizeof(int16_t)); |
143 |
|
|
144 |
} else { |
} else { |
145 |
const static int16_t zero_block[64] = |
const static int16_t zero_block[64] = |
146 |
{ |
{ |
154 |
0, 0, 0, 0, 0, 0, 0, 0, |
0, 0, 0, 0, 0, 0, 0, 0, |
155 |
}; |
}; |
156 |
bits = 0; |
bits = 0; |
|
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
|
|
} |
|
|
|
|
|
return bits + (lambda*distortion)/(quant*quant); |
|
|
} |
|
157 |
|
|
158 |
|
if (metric) distortion = masked_sseh8_16bit(data, (int16_t * const) zero_block, rel_var8); |
159 |
|
else distortion = sse8_16bit(data, (int16_t * const) zero_block, 8*sizeof(int16_t)); |
160 |
|
|
|
static void |
|
|
transfer_8to16sub2ro(int16_t * const dct, |
|
|
const uint8_t * const cur, |
|
|
const uint8_t * ref1, |
|
|
const uint8_t * ref2, |
|
|
const uint32_t stride) |
|
|
{ |
|
|
uint32_t i, j; |
|
|
|
|
|
for (j = 0; j < 8; j++) { |
|
|
for (i = 0; i < 8; i++) { |
|
|
uint8_t c = cur[j * stride + i]; |
|
|
int r = (ref1[j * stride + i] + ref2[j * stride + i] + 1) / 2; |
|
|
dct[j * 8 + i] = (int16_t) c - (int16_t) r; |
|
|
} |
|
161 |
} |
} |
162 |
|
|
163 |
|
return bits + (lambda*distortion)/quant_sq; |
164 |
} |
} |
165 |
|
|
166 |
static void |
static void |
168 |
{ |
{ |
169 |
|
|
170 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
171 |
int32_t rd = 3*BITS_MULT; /* note to self: 3 bits minimum, but maybe 4 if it's forward mode */ |
int32_t rd = (3+2)*BITS_MULT; /* 3 bits for mode + 2 for vector (minimum) */ |
172 |
VECTOR * current; |
VECTOR * current; |
173 |
const uint8_t * ptr; |
const uint8_t * ptr; |
174 |
int i, xc, yc; |
int i, xc, yc; |
175 |
unsigned cbp = 0; |
unsigned cbp = 0; |
176 |
|
int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
177 |
|
|
178 |
if ( (x > data->max_dx) || (x < data->min_dx) |
if ( (x > data->max_dx) || (x < data->min_dx) |
179 |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
|| (y > data->max_dy) || (y < data->min_dy) ) return; |
188 |
xc = x/2; yc = y/2; |
xc = x/2; yc = y/2; |
189 |
} |
} |
190 |
|
|
191 |
rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0); |
rd += BITS_MULT*(d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision)-2); |
192 |
|
|
193 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
194 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
195 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
196 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
197 |
|
&cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, |
198 |
|
data->quant_sq, &cbpcost, data->rel_var8[i], data->metric); |
199 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
200 |
} |
} |
201 |
|
|
206 |
/* chroma U */ |
/* chroma U */ |
207 |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
208 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
209 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
210 |
|
&cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, |
211 |
|
data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); |
212 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
213 |
|
|
214 |
/* chroma V */ |
/* chroma V */ |
215 |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding); |
216 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
217 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
218 |
|
&cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, |
219 |
if (cbp) rd += BITS_MULT * 7; |
data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); |
220 |
|
|
221 |
if (rd < data->iMinSAD[0]) { |
if (rd < data->iMinSAD[0]) { |
222 |
data->iMinSAD[0] = rd; |
data->iMinSAD[0] = rd; |
226 |
} |
} |
227 |
} |
} |
228 |
|
|
|
|
|
229 |
static void |
static void |
230 |
CheckCandidateRDDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
CheckCandidateRDDirect(const int x, const int y, SearchData * const data, const unsigned int Direction) |
231 |
{ |
{ |
235 |
unsigned int cbp = 0; |
unsigned int cbp = 0; |
236 |
unsigned int k; |
unsigned int k; |
237 |
VECTOR mvs, b_mvs; |
VECTOR mvs, b_mvs; |
238 |
|
int cbpcost = 6*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
239 |
|
|
240 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
241 |
|
|
273 |
} |
} |
274 |
|
|
275 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
276 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
277 |
|
&cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices, |
278 |
|
data->quant_sq, &cbpcost, data->rel_var8[k], data->metric); |
279 |
if (rd > *(data->iMinSAD)) return; |
if (rd > *(data->iMinSAD)) return; |
280 |
} |
} |
281 |
|
|
289 |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
290 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
291 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
292 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
293 |
|
&cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, |
294 |
|
data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); |
295 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
296 |
|
|
297 |
/* chroma V */ |
/* chroma V */ |
298 |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
299 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
300 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
301 |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP_direct(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, |
302 |
|
&cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, |
303 |
|
data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); |
304 |
|
|
|
if (cbp) |
|
|
rd += BITS_MULT * 6; |
|
305 |
if (cbp || x != 0 || y != 0) |
if (cbp || x != 0 || y != 0) |
306 |
rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0, 0); |
rd += BITS_MULT * d_mv_bits(x, y, zeroMV, 1, 0); |
307 |
|
|
308 |
if (rd < *(data->iMinSAD)) { |
if (rd < *(data->iMinSAD)) { |
309 |
*data->iMinSAD = rd; |
*data->iMinSAD = rd; |
313 |
} |
} |
314 |
} |
} |
315 |
|
|
|
|
|
|
|
|
|
|
|
316 |
static void |
static void |
317 |
CheckCandidateRDInt(const int x, const int y, SearchData * const data, const unsigned int Direction) |
CheckCandidateRDInt(const int x, const int y, SearchData * const data, const unsigned int Direction) |
318 |
{ |
{ |
321 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
322 |
unsigned int cbp = 0; |
unsigned int cbp = 0; |
323 |
unsigned int i; |
unsigned int i; |
324 |
|
int cbpcost = 7*BITS_MULT; /* how much to add if cbp turns out to be non-zero */ |
325 |
|
|
326 |
const uint8_t *ReferenceF, *ReferenceB; |
const uint8_t *ReferenceF, *ReferenceB; |
327 |
VECTOR *current; |
VECTOR *current; |
356 |
xcb = xb/2; ycb = yb/2; |
xcb = xb/2; ycb = yb/2; |
357 |
} |
} |
358 |
|
|
359 |
rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0) |
rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision) |
360 |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision, 0)); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision)); |
|
|
|
361 |
|
|
362 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
363 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
364 |
if (rd >= *data->iMinSAD) return; |
if (rd >= *data->iMinSAD) return; |
365 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
366 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
367 |
|
i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, |
368 |
|
data->quant_sq, &cbpcost, data->rel_var8[i], data->metric); |
369 |
} |
} |
370 |
|
|
371 |
/* chroma */ |
/* chroma */ |
378 |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
379 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[4], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
380 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
381 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
382 |
|
4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, |
383 |
|
data->quant_sq, &cbpcost, data->rel_var8[4], data->metric); |
384 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
385 |
|
|
386 |
|
|
388 |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
ReferenceF = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xcf, ycf, data->iEdgedWidth/2, data->rounding); |
389 |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
ReferenceB = interpolate8x8_switch2(data->RefQ + 16, data->b_RefP[5], 0, 0, xcb, ycb, data->iEdgedWidth/2, data->rounding); |
390 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
391 |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices); |
rd += Block_CalcBits_BVOP(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, |
392 |
|
5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, |
393 |
if (cbp) rd += BITS_MULT * 7; |
data->quant_sq, &cbpcost, data->rel_var8[5], data->metric); |
394 |
|
|
395 |
if (rd < *(data->iMinSAD)) { |
if (rd < *(data->iMinSAD)) { |
396 |
*data->iMinSAD = rd; |
*data->iMinSAD = rd; |
412 |
Data->iMinSAD[0] = *best_sad; |
Data->iMinSAD[0] = *best_sad; |
413 |
|
|
414 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
415 |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0); |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel); |
416 |
|
|
417 |
Data->qpel_precision = Data->qpel; |
Data->qpel_precision = Data->qpel; |
418 |
|
|
455 |
Data->iMinSAD[0] = *best_sad; |
Data->iMinSAD[0] = *best_sad; |
456 |
|
|
457 |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, |
458 |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel, 0); |
x, y, 4, pParam->width, pParam->height, Data->iFcode, 1 + Data->qpel); |
459 |
|
|
460 |
Data->qpel_precision = Data->qpel; |
Data->qpel_precision = Data->qpel; |
461 |
|
|
496 |
VECTOR * f_predMV, |
VECTOR * f_predMV, |
497 |
VECTOR * b_predMV, |
VECTOR * b_predMV, |
498 |
const uint32_t MotionFlags, |
const uint32_t MotionFlags, |
499 |
|
const uint32_t VopFlags, |
500 |
const MBParam * const pParam, |
const MBParam * const pParam, |
501 |
int x, int y) |
int x, int y, |
502 |
|
int best_sad, |
503 |
|
int force_direct) |
504 |
{ |
{ |
505 |
int mode = MODE_DIRECT, k; |
int mode = MODE_DIRECT, k; |
506 |
int f_rd, b_rd, i_rd, d_rd, best_rd; |
int f_rd, b_rd, i_rd, d_rd, best_rd; |
508 |
const uint32_t iQuant = Data_d->iQuant; |
const uint32_t iQuant = Data_d->iQuant; |
509 |
int i; |
int i; |
510 |
int ref_quant = b_mb->quant; |
int ref_quant = b_mb->quant; |
511 |
|
int no_of_checks = 0; |
512 |
|
|
513 |
int order[4] = {MODE_DIRECT, MODE_FORWARD, MODE_BACKWARD, MODE_INTERPOLATE}; |
int order[4] = {MODE_DIRECT, MODE_FORWARD, MODE_BACKWARD, MODE_INTERPOLATE}; |
514 |
|
|
515 |
|
Data_d->metric = Data_b->metric = Data_f->metric = Data_i->metric = !!(VopFlags & XVID_VOP_RD_PSNRHVSM); |
516 |
|
|
517 |
Data_d->scan_table = Data_b->scan_table = Data_f->scan_table = Data_i->scan_table |
Data_d->scan_table = Data_b->scan_table = Data_f->scan_table = Data_i->scan_table |
518 |
= /*VopFlags & XVID_VOP_ALTERNATESCAN ? scan_tables[2] : */scan_tables[0]; |
= /*VopFlags & XVID_VOP_ALTERNATESCAN ? scan_tables[2] : */scan_tables[0]; |
519 |
|
*Data_f->cbp = *Data_b->cbp = *Data_i->cbp = *Data_d->cbp = 63; |
520 |
|
|
521 |
f_rd = b_rd = i_rd = d_rd = best_rd = 256*4096; |
f_rd = b_rd = i_rd = d_rd = best_rd = 256*4096; |
522 |
|
|
523 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
524 |
int lam = (LAMBDA*iQuant*iQuant)/(ref_quant*(ref_quant+1)); /* re-calculate as if it was p-frame's quant +.5 */ |
/* re-calculate as if it was p-frame's quant +.5 */ |
525 |
|
int lam = (pMB->lambda[i]*LAMBDA*iQuant*iQuant)/(ref_quant*(ref_quant+1)); |
526 |
|
lam >>= LAMBDA_EXP; |
527 |
Data_d->lambda[i] = lam; |
Data_d->lambda[i] = lam; |
528 |
Data_b->lambda[i] = lam; |
Data_b->lambda[i] = lam; |
529 |
Data_f->lambda[i] = lam; |
Data_f->lambda[i] = lam; |
530 |
Data_i->lambda[i] = lam; |
Data_i->lambda[i] = lam; |
531 |
|
|
532 |
|
Data_d->rel_var8[i] = pMB->rel_var8[i]; |
533 |
|
Data_b->rel_var8[i] = pMB->rel_var8[i]; |
534 |
|
Data_f->rel_var8[i] = pMB->rel_var8[i]; |
535 |
|
Data_i->rel_var8[i] = pMB->rel_var8[i]; |
536 |
|
} |
537 |
|
|
538 |
|
if (force_direct) { |
539 |
|
best_rd = 0; |
540 |
|
goto set_mode; /* bypass checks for non-direct modes */ |
541 |
} |
} |
542 |
|
|
543 |
/* find the best order of evaluation - smallest SAD comes first, because *if* it means smaller RD, |
/* find the best order of evaluation - smallest SAD comes first, because *if* it means smaller RD, |
556 |
} |
} |
557 |
} |
} |
558 |
|
|
559 |
|
for(i = 0; i < 4; i++) |
560 |
|
if (get_sad_for_mode(order[i], Data_d, Data_b, Data_f, Data_i) < 2*best_sad) |
561 |
|
no_of_checks++; |
562 |
|
|
563 |
|
if (no_of_checks > 1) { |
564 |
/* evaluate cost of all modes */ |
/* evaluate cost of all modes */ |
565 |
for (i = 0; i < 4; i++) { |
for (i = 0; i < no_of_checks; i++) { |
566 |
int rd; |
int rd; |
567 |
|
if (2*best_sad < get_sad_for_mode(order[i], Data_d, Data_b, Data_f, Data_i)) |
568 |
|
break; /* further SADs are too big */ |
569 |
|
|
570 |
switch (order[i]) { |
switch (order[i]) { |
571 |
case MODE_DIRECT: |
case MODE_DIRECT: |
587 |
best_rd = rd; |
best_rd = rd; |
588 |
} |
} |
589 |
} |
} |
590 |
|
} else { |
591 |
|
/* only 1 mode is below the threshold */ |
592 |
|
mode = order[0]; |
593 |
|
best_rd = 0; |
594 |
|
} |
595 |
|
|
596 |
|
|
597 |
|
set_mode: |
598 |
pMB->sad16 = best_rd; |
pMB->sad16 = best_rd; |
599 |
pMB->mode = mode; |
pMB->mode = mode; |
600 |
|
|
646 |
} |
} |
647 |
pMB->mvs[0] = *Data_f->currentMV; |
pMB->mvs[0] = *Data_f->currentMV; |
648 |
pMB->cbp = *Data_f->cbp; |
pMB->cbp = *Data_f->cbp; |
649 |
|
pMB->b_mvs[0] = *Data_b->currentMV; /* hint for future searches */ |
650 |
break; |
break; |
651 |
|
|
652 |
case MODE_BACKWARD: |
case MODE_BACKWARD: |
662 |
} |
} |
663 |
pMB->b_mvs[0] = *Data_b->currentMV; |
pMB->b_mvs[0] = *Data_b->currentMV; |
664 |
pMB->cbp = *Data_b->cbp; |
pMB->cbp = *Data_b->cbp; |
665 |
|
pMB->mvs[0] = *Data_f->currentMV; /* hint for future searches */ |
666 |
break; |
break; |
667 |
|
|
668 |
|
|