[cvs] / xvidcore / src / prediction / mbprediction.h Repository:
ViewVC logotype

Annotation of /xvidcore/src/prediction/mbprediction.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (view) (download)

1 : edgomez 1.5 /**************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - MB prediction header file -
5 :     *
6 :     * This program is an implementation of a part of one or more MPEG-4
7 :     * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 :     * to use this software module in hardware or software products are
9 :     * advised that its use may infringe existing patents or copyrights, and
10 :     * any such use would be at such party's own risk. The original
11 :     * developer of this software module and his/her company, and subsequent
12 :     * editors and their companies, will have no liability for use of this
13 :     * software or modifications or derivatives thereof.
14 :     *
15 :     * This program is free software; you can redistribute it and/or modify
16 :     * it under the terms of the GNU General Public License as published by
17 :     * the xvid_free Software Foundation; either version 2 of the License, or
18 :     * (at your option) any later version.
19 :     *
20 :     * This program is distributed in the hope that it will be useful,
21 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 :     * GNU General Public License for more details.
24 :     *
25 :     * You should have received a copy of the GNU General Public License
26 :     * along with this program; if not, write to the xvid_free Software
27 :     * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 :     *
29 : edgomez 1.14 * $Id: mbprediction.h,v 1.13 2002/07/10 19:17:49 chl Exp $
30 : edgomez 1.5 *
31 :     *************************************************************************/
32 :    
33 : Isibaar 1.1 #ifndef _MBPREDICTION_H_
34 :     #define _MBPREDICTION_H_
35 :    
36 :     #include "../portab.h"
37 :     #include "../decoder.h"
38 :     #include "../global.h"
39 :    
40 :     #define MIN(X, Y) ((X)<(Y)?(X):(Y))
41 :     #define MAX(X, Y) ((X)>(Y)?(X):(Y))
42 :    
43 : edgomez 1.5 /* very large value */
44 : Isibaar 1.1 #define MV_MAX_ERROR (4096 * 256)
45 :    
46 :     #define MVequal(A,B) ( ((A).x)==((B).x) && ((A).y)==((B).y) )
47 :    
48 : edgomez 1.14 /*****************************************************************************
49 :     * Prototypes
50 :     ****************************************************************************/
51 :    
52 : edgomez 1.9 void MBPrediction(FRAMEINFO * frame, /* <-- The parameter for ACDC and MV prediction */
53 :    
54 :     uint32_t x_pos, /* <-- The x position of the MB to be searched */
55 :    
56 :     uint32_t y_pos, /* <-- The y position of the MB to be searched */
57 :    
58 :     uint32_t x_dim, /* <-- Number of macroblocks in a row */
59 :    
60 :     int16_t * qcoeff); /* <-> The quantized DCT coefficients */
61 :    
62 :     void add_acdc(MACROBLOCK * pMB,
63 :     uint32_t block,
64 :     int16_t dct_codes[64],
65 :     uint32_t iDcScaler,
66 :     int16_t predictors[8]);
67 :    
68 :    
69 :     void predict_acdc(MACROBLOCK * pMBs,
70 :     uint32_t x,
71 :     uint32_t y,
72 :     uint32_t mb_width,
73 :     uint32_t block,
74 :     int16_t qcoeff[64],
75 :     uint32_t current_quant,
76 :     int32_t iDcScaler,
77 : suxen_drol 1.10 int16_t predictors[8],
78 : suxen_drol 1.11 const int bound);
79 : suxen_drol 1.10
80 : Isibaar 1.1
81 : edgomez 1.14 /*****************************************************************************
82 :     * Inlined functions
83 :     ****************************************************************************/
84 :    
85 :     /*
86 :     * MODE_INTER, vm18 page 48
87 :     * MODE_INTER4V vm18 page 51
88 :     *
89 :     * (x,y-1) (x+1,y-1)
90 :     * [ | ] [ | ]
91 :     * [ 2 | 3 ] [ 2 | ]
92 :     *
93 :     * (x-1,y) (x,y) (x+1,y)
94 :     * [ | 1 ] [ 0 | 1 ] [ 0 | ]
95 :     * [ | 3 ] [ 2 | 3 ] [ | ]
96 :     */
97 : suxen_drol 1.12
98 :     static __inline VECTOR
99 :     get_pmv2(const MACROBLOCK * const mbs,
100 :     const int mb_width,
101 :     const int bound,
102 :     const int x,
103 :     const int y,
104 :     const int block)
105 :     {
106 :     static const VECTOR zeroMV = { 0, 0 };
107 :    
108 :     int lx, ly, lz; /* left */
109 :     int tx, ty, tz; /* top */
110 :     int rx, ry, rz; /* top-right */
111 :     int lpos, tpos, rpos;
112 :     int num_cand, last_cand;
113 :    
114 :     VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */
115 :    
116 :     switch (block) {
117 :     case 0:
118 :     lx = x - 1; ly = y; lz = 1;
119 :     tx = x; ty = y - 1; tz = 2;
120 :     rx = x + 1; ry = y - 1; rz = 2;
121 :     break;
122 :     case 1:
123 :     lx = x; ly = y; lz = 0;
124 :     tx = x; ty = y - 1; tz = 3;
125 :     rx = x + 1; ry = y - 1; rz = 2;
126 :     break;
127 :     case 2:
128 :     lx = x - 1; ly = y; lz = 3;
129 :     tx = x; ty = y; tz = 0;
130 :     rx = x; ry = y; rz = 1;
131 :     break;
132 :     default:
133 :     lx = x; ly = y; lz = 2;
134 :     tx = x; ty = y; tz = 0;
135 :     rx = x; ry = y; rz = 1;
136 :     }
137 :    
138 :     lpos = lx + ly * mb_width;
139 :     rpos = rx + ry * mb_width;
140 :     tpos = tx + ty * mb_width;
141 :     num_cand = 0;
142 :    
143 :     if (lpos >= bound && lx >= 0) {
144 :     num_cand++;
145 :     last_cand = 1;
146 :     pmv[1] = mbs[lpos].mvs[lz];
147 :     } else {
148 :     pmv[1] = zeroMV;
149 :     }
150 :    
151 :     if (tpos >= bound) {
152 :     num_cand++;
153 :     last_cand = 2;
154 :     pmv[2] = mbs[tpos].mvs[tz];
155 :     } else {
156 :     pmv[2] = zeroMV;
157 :     }
158 :    
159 :     if (rpos >= bound && rx < mb_width) {
160 :     num_cand++;
161 :     last_cand = 3;
162 :     pmv[3] = mbs[rpos].mvs[rz];
163 :     } else {
164 :     pmv[3] = zeroMV;
165 :     }
166 :    
167 : chl 1.13 /* if only one valid candidate predictor, the invalid candiates are set to the canidate */
168 : suxen_drol 1.12 if (num_cand != 1) {
169 :     /* set median */
170 :    
171 :     pmv[0].x =
172 :     MIN(MAX(pmv[1].x, pmv[2].x),
173 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
174 :     pmv[0].y =
175 :     MIN(MAX(pmv[1].y, pmv[2].y),
176 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
177 :     return pmv[0];
178 :     }
179 :    
180 :     return pmv[last_cand]; /* no point calculating median mv */
181 :     }
182 :    
183 :    
184 :    
185 : edgomez 1.14 /*
186 :     * pmv are filled with:
187 :     * [0]: Median (or whatever is correct in a special case)
188 :     * [1]: left neighbour
189 :     * [2]: top neighbour
190 :     * [3]: topright neighbour
191 :     * psad are filled with:
192 :     * [0]: minimum of [1] to [3]
193 :     * [1]: left neighbour's SAD (NB:[1] to [3] are actually not needed)
194 :     * [2]: top neighbour's SAD
195 :     * [3]: topright neighbour's SAD
196 :     */
197 : chl 1.13
198 : suxen_drol 1.12 static __inline int
199 :     get_pmvdata2(const MACROBLOCK * const mbs,
200 :     const int mb_width,
201 :     const int bound,
202 :     const int x,
203 :     const int y,
204 :     const int block,
205 :     VECTOR * const pmv,
206 :     int32_t * const psad)
207 :     {
208 :     static const VECTOR zeroMV = { 0, 0 };
209 :    
210 :     int lx, ly, lz; /* left */
211 :     int tx, ty, tz; /* top */
212 :     int rx, ry, rz; /* top-right */
213 :     int lpos, tpos, rpos;
214 :     int num_cand, last_cand;
215 :    
216 :     switch (block) {
217 :     case 0:
218 :     lx = x - 1; ly = y; lz = 1;
219 :     tx = x; ty = y - 1; tz = 2;
220 :     rx = x + 1; ry = y - 1; rz = 2;
221 :     break;
222 :     case 1:
223 :     lx = x; ly = y; lz = 0;
224 :     tx = x; ty = y - 1; tz = 3;
225 :     rx = x + 1; ry = y - 1; rz = 2;
226 :     break;
227 :     case 2:
228 :     lx = x - 1; ly = y; lz = 3;
229 :     tx = x; ty = y; tz = 0;
230 :     rx = x; ry = y; rz = 1;
231 :     break;
232 :     default:
233 :     lx = x; ly = y; lz = 2;
234 :     tx = x; ty = y; tz = 0;
235 :     rx = x; ry = y; rz = 1;
236 :     }
237 :    
238 :     lpos = lx + ly * mb_width;
239 :     rpos = rx + ry * mb_width;
240 :     tpos = tx + ty * mb_width;
241 :     num_cand = 0;
242 :    
243 :     if (lpos >= bound && lx >= 0) {
244 :     num_cand++;
245 :     last_cand = 1;
246 :     pmv[1] = mbs[lpos].mvs[lz];
247 :     psad[1] = mbs[lpos].sad8[lz];
248 :     } else {
249 :     pmv[1] = zeroMV;
250 :     psad[1] = MV_MAX_ERROR;
251 :     }
252 :    
253 :     if (tpos >= bound) {
254 :     num_cand++;
255 :     last_cand = 2;
256 :     pmv[2]= mbs[tpos].mvs[tz];
257 :     psad[2] = mbs[tpos].sad8[tz];
258 :     } else {
259 :     pmv[2] = zeroMV;
260 :     psad[2] = MV_MAX_ERROR;
261 :     }
262 :    
263 :     if (rpos >= bound && rx < mb_width) {
264 :     num_cand++;
265 :     last_cand = 3;
266 :     pmv[3] = mbs[rpos].mvs[rz];
267 :     psad[3] = mbs[rpos].sad8[rz];
268 : chl 1.13 } else {
269 :     pmv[3] = zeroMV;
270 :     psad[3] = MV_MAX_ERROR;
271 :     }
272 :    
273 :     /* original pmvdata() compatibility hack */
274 :     if (x == 0 && y == 0 && block == 0)
275 :     {
276 :     pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
277 :     psad[0] = 0;
278 :     psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
279 :     return 0;
280 :     }
281 :    
282 :     /* if only one valid candidate preictor, the invalid candiates are set to the canidate */
283 :     if (num_cand == 1) {
284 :     pmv[0] = pmv[last_cand];
285 :     psad[0] = psad[last_cand];
286 :     // return MVequal(pmv[0], zeroMV); /* no point calculating median mv and minimum sad */
287 :    
288 :     /* original pmvdata() compatibility hack */
289 :     return y==0 && block <= 1 ? 0 : MVequal(pmv[0], zeroMV);
290 :     }
291 :    
292 :     if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
293 :     pmv[0] = pmv[1];
294 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
295 :     return 1;
296 :     /* compatibility patch */
297 :     //return y==0 && block <= 1 ? 0 : 1;
298 :     }
299 :    
300 :     /* set median, minimum */
301 :    
302 :     pmv[0].x =
303 :     MIN(MAX(pmv[1].x, pmv[2].x),
304 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
305 :     pmv[0].y =
306 :     MIN(MAX(pmv[1].y, pmv[2].y),
307 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
308 :    
309 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
310 :    
311 :     return 0;
312 :     }
313 :    
314 :     /* copies of get_pmv and get_pmvdata for prediction from integer search */
315 :    
316 :     static __inline VECTOR
317 :     get_ipmv(const MACROBLOCK * const mbs,
318 :     const int mb_width,
319 :     const int bound,
320 :     const int x,
321 :     const int y,
322 :     const int block)
323 :     {
324 :     static const VECTOR zeroMV = { 0, 0 };
325 :    
326 :     int lx, ly, lz; /* left */
327 :     int tx, ty, tz; /* top */
328 :     int rx, ry, rz; /* top-right */
329 :     int lpos, tpos, rpos;
330 :     int num_cand, last_cand;
331 :    
332 :     VECTOR pmv[4]; /* left neighbour, top neighbour, top-right neighbour */
333 :    
334 :     switch (block) {
335 :     case 0:
336 :     lx = x - 1; ly = y; lz = 1;
337 :     tx = x; ty = y - 1; tz = 2;
338 :     rx = x + 1; ry = y - 1; rz = 2;
339 :     break;
340 :     case 1:
341 :     lx = x; ly = y; lz = 0;
342 :     tx = x; ty = y - 1; tz = 3;
343 :     rx = x + 1; ry = y - 1; rz = 2;
344 :     break;
345 :     case 2:
346 :     lx = x - 1; ly = y; lz = 3;
347 :     tx = x; ty = y; tz = 0;
348 :     rx = x; ry = y; rz = 1;
349 :     break;
350 :     default:
351 :     lx = x; ly = y; lz = 2;
352 :     tx = x; ty = y; tz = 0;
353 :     rx = x; ry = y; rz = 1;
354 :     }
355 :    
356 :     lpos = lx + ly * mb_width;
357 :     rpos = rx + ry * mb_width;
358 :     tpos = tx + ty * mb_width;
359 :     num_cand = 0;
360 :    
361 :     if (lpos >= bound && lx >= 0) {
362 :     num_cand++;
363 :     last_cand = 1;
364 :     pmv[1] = mbs[lpos].i_mvs[lz];
365 :     } else {
366 :     pmv[1] = zeroMV;
367 :     }
368 :    
369 :     if (tpos >= bound) {
370 :     num_cand++;
371 :     last_cand = 2;
372 :     pmv[2] = mbs[tpos].i_mvs[tz];
373 :     } else {
374 :     pmv[2] = zeroMV;
375 :     }
376 :    
377 :     if (rpos >= bound && rx < mb_width) {
378 :     num_cand++;
379 :     last_cand = 3;
380 :     pmv[3] = mbs[rpos].i_mvs[rz];
381 :     } else {
382 :     pmv[3] = zeroMV;
383 :     }
384 :    
385 :     /* if only one valid candidate predictor, the invalid candiates are set to the canidate */
386 :     if (num_cand != 1) {
387 :     /* set median */
388 :    
389 :     pmv[0].x =
390 :     MIN(MAX(pmv[1].x, pmv[2].x),
391 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
392 :     pmv[0].y =
393 :     MIN(MAX(pmv[1].y, pmv[2].y),
394 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
395 :     return pmv[0];
396 :     }
397 :    
398 :     return pmv[last_cand]; /* no point calculating median mv */
399 :     }
400 :    
401 :     static __inline int
402 :     get_ipmvdata(const MACROBLOCK * const mbs,
403 :     const int mb_width,
404 :     const int bound,
405 :     const int x,
406 :     const int y,
407 :     const int block,
408 :     VECTOR * const pmv,
409 :     int32_t * const psad)
410 :     {
411 :     static const VECTOR zeroMV = { 0, 0 };
412 :    
413 :     int lx, ly, lz; /* left */
414 :     int tx, ty, tz; /* top */
415 :     int rx, ry, rz; /* top-right */
416 :     int lpos, tpos, rpos;
417 :     int num_cand, last_cand;
418 :    
419 :     switch (block) {
420 :     case 0:
421 :     lx = x - 1; ly = y; lz = 1;
422 :     tx = x; ty = y - 1; tz = 2;
423 :     rx = x + 1; ry = y - 1; rz = 2;
424 :     break;
425 :     case 1:
426 :     lx = x; ly = y; lz = 0;
427 :     tx = x; ty = y - 1; tz = 3;
428 :     rx = x + 1; ry = y - 1; rz = 2;
429 :     break;
430 :     case 2:
431 :     lx = x - 1; ly = y; lz = 3;
432 :     tx = x; ty = y; tz = 0;
433 :     rx = x; ry = y; rz = 1;
434 :     break;
435 :     default:
436 :     lx = x; ly = y; lz = 2;
437 :     tx = x; ty = y; tz = 0;
438 :     rx = x; ry = y; rz = 1;
439 :     }
440 :    
441 :     lpos = lx + ly * mb_width;
442 :     rpos = rx + ry * mb_width;
443 :     tpos = tx + ty * mb_width;
444 :     num_cand = 0;
445 :    
446 :     if (lpos >= bound && lx >= 0) {
447 :     num_cand++;
448 :     last_cand = 1;
449 :     pmv[1] = mbs[lpos].i_mvs[lz];
450 :     psad[1] = mbs[lpos].i_sad8[lz];
451 :     } else {
452 :     pmv[1] = zeroMV;
453 :     psad[1] = MV_MAX_ERROR;
454 :     }
455 :    
456 :     if (tpos >= bound) {
457 :     num_cand++;
458 :     last_cand = 2;
459 :     pmv[2]= mbs[tpos].i_mvs[tz];
460 :     psad[2] = mbs[tpos].i_sad8[tz];
461 :     } else {
462 :     pmv[2] = zeroMV;
463 :     psad[2] = MV_MAX_ERROR;
464 :     }
465 :    
466 :     if (rpos >= bound && rx < mb_width) {
467 :     num_cand++;
468 :     last_cand = 3;
469 :     pmv[3] = mbs[rpos].i_mvs[rz];
470 :     psad[3] = mbs[rpos].i_sad8[rz];
471 : suxen_drol 1.12 } else {
472 :     pmv[3] = zeroMV;
473 :     psad[3] = MV_MAX_ERROR;
474 :     }
475 :    
476 :     /* original pmvdata() compatibility hack */
477 :     if (x == 0 && y == 0 && block == 0)
478 :     {
479 :     pmv[0] = pmv[1] = pmv[2] = pmv[3] = zeroMV;
480 :     psad[0] = 0;
481 :     psad[1] = psad[2] = psad[3] = MV_MAX_ERROR;
482 :     return 0;
483 :     }
484 :    
485 :     /* if only one valid candidate preictor, the invalid candiates are set to the canidate */
486 :     if (num_cand == 1) {
487 :     pmv[0] = pmv[last_cand];
488 :     psad[0] = psad[last_cand];
489 :     // return MVequal(pmv[0], zeroMV); /* no point calculating median mv and minimum sad */
490 :    
491 :     /* original pmvdata() compatibility hack */
492 :     return y==0 && block <= 1 ? 0 : MVequal(pmv[0], zeroMV);
493 :     }
494 :    
495 :     if ((MVequal(pmv[1], pmv[2])) && (MVequal(pmv[1], pmv[3]))) {
496 :     pmv[0] = pmv[1];
497 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
498 :     return 1;
499 :     /* compatibility patch */
500 :     //return y==0 && block <= 1 ? 0 : 1;
501 :     }
502 :    
503 :     /* set median, minimum */
504 :    
505 :     pmv[0].x =
506 :     MIN(MAX(pmv[1].x, pmv[2].x),
507 :     MIN(MAX(pmv[2].x, pmv[3].x), MAX(pmv[1].x, pmv[3].x)));
508 :     pmv[0].y =
509 :     MIN(MAX(pmv[1].y, pmv[2].y),
510 :     MIN(MAX(pmv[2].y, pmv[3].y), MAX(pmv[1].y, pmv[3].y)));
511 :    
512 :     psad[0] = MIN(MIN(psad[1], psad[2]), psad[3]);
513 :    
514 :     return 0;
515 :     }
516 : Isibaar 1.1
517 :    
518 : edgomez 1.9 #endif /* _MBPREDICTION_H_ */

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4