1 /* predict.c, motion compensated prediction */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
33 #include "cpu_accel.h"
37 /* private prototypes */
38 static void predict_mb (
40 uint8_t *oldref
[], uint8_t *newref
[], uint8_t *cur
[],
41 int lx
, int bx
, int by
, mbinfo_s
*mbi
, int secondfield
);
45 uint8_t *src
[], int sfield
,
46 uint8_t *dst
[], int dfield
,
47 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
);
49 static void pred_comp (
51 uint8_t *src
, uint8_t *dst
,
52 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
);
54 static void pred_comp_mmxe(
56 uint8_t *src
, uint8_t *dst
,
57 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
);
58 static void pred_comp_mmx(
60 uint8_t *src
, uint8_t *dst
,
61 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
);
64 ( pict_data_s
*picture
,int DMV
[][2],
65 int *dmvector
, int mvx
, int mvy
);
67 static void clearblock (pict_data_s
*picture
,
68 uint8_t *cur
[], int i0
, int j0
);
71 Initialise prediction - currently purely selection of which
72 versions of the various low level computation routines to use
76 static void (*ppred_comp
)(
78 uint8_t *src
, uint8_t *dst
,
79 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
);
83 int cpucap
= cpu_accel();
85 if( cpucap
== 0 ) /* No MMX/SSE etc support available */
87 ppred_comp
= pred_comp
;
91 else if(cpucap
& ACCEL_X86_MMXEXT
) /* AMD MMX or SSE... */
93 if(verbose
) fprintf( stderr
, "SETTING EXTENDED MMX for PREDICTION!\n");
94 ppred_comp
= pred_comp_mmxe
;
96 else if(cpucap
& ACCEL_X86_MMX
) /* Original MMX... */
98 if(verbose
) fprintf( stderr
, "SETTING MMX for PREDICTION!\n");
99 ppred_comp
= pred_comp_mmx
;
104 ppred_comp
= pred_comp
;
108 /* form prediction for a complete picture (frontend for predict_mb)
110 * reff: reference frame for forward prediction
111 * refb: reference frame for backward prediction
112 * cur: destination (current) frame
113 * secondfield: predict second field of a frame
114 * mbi: macroblock info
120 void predict(pict_data_s
*picture
,
127 mbinfo_s
*mbi
= picture
->mbinfo
;
130 /* loop through all macroblocks of the picture */
131 for (j
=0; j
<height2
; j
+=16)
132 for (i
=0; i
<width
; i
+=16)
134 predict_mb(picture
,reff
,refb
,cur
,width
,i
,j
,
135 &mbi
[k
], secondfield
);
140 /* form prediction for one macroblock
142 * oldref: reference frame for forward prediction
143 * newref: reference frame for backward prediction
144 * cur: destination (current) frame
145 * lx: frame width (identical to global var `width')
146 * bx,by: picture (field or frame) coordinates of macroblock to be predicted
147 * pict_type: I, P or B
148 * pict_struct: FRAME_PICTURE, TOP_FIELD, BOTTOM_FIELD
149 * mb_type: MB_FORWARD, MB_BACKWARD, MB_INTRA
150 * motion_type: MC_FRAME, MC_FIELD, MC_16X8, MC_DMV
151 * secondfield: predict second field of a frame
152 * PMV[2][2][2]: motion vectors (in half pel picture coordinates)
153 * mv_field_sel[2][2]: motion vertical field selects (for field predictions)
154 * dmvector: differential motion vectors (for dual prime)
157 * - when predicting a P type picture which is the second field of
158 * a frame, the same parity reference field is in oldref, while the
159 * opposite parity reference field is assumed to be in newref!
160 * - intra macroblocks are modelled to have a constant prediction of 128
161 * for all pels; this results in a DC DCT coefficient symmetric to 0
162 * - vectors for field prediction in frame pictures are in half pel frame
163 * coordinates (vertical component is twice the field value and always
166 * already covers dual prime (not yet used)
169 static void predict_mb (
170 pict_data_s
*picture
,
171 uint8_t *oldref
[], uint8_t *newref
[], uint8_t *cur
[],
172 int lx
, int bx
, int by
, mbinfo_s
*mbi
, int secondfield
175 int addflag
, currentfield
;
179 if (mbi
->mb_type
&MB_INTRA
)
181 clearblock(picture
,cur
,bx
,by
);
185 addflag
= 0; /* first prediction is stored, second is added and averaged */
187 if ((mbi
->mb_type
& MB_FORWARD
) || (picture
->pict_type
==P_TYPE
))
189 /* forward prediction, including zero MV in P pictures */
191 if (picture
->pict_struct
==FRAME_PICTURE
)
195 if ((mbi
->motion_type
==MC_FRAME
) || !(mbi
->mb_type
& MB_FORWARD
))
197 /* frame-based prediction in frame picture */
200 lx
,16,16,bx
,by
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1],0);
202 else if (mbi
->motion_type
==MC_FIELD
)
204 /* field-based prediction in frame picture
206 * note scaling of the vertical coordinates (by, mbi->MV[][0][1])
207 * from frame to field!
210 /* top field prediction */
211 pred(picture
,oldref
,mbi
->mv_field_sel
[0][0],cur
,0,
212 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1]>>1,0);
214 /* bottom field prediction */
215 pred(picture
,oldref
,mbi
->mv_field_sel
[1][0],cur
,1,
216 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[1][0][0],mbi
->MV
[1][0][1]>>1,0);
218 else if (mbi
->motion_type
==MC_DMV
)
220 /* dual prime prediction */
222 /* calculate derived motion vectors */
223 calc_DMV(picture
,DMV
,mbi
->dmvector
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1]>>1);
225 /* predict top field from top field */
226 pred(picture
,oldref
,0,cur
,0,
227 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1]>>1,0);
229 /* predict bottom field from bottom field */
230 pred(picture
,oldref
,1,cur
,1,
231 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1]>>1,0);
233 /* predict and add to top field from bottom field */
234 pred(picture
,oldref
,1,cur
,0,
235 lx
<<1,16,8,bx
,by
>>1,DMV
[0][0],DMV
[0][1],1);
237 /* predict and add to bottom field from top field */
238 pred(picture
,oldref
,0,cur
,1,
239 lx
<<1,16,8,bx
,by
>>1,DMV
[1][0],DMV
[1][1],1);
243 /* invalid mbi->motion_type in frame picture */
244 fprintf(stderr
,"invalid motion_type\n");
247 else /* TOP_FIELD or BOTTOM_FIELD */
251 currentfield
= (picture
->pict_struct
==BOTTOM_FIELD
);
253 /* determine which frame to use for prediction */
254 if ((picture
->pict_type
==P_TYPE
) && secondfield
255 && (currentfield
!=mbi
->mv_field_sel
[0][0]))
256 predframe
= newref
; /* same frame */
258 predframe
= oldref
; /* previous frame */
260 if ((mbi
->motion_type
==MC_FIELD
) || !(mbi
->mb_type
& MB_FORWARD
))
262 /* field-based prediction in field picture */
263 pred(picture
,predframe
,mbi
->mv_field_sel
[0][0],cur
,currentfield
,
264 lx
<<1,16,16,bx
,by
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1],0);
266 else if (mbi
->motion_type
==MC_16X8
)
268 /* 16 x 8 motion compensation in field picture */
271 pred(picture
,predframe
,mbi
->mv_field_sel
[0][0],cur
,currentfield
,
272 lx
<<1,16,8,bx
,by
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1],0);
274 /* determine which frame to use for lower half prediction */
275 if ((picture
->pict_type
==P_TYPE
) && secondfield
276 && (currentfield
!=mbi
->mv_field_sel
[1][0]))
277 predframe
= newref
; /* same frame */
279 predframe
= oldref
; /* previous frame */
282 pred(picture
,predframe
,mbi
->mv_field_sel
[1][0],cur
,currentfield
,
283 lx
<<1,16,8,bx
,by
+8,mbi
->MV
[1][0][0],mbi
->MV
[1][0][1],0);
285 else if (mbi
->motion_type
==MC_DMV
)
287 /* dual prime prediction */
289 /* determine which frame to use for prediction */
291 predframe
= newref
; /* same frame */
293 predframe
= oldref
; /* previous frame */
295 /* calculate derived motion vectors */
296 calc_DMV(picture
,DMV
,mbi
->dmvector
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1]);
298 /* predict from field of same parity */
299 pred(picture
,oldref
,currentfield
,cur
,currentfield
,
300 lx
<<1,16,16,bx
,by
,mbi
->MV
[0][0][0],mbi
->MV
[0][0][1],0);
302 /* predict from field of opposite parity */
303 pred(picture
,predframe
,!currentfield
,cur
,currentfield
,
304 lx
<<1,16,16,bx
,by
,DMV
[0][0],DMV
[0][1],1);
308 /* invalid motion_type in field picture */
309 fprintf(stderr
,"invalid motion_type\n");
312 addflag
= 1; /* next prediction (if any) will be averaged with this one */
315 if (mbi
->mb_type
& MB_BACKWARD
)
317 /* backward prediction */
319 if (picture
->pict_struct
==FRAME_PICTURE
)
323 if (mbi
->motion_type
==MC_FRAME
)
325 /* frame-based prediction in frame picture */
326 pred(picture
,newref
,0,cur
,0,
327 lx
,16,16,bx
,by
,mbi
->MV
[0][1][0],mbi
->MV
[0][1][1],addflag
);
331 /* field-based prediction in frame picture
333 * note scaling of the vertical coordinates (by, mbi->MV[][1][1])
334 * from frame to field!
337 /* top field prediction */
338 pred(picture
,newref
,mbi
->mv_field_sel
[0][1],cur
,0,
339 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[0][1][0],mbi
->MV
[0][1][1]>>1,addflag
);
341 /* bottom field prediction */
342 pred(picture
,newref
,mbi
->mv_field_sel
[1][1],cur
,1,
343 lx
<<1,16,8,bx
,by
>>1,mbi
->MV
[1][1][0],mbi
->MV
[1][1][1]>>1,addflag
);
346 else /* TOP_FIELD or BOTTOM_FIELD */
350 currentfield
= (picture
->pict_struct
==BOTTOM_FIELD
);
352 if (mbi
->motion_type
==MC_FIELD
)
354 /* field-based prediction in field picture */
355 pred(picture
,newref
,mbi
->mv_field_sel
[0][1],cur
,currentfield
,
356 lx
<<1,16,16,bx
,by
,mbi
->MV
[0][1][0],mbi
->MV
[0][1][1],addflag
);
358 else if (mbi
->motion_type
==MC_16X8
)
360 /* 16 x 8 motion compensation in field picture */
363 pred(picture
,newref
,mbi
->mv_field_sel
[0][1],cur
,currentfield
,
364 lx
<<1,16,8,bx
,by
,mbi
->MV
[0][1][0],mbi
->MV
[0][1][1],addflag
);
367 pred(picture
,newref
,mbi
->mv_field_sel
[1][1],cur
,currentfield
,
368 lx
<<1,16,8,bx
,by
+8,mbi
->MV
[1][1][0],mbi
->MV
[1][1][1],addflag
);
372 /* invalid motion_type in field picture */
373 fprintf(stderr
,"invalid motion_type\n");
379 /* predict a rectangular block (all three components)
381 * src: source frame (Y,U,V)
382 * sfield: source field select (0: frame or top field, 1: bottom field)
383 * dst: destination frame (Y,U,V)
384 * dfield: destination field select (0: frame or top field, 1: bottom field)
386 * the following values are in luminance picture (frame or field) dimensions
387 * lx: distance of vertically adjacent pels (selects frame or field pred.)
388 * w,h: width and height of block (only 16x16 or 16x8 are used)
389 * x,y: coordinates of destination block
390 * dx,dy: half pel motion vector
391 * addflag: store or add (= average) prediction
395 pict_data_s
*picture
,
396 uint8_t *src
[], int sfield
,
397 uint8_t *dst
[], int dfield
,
398 int lx
, int w
, int h
, int x
, int y
, int dx
, int dy
, int addflag
403 for (cc
=0; cc
<3; cc
++)
407 /* scale for color components */
408 if (chroma_format
==CHROMA420
)
411 h
>>= 1; y
>>= 1; dy
/= 2;
413 if (chroma_format
!=CHROMA444
)
416 w
>>= 1; x
>>= 1; dx
/= 2;
420 (*ppred_comp
)( picture
,
421 src
[cc
]+(sfield
?lx
>>1:0),dst
[cc
]+(dfield
?lx
>>1:0),
422 lx
,w
,h
,x
,y
,dx
,dy
,addflag
);
426 /* low level prediction routine
428 * src: prediction source
429 * dst: prediction destination
430 * lx: line width (for both src and dst)
431 * x,y: destination coordinates
432 * dx,dy: half pel motion vector
433 * w,h: size of prediction block
434 * addflag: store or add prediction
436 * There are also SIMD versions of this routine...
439 static void pred_comp(
440 pict_data_s
*picture
,
449 int xint
, xh
, yint
, yh
;
453 /* half pel scaling */
454 xint
= dx
>>1; /* integer part */
455 xh
= dx
& 1; /* half pel flag */
460 s
= src
+ lx
*(y
+yint
) + (x
+xint
); /* motion vector */
468 d
[i
] = (unsigned int)(d
[i
]+s
[i
]+1)>>1;
485 d
[i
] = (d
[i
] + ((unsigned int)(s
[i
]+s
[i
+lx
]+1)>>1)+1)>>1;
493 d
[i
] = (unsigned int)(s
[i
]+s
[i
+lx
]+1)>>1;
502 d
[i
] = (d
[i
] + ((unsigned int)(s
[i
]+s
[i
+1]+1)>>1)+1)>>1;
510 d
[i
] = (unsigned int)(s
[i
]+s
[i
+1]+1)>>1;
514 else /* if (xh && yh) */
519 d
[i
] = (d
[i
] + ((unsigned int)(s
[i
]+s
[i
+1]+s
[i
+lx
]+s
[i
+lx
+1]+2)>>2)+1)>>1;
527 d
[i
] = (unsigned int)(s
[i
]+s
[i
+1]+s
[i
+lx
]+s
[i
+lx
+1]+2)>>2;
534 static void pred_comp_mmxe(
535 pict_data_s
*picture
,
544 int xint
, xh
, yint
, yh
;
547 /* half pel scaling */
548 xint
= dx
>>1; /* integer part */
549 xh
= dx
& 1; /* half pel flag */
554 s
= src
+ lx
*(y
+yint
) + (x
+xint
); /* motion vector */
560 predcomp_11_mmxe(s
,d
,lx
,w
,h
,addflag
);
562 predcomp_10_mmxe(s
,d
,lx
,w
,h
,addflag
);
567 predcomp_01_mmxe(s
,d
,lx
,w
,h
,addflag
);
569 predcomp_00_mmxe(s
,d
,lx
,w
,h
,addflag
);
574 static void pred_comp_mmx(
575 pict_data_s
*picture
,
584 int xint
, xh
, yint
, yh
;
587 /* half pel scaling */
588 xint
= dx
>>1; /* integer part */
589 xh
= dx
& 1; /* half pel flag */
594 s
= src
+ lx
*(y
+yint
) + (x
+xint
); /* motion vector */
600 predcomp_11_mmx(s
,d
,lx
,w
,h
,addflag
);
602 predcomp_10_mmx(s
,d
,lx
,w
,h
,addflag
);
607 predcomp_01_mmx(s
,d
,lx
,w
,h
,addflag
);
609 predcomp_00_mmx(s
,d
,lx
,w
,h
,addflag
);
615 /* calculate derived motion vectors (DMV) for dual prime prediction
616 * dmvector[2]: differential motion vectors (-1,0,+1)
617 * mvx,mvy: motion vector (for same parity)
619 * DMV[2][2]: derived motion vectors (for opposite parity)
621 * uses global variables pict_struct and topfirst
624 * - all vectors are in field coordinates (even for frame pictures)
627 static void calc_DMV(
628 pict_data_s
*picture
,int DMV
[][2],
629 int *dmvector
, int mvx
, int mvy
632 if (picture
->pict_struct
==FRAME_PICTURE
)
634 if (picture
->topfirst
)
636 /* vector for prediction of top field from bottom field */
637 DMV
[0][0] = ((mvx
+(mvx
>0))>>1) + dmvector
[0];
638 DMV
[0][1] = ((mvy
+(mvy
>0))>>1) + dmvector
[1] - 1;
640 /* vector for prediction of bottom field from top field */
641 DMV
[1][0] = ((3*mvx
+(mvx
>0))>>1) + dmvector
[0];
642 DMV
[1][1] = ((3*mvy
+(mvy
>0))>>1) + dmvector
[1] + 1;
646 /* vector for prediction of top field from bottom field */
647 DMV
[0][0] = ((3*mvx
+(mvx
>0))>>1) + dmvector
[0];
648 DMV
[0][1] = ((3*mvy
+(mvy
>0))>>1) + dmvector
[1] - 1;
650 /* vector for prediction of bottom field from top field */
651 DMV
[1][0] = ((mvx
+(mvx
>0))>>1) + dmvector
[0];
652 DMV
[1][1] = ((mvy
+(mvy
>0))>>1) + dmvector
[1] + 1;
657 /* vector for prediction from field of opposite 'parity' */
658 DMV
[0][0] = ((mvx
+(mvx
>0))>>1) + dmvector
[0];
659 DMV
[0][1] = ((mvy
+(mvy
>0))>>1) + dmvector
[1];
661 /* correct for vertical field shift */
662 if (picture
->pict_struct
==TOP_FIELD
)
669 static void clearblock(
670 pict_data_s
*picture
,
671 uint8_t *cur
[], int i0
, int j0
677 p
= cur
[0] + ((picture
->pict_struct
==BOTTOM_FIELD
) ? width
: 0) + i0
+ width2
*j0
;
688 if (chroma_format
!=CHROMA444
)
693 if (chroma_format
==CHROMA420
)
698 p
= cur
[1] + ((picture
->pict_struct
==BOTTOM_FIELD
) ? chrom_width
: 0) + i0
708 p
= cur
[2] + ((picture
->pict_struct
==BOTTOM_FIELD
) ? chrom_width
: 0) + i0