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 |
|
* |
8 |
|
* This program is free software ; you can redistribute it and/or modify |
9 |
|
* it under the terms of the GNU General Public License as published by |
10 |
|
* the Free Software Foundation ; either version 2 of the License, or |
11 |
|
* (at your option) any later version. |
12 |
|
* |
13 |
|
* This program is distributed in the hope that it will be useful, |
14 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
15 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
* GNU General Public License for more details. |
17 |
|
* |
18 |
|
* You should have received a copy of the GNU General Public License |
19 |
|
* along with this program ; if not, write to the Free Software |
20 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
|
* |
22 |
|
* $Id$ |
23 |
|
* |
24 |
|
****************************************************************************/ |
25 |
|
|
26 |
#include <assert.h> |
#include <assert.h> |
27 |
#include <stdio.h> |
#include <stdio.h> |
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 |
{ |
{ |
61 |
int sum; |
int sum; |
62 |
int bits; |
int bits; |
91 |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
92 |
} |
} |
93 |
|
|
94 |
return bits + (lambda*distortion)/(quant*quant); |
return bits + (lambda*distortion)/quant_sq; |
95 |
} |
} |
96 |
|
|
97 |
|
|
104 |
const int block, |
const int block, |
105 |
const uint16_t * scan_table, |
const uint16_t * scan_table, |
106 |
const unsigned int lambda, |
const unsigned int lambda, |
107 |
const uint16_t * mpeg_quant_matrices) |
const uint16_t * mpeg_quant_matrices, |
108 |
|
const unsigned int quant_sq) |
109 |
{ |
{ |
110 |
int sum; |
int sum; |
111 |
int bits; |
int bits; |
140 |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
distortion = sse8_16bit(data, zero_block, 8*sizeof(int16_t)); |
141 |
} |
} |
142 |
|
|
143 |
return bits + (lambda*distortion)/(quant*quant); |
return bits + (lambda*distortion)/quant_sq; |
|
} |
|
|
|
|
|
|
|
|
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; |
|
|
} |
|
|
} |
|
144 |
} |
} |
145 |
|
|
146 |
static void |
static void |
148 |
{ |
{ |
149 |
|
|
150 |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64; |
151 |
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) */ |
152 |
VECTOR * current; |
VECTOR * current; |
153 |
const uint8_t * ptr; |
const uint8_t * ptr; |
154 |
int i, xc, yc; |
int i, xc, yc; |
167 |
xc = x/2; yc = y/2; |
xc = x/2; yc = y/2; |
168 |
} |
} |
169 |
|
|
170 |
rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision); |
rd += BITS_MULT*(d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision)-2); |
171 |
|
|
172 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
173 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
174 |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth); |
175 |
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, |
176 |
|
&cbp, i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, data->quant_sq); |
177 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
178 |
} |
} |
179 |
|
|
184 |
/* chroma U */ |
/* chroma U */ |
185 |
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); |
186 |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2); |
187 |
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, |
188 |
|
&cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); |
189 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
190 |
|
|
191 |
/* chroma V */ |
/* chroma V */ |
192 |
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); |
193 |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2); |
194 |
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, |
195 |
|
&cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); |
196 |
|
|
197 |
if (cbp) rd += BITS_MULT * 7; |
if (cbp) rd += BITS_MULT * 7; |
198 |
|
|
204 |
} |
} |
205 |
} |
} |
206 |
|
|
|
|
|
207 |
static void |
static void |
208 |
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) |
209 |
{ |
{ |
250 |
} |
} |
251 |
|
|
252 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF, ReferenceB, data->iEdgedWidth); |
253 |
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, |
254 |
|
&cbp, k, data->scan_table, data->lambda[k], data->mpeg_quant_matrices, data->quant_sq); |
255 |
if (rd > *(data->iMinSAD)) return; |
if (rd > *(data->iMinSAD)) return; |
256 |
} |
} |
257 |
|
|
265 |
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); |
266 |
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); |
267 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
268 |
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, |
269 |
|
&cbp, 4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); |
270 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
271 |
|
|
272 |
/* chroma V */ |
/* chroma V */ |
273 |
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); |
274 |
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); |
275 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
276 |
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, |
277 |
|
&cbp, 5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); |
278 |
|
|
279 |
if (cbp) |
if (cbp) |
280 |
rd += BITS_MULT * 6; |
rd += BITS_MULT * 6; |
289 |
} |
} |
290 |
} |
} |
291 |
|
|
|
|
|
|
|
|
|
|
|
292 |
static void |
static void |
293 |
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) |
294 |
{ |
{ |
334 |
rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision) |
rd += BITS_MULT * (d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision) |
335 |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision)); |
+ d_mv_bits(xb, yb, data->bpredMV, data->iFcode, data->qpel^data->qpel_precision)); |
336 |
|
|
|
|
|
337 |
for(i = 0; i < 4; i++) { |
for(i = 0; i < 4; i++) { |
338 |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
int s = 8*((i&1) + (i>>1)*data->iEdgedWidth); |
339 |
if (rd >= *data->iMinSAD) return; |
if (rd >= *data->iMinSAD) return; |
340 |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
transfer_8to16sub2ro(in, data->Cur + s, ReferenceF + s, ReferenceB + s, data->iEdgedWidth); |
341 |
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, |
342 |
|
i, data->scan_table, data->lambda[i], data->mpeg_quant_matrices, data->quant_sq); |
343 |
} |
} |
344 |
|
|
345 |
/* chroma */ |
/* chroma */ |
352 |
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); |
353 |
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); |
354 |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurU, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
355 |
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, |
356 |
|
4, data->scan_table, data->lambda[4], data->mpeg_quant_matrices, data->quant_sq); |
357 |
if (rd >= data->iMinSAD[0]) return; |
if (rd >= data->iMinSAD[0]) return; |
358 |
|
|
359 |
|
|
361 |
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); |
362 |
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); |
363 |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
transfer_8to16sub2ro(in, data->CurV, ReferenceF, ReferenceB, data->iEdgedWidth/2); |
364 |
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, |
365 |
|
5, data->scan_table, data->lambda[5], data->mpeg_quant_matrices, data->quant_sq); |
366 |
|
|
367 |
if (cbp) rd += BITS_MULT * 7; |
if (cbp) rd += BITS_MULT * 7; |
368 |
|
|