]> git.sesse.net Git - vlc/blob - plugins/motion/motion3dnow.c
* Altivec IDCT and motion compensation, based on Paul Mackerras's mpeg2dec
[vlc] / plugins / motion / motion3dnow.c
1 /*****************************************************************************
2  * motion3dnow.c : 3DNow! motion compensation module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: motion3dnow.c,v 1.2 2001/09/05 16:07:49 massiot Exp $
6  *
7  * Authors: Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
8  *          Michel Lespinasse <walken@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME motion3dnow
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <stdlib.h>                                      /* malloc(), free() */
34
35 #include "config.h"
36 #include "common.h"                                     /* boolean_t, byte_t */
37 #include "threads.h"
38 #include "mtime.h"
39 #include "tests.h"
40
41 #include "mmx.h"
42
43 #include "modules.h"
44 #include "modules_export.h"
45
46 /*****************************************************************************
47  * Local and extern prototypes.
48  *****************************************************************************/
49 static void motion_getfunctions( function_list_t * p_function_list );
50
51 /*****************************************************************************
52  * Build configuration tree.
53  *****************************************************************************/
54 MODULE_CONFIG_START
55 ADD_WINDOW( "Configuration for 3DNow! motion compensation module" )
56     ADD_COMMENT( "Ha, ha -- nothing to configure yet" )
57 MODULE_CONFIG_STOP
58
59 MODULE_INIT_START
60     p_module->i_capabilities = MODULE_CAPABILITY_NULL
61                                 | MODULE_CAPABILITY_MOTION;
62     p_module->psz_longname = "3DNow! motion compensation module";
63 MODULE_INIT_STOP
64
65 MODULE_ACTIVATE_START
66     motion_getfunctions( &p_module->p_functions->motion );
67 MODULE_ACTIVATE_STOP
68
69 MODULE_DEACTIVATE_START
70 MODULE_DEACTIVATE_STOP
71
72 /*****************************************************************************
73  * motion_Probe: tests probe the CPU and return a score
74  *****************************************************************************/
75 static int motion_Probe( probedata_t *p_data )
76 {
77     if( !TestCPU( CPU_CAPABILITY_3DNOW ) )
78     {
79         return( 0 );
80     }
81
82     if( TestMethod( MOTION_METHOD_VAR, "motion3dnow" )
83          || TestMethod( MOTION_METHOD_VAR, "3dnow" ) )
84     {
85         return( 999 );
86     }
87
88     return( 250 );
89 }
90
91 /*****************************************************************************
92  * Motion compensation in 3DNow (OK I know this does MMXEXT too and it's ugly)
93  *****************************************************************************/
94
95 #define CPU_MMXEXT 0
96 #define CPU_3DNOW 1
97
98
99 //CPU_MMXEXT/CPU_3DNOW adaptation layer
100
101 #define pavg_r2r(src,dest)                                                  \
102 do {                                                                        \
103     if (cpu == CPU_MMXEXT)                                                  \
104         pavgb_r2r (src, dest);                                              \
105     else                                                                    \
106         pavgusb_r2r (src, dest);                                            \
107 } while (0)
108
109 #define pavg_m2r(src,dest)                                                  \
110 do {                                                                        \
111     if (cpu == CPU_MMXEXT)                                                  \
112         pavgb_m2r (src, dest);                                              \
113     else                                                                    \
114         pavgusb_m2r (src, dest);                                            \
115 } while (0)
116
117
118 //CPU_MMXEXT code
119
120
121 static __inline__ void MC_put1_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
122                               int stride)
123 {
124     do {
125         movq_m2r (*ref, mm0);
126         movq_r2m (mm0, *dest);
127         ref += stride;
128         dest += stride;
129     } while (--height);
130 }
131
132 static __inline__ void MC_put1_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
133                                int stride)
134 {
135     do {
136         movq_m2r (*ref, mm0);
137         movq_m2r (*(ref+8), mm1);
138         ref += stride;
139         movq_r2m (mm0, *dest);
140         movq_r2m (mm1, *(dest+8));
141         dest += stride;
142     } while (--height);
143 }
144
145 static __inline__ void MC_avg1_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
146                               int stride, int cpu)
147 {
148     do {
149         movq_m2r (*ref, mm0);
150         pavg_m2r (*dest, mm0);
151         ref += stride;
152         movq_r2m (mm0, *dest);
153         dest += stride;
154     } while (--height);
155 }
156
157 static __inline__ void MC_avg1_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
158                                int stride, int cpu)
159 {
160     do {
161         movq_m2r (*ref, mm0);
162         movq_m2r (*(ref+8), mm1);
163         pavg_m2r (*dest, mm0);
164         pavg_m2r (*(dest+8), mm1);
165         movq_r2m (mm0, *dest);
166         ref += stride;
167         movq_r2m (mm1, *(dest+8));
168         dest += stride;
169     } while (--height);
170 }
171
172 static __inline__ void MC_put2_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
173                               int stride, int offset, int cpu)
174 {
175     do {
176         movq_m2r (*ref, mm0);
177         pavg_m2r (*(ref+offset), mm0);
178         ref += stride;
179         movq_r2m (mm0, *dest);
180         dest += stride;
181     } while (--height);
182 }
183
184 static __inline__ void MC_put2_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
185                                int stride, int offset, int cpu)
186 {
187     do {
188         movq_m2r (*ref, mm0);
189         movq_m2r (*(ref+8), mm1);
190         pavg_m2r (*(ref+offset), mm0);
191         pavg_m2r (*(ref+offset+8), mm1);
192         movq_r2m (mm0, *dest);
193         ref += stride;
194         movq_r2m (mm1, *(dest+8));
195         dest += stride;
196     } while (--height);
197 }
198
199 static __inline__ void MC_avg2_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
200                               int stride, int offset, int cpu)
201 {
202     do {
203         movq_m2r (*ref, mm0);
204         pavg_m2r (*(ref+offset), mm0);
205         pavg_m2r (*dest, mm0);
206         ref += stride;
207         movq_r2m (mm0, *dest);
208         dest += stride;
209     } while (--height);
210 }
211
212 static __inline__ void MC_avg2_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
213                                int stride, int offset, int cpu)
214 {
215     do {
216         movq_m2r (*ref, mm0);
217         movq_m2r (*(ref+8), mm1);
218         pavg_m2r (*(ref+offset), mm0);
219         pavg_m2r (*(ref+offset+8), mm1);
220         pavg_m2r (*dest, mm0);
221         pavg_m2r (*(dest+8), mm1);
222         ref += stride;
223         movq_r2m (mm0, *dest);
224         movq_r2m (mm1, *(dest+8));
225         dest += stride;
226     } while (--height);
227 }
228
229 static mmx_t mask_one = {0x0101010101010101LL};
230
231 static __inline__ void MC_put4_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
232                               int stride, int cpu)
233 {
234     movq_m2r (*ref, mm0);
235     movq_m2r (*(ref+1), mm1);
236     movq_r2r (mm0, mm7);
237     pxor_r2r (mm1, mm7);
238     pavg_r2r (mm1, mm0);
239     ref += stride;
240
241     do {
242         movq_m2r (*ref, mm2);
243         movq_r2r (mm0, mm5);
244
245         movq_m2r (*(ref+1), mm3);
246         movq_r2r (mm2, mm6);
247
248         pxor_r2r (mm3, mm6);
249         pavg_r2r (mm3, mm2);
250
251         por_r2r (mm6, mm7);
252         pxor_r2r (mm2, mm5);
253
254         pand_r2r (mm5, mm7);
255         pavg_r2r (mm2, mm0);
256
257         pand_m2r (mask_one, mm7);
258
259         psubusb_r2r (mm7, mm0);
260
261         ref += stride;
262         movq_r2m (mm0, *dest);
263         dest += stride;
264
265         movq_r2r (mm6, mm7);        // unroll !
266         movq_r2r (mm2, mm0);        // unroll !
267     } while (--height);
268 }
269
270 static __inline__ void MC_put4_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
271                                int stride, int cpu)
272 {
273     do {
274         movq_m2r (*ref, mm0);
275         movq_m2r (*(ref+stride+1), mm1);
276         movq_r2r (mm0, mm7);
277         movq_m2r (*(ref+1), mm2);
278         pxor_r2r (mm1, mm7);
279         movq_m2r (*(ref+stride), mm3);
280         movq_r2r (mm2, mm6);
281         pxor_r2r (mm3, mm6);
282         pavg_r2r (mm1, mm0);
283         pavg_r2r (mm3, mm2);
284         por_r2r (mm6, mm7);
285         movq_r2r (mm0, mm6);
286         pxor_r2r (mm2, mm6);
287         pand_r2r (mm6, mm7);
288         pand_m2r (mask_one, mm7);
289         pavg_r2r (mm2, mm0);
290         psubusb_r2r (mm7, mm0);
291         movq_r2m (mm0, *dest);
292
293         movq_m2r (*(ref+8), mm0);
294         movq_m2r (*(ref+stride+9), mm1);
295         movq_r2r (mm0, mm7);
296         movq_m2r (*(ref+9), mm2);
297         pxor_r2r (mm1, mm7);
298         movq_m2r (*(ref+stride+8), mm3);
299         movq_r2r (mm2, mm6);
300         pxor_r2r (mm3, mm6);
301         pavg_r2r (mm1, mm0);
302         pavg_r2r (mm3, mm2);
303         por_r2r (mm6, mm7);
304         movq_r2r (mm0, mm6);
305         pxor_r2r (mm2, mm6);
306         pand_r2r (mm6, mm7);
307         pand_m2r (mask_one, mm7);
308         pavg_r2r (mm2, mm0);
309         psubusb_r2r (mm7, mm0);
310         ref += stride;
311         movq_r2m (mm0, *(dest+8));
312         dest += stride;
313     } while (--height);
314 }
315
316 static __inline__ void MC_avg4_8 (int height, yuv_data_t * dest, yuv_data_t * ref,
317                               int stride, int cpu)
318 {
319     do {
320         movq_m2r (*ref, mm0);
321         movq_m2r (*(ref+stride+1), mm1);
322         movq_r2r (mm0, mm7);
323         movq_m2r (*(ref+1), mm2);
324         pxor_r2r (mm1, mm7);
325         movq_m2r (*(ref+stride), mm3);
326         movq_r2r (mm2, mm6);
327         pxor_r2r (mm3, mm6);
328         pavg_r2r (mm1, mm0);
329         pavg_r2r (mm3, mm2);
330         por_r2r (mm6, mm7);
331         movq_r2r (mm0, mm6);
332         pxor_r2r (mm2, mm6);
333         pand_r2r (mm6, mm7);
334         pand_m2r (mask_one, mm7);
335         pavg_r2r (mm2, mm0);
336         psubusb_r2r (mm7, mm0);
337         movq_m2r (*dest, mm1);
338         pavg_r2r (mm1, mm0);
339         ref += stride;
340         movq_r2m (mm0, *dest);
341         dest += stride;
342     } while (--height);
343 }
344
345 static __inline__ void MC_avg4_16 (int height, yuv_data_t * dest, yuv_data_t * ref,
346                                int stride, int cpu)
347 {
348     do {
349         movq_m2r (*ref, mm0);
350         movq_m2r (*(ref+stride+1), mm1);
351         movq_r2r (mm0, mm7);
352         movq_m2r (*(ref+1), mm2);
353         pxor_r2r (mm1, mm7);
354         movq_m2r (*(ref+stride), mm3);
355         movq_r2r (mm2, mm6);
356         pxor_r2r (mm3, mm6);
357         pavg_r2r (mm1, mm0);
358         pavg_r2r (mm3, mm2);
359         por_r2r (mm6, mm7);
360         movq_r2r (mm0, mm6);
361         pxor_r2r (mm2, mm6);
362         pand_r2r (mm6, mm7);
363         pand_m2r (mask_one, mm7);
364         pavg_r2r (mm2, mm0);
365         psubusb_r2r (mm7, mm0);
366         movq_m2r (*dest, mm1);
367         pavg_r2r (mm1, mm0);
368         movq_r2m (mm0, *dest);
369
370         movq_m2r (*(ref+8), mm0);
371         movq_m2r (*(ref+stride+9), mm1);
372         movq_r2r (mm0, mm7);
373         movq_m2r (*(ref+9), mm2);
374         pxor_r2r (mm1, mm7);
375         movq_m2r (*(ref+stride+8), mm3);
376         movq_r2r (mm2, mm6);
377         pxor_r2r (mm3, mm6);
378         pavg_r2r (mm1, mm0);
379         pavg_r2r (mm3, mm2);
380         por_r2r (mm6, mm7);
381         movq_r2r (mm0, mm6);
382         pxor_r2r (mm2, mm6);
383         pand_r2r (mm6, mm7);
384         pand_m2r (mask_one, mm7);
385         pavg_r2r (mm2, mm0);
386         psubusb_r2r (mm7, mm0);
387         movq_m2r (*(dest+8), mm1);
388         pavg_r2r (mm1, mm0);
389         ref += stride;
390         movq_r2m (mm0, *(dest+8));
391         dest += stride;
392     } while (--height);
393 }
394
395 static void MC_avg_16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
396                               int stride, int height)
397 {
398     MC_avg1_16 (height, dest, ref, stride, CPU_MMXEXT);
399 }
400
401 static void MC_avg_8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
402                              int stride, int height)
403 {
404     MC_avg1_8 (height, dest, ref, stride, CPU_MMXEXT);
405 }
406
407 static void MC_put_16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
408                               int stride, int height)
409 {
410     MC_put1_16 (height, dest, ref, stride);
411 }
412
413 static void MC_put_8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
414                              int stride, int height)
415 {
416     MC_put1_8 (height, dest, ref, stride);
417 }
418
419 static void MC_avg_x16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
420                                int stride, int height)
421 {
422     MC_avg2_16 (height, dest, ref, stride, 1, CPU_MMXEXT);
423 }
424
425 static void MC_avg_x8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
426                               int stride, int height)
427 {
428     MC_avg2_8 (height, dest, ref, stride, 1, CPU_MMXEXT);
429 }
430
431 static void MC_put_x16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
432                                int stride, int height)
433 {
434     MC_put2_16 (height, dest, ref, stride, 1, CPU_MMXEXT);
435 }
436
437 static void MC_put_x8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
438                               int stride, int height)
439 {
440     MC_put2_8 (height, dest, ref, stride, 1, CPU_MMXEXT);
441 }
442
443 static void MC_avg_y16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
444                                int stride, int height)
445 {
446     MC_avg2_16 (height, dest, ref, stride, stride, CPU_MMXEXT);
447 }
448
449 static void MC_avg_y8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
450                               int stride, int height)
451 {
452     MC_avg2_8 (height, dest, ref, stride, stride, CPU_MMXEXT);
453 }
454
455 static void MC_put_y16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
456                                int stride, int height)
457 {
458     MC_put2_16 (height, dest, ref, stride, stride, CPU_MMXEXT);
459 }
460
461 static void MC_put_y8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
462                               int stride, int height)
463 {
464     MC_put2_8 (height, dest, ref, stride, stride, CPU_MMXEXT);
465 }
466
467 static void MC_avg_xy16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
468                                 int stride, int height)
469 {
470     MC_avg4_16 (height, dest, ref, stride, CPU_MMXEXT);
471 }
472
473 static void MC_avg_xy8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
474                                int stride, int height)
475 {
476     MC_avg4_8 (height, dest, ref, stride, CPU_MMXEXT);
477 }
478
479 static void MC_put_xy16_mmxext (yuv_data_t * dest, yuv_data_t * ref,
480                                 int stride, int height)
481 {
482     MC_put4_16 (height, dest, ref, stride, CPU_MMXEXT);
483 }
484
485 static void MC_put_xy8_mmxext (yuv_data_t * dest, yuv_data_t * ref,
486                                int stride, int height)
487 {
488     MC_put4_8 (height, dest, ref, stride, CPU_MMXEXT);
489 }
490
491
492 static void MC_avg_16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
493                               int stride, int height)
494 {
495     MC_avg1_16 (height, dest, ref, stride, CPU_3DNOW);
496 }
497
498 static void MC_avg_8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
499                              int stride, int height)
500 {
501     MC_avg1_8 (height, dest, ref, stride, CPU_3DNOW);
502 }
503
504 static void MC_put_16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
505                               int stride, int height)
506 {
507     MC_put1_16 (height, dest, ref, stride);
508 }
509
510 static void MC_put_8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
511                              int stride, int height)
512 {
513     MC_put1_8 (height, dest, ref, stride);
514 }
515
516 static void MC_avg_x16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
517                                int stride, int height)
518 {
519     MC_avg2_16 (height, dest, ref, stride, 1, CPU_3DNOW);
520 }
521
522 static void MC_avg_x8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
523                               int stride, int height)
524 {
525     MC_avg2_8 (height, dest, ref, stride, 1, CPU_3DNOW);
526 }
527
528 static void MC_put_x16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
529                                int stride, int height)
530 {
531     MC_put2_16 (height, dest, ref, stride, 1, CPU_3DNOW);
532 }
533
534 static void MC_put_x8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
535                               int stride, int height)
536 {
537     MC_put2_8 (height, dest, ref, stride, 1, CPU_3DNOW);
538 }
539
540 static void MC_avg_y16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
541                                int stride, int height)
542 {
543     MC_avg2_16 (height, dest, ref, stride, stride, CPU_3DNOW);
544 }
545
546 static void MC_avg_y8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
547                               int stride, int height)
548 {
549     MC_avg2_8 (height, dest, ref, stride, stride, CPU_3DNOW);
550 }
551
552 static void MC_put_y16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
553                                int stride, int height)
554 {
555     MC_put2_16 (height, dest, ref, stride, stride, CPU_3DNOW);
556 }
557
558 static void MC_put_y8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
559                               int stride, int height)
560 {
561     MC_put2_8 (height, dest, ref, stride, stride, CPU_3DNOW);
562 }
563
564 static void MC_avg_xy16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
565                                 int stride, int height)
566 {
567     MC_avg4_16 (height, dest, ref, stride, CPU_3DNOW);
568 }
569
570 static void MC_avg_xy8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
571                                int stride, int height)
572 {
573     MC_avg4_8 (height, dest, ref, stride, CPU_3DNOW);
574 }
575
576 static void MC_put_xy16_3dnow (yuv_data_t * dest, yuv_data_t * ref,
577                                 int stride, int height)
578 {
579     MC_put4_16 (height, dest, ref, stride, CPU_3DNOW);
580 }
581
582 static void MC_put_xy8_3dnow (yuv_data_t * dest, yuv_data_t * ref,
583                                int stride, int height)
584 {
585     MC_put4_8 (height, dest, ref, stride, CPU_3DNOW);
586 }
587
588 /*****************************************************************************
589  * Functions exported as capabilities. They are declared as static so that
590  * we don't pollute the namespace too much.
591  *****************************************************************************/
592 static void motion_getfunctions( function_list_t * p_function_list )
593 {
594     static void (* ppppf_motion[2][2][4])( yuv_data_t *, yuv_data_t *,
595                                            int, int ) =
596     {
597         {
598             /* Copying functions */
599             {
600                 /* Width == 16 */
601                 MC_put_16_3dnow, MC_put_x16_3dnow, MC_put_y16_3dnow, MC_put_xy16_3dnow
602             },
603             {
604                 /* Width == 8 */
605                 MC_put_8_3dnow,  MC_put_x8_3dnow,  MC_put_y8_3dnow, MC_put_xy8_3dnow
606             }
607         },
608         {
609             /* Averaging functions */
610             {
611                 /* Width == 16 */
612                 MC_avg_16_3dnow, MC_avg_x16_3dnow, MC_avg_y16_3dnow, MC_avg_xy16_3dnow
613             },
614             {
615                 /* Width == 8 */
616                 MC_avg_8_3dnow,  MC_avg_x8_3dnow,  MC_avg_y8_3dnow,  MC_avg_xy8_3dnow
617             }
618         }
619     };
620
621     p_function_list->pf_probe = motion_Probe;
622
623 #define list p_function_list->functions.motion
624     memcpy( list.ppppf_motion, ppppf_motion, sizeof( void * ) * 16 );
625 #undef list
626
627     return;
628 }
629