Make mouse-moved and mouse-clicked coordinates, remove mouse-x and -y
[vlc.git] / modules / video_filter / transform.c
1 /*****************************************************************************
2  * transform.c : transform image module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Samuel Hocevar <sam@zoy.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_vout.h>
35
36 #include "filter_common.h"
37 #include "filter_picture.h"
38
39 #define TRANSFORM_MODE_HFLIP   1
40 #define TRANSFORM_MODE_VFLIP   2
41 #define TRANSFORM_MODE_90      3
42 #define TRANSFORM_MODE_180     4
43 #define TRANSFORM_MODE_270     5
44
45 /*****************************************************************************
46  * Local prototypes
47  *****************************************************************************/
48 static int  Create    ( vlc_object_t * );
49 static void Destroy   ( vlc_object_t * );
50
51 static int  Init      ( vout_thread_t * );
52 static void End       ( vout_thread_t * );
53 static void Render    ( vout_thread_t *, picture_t * );
54
55 static void FilterPlanar( vout_thread_t *, const picture_t *, picture_t * );
56 static void FilterI422( vout_thread_t *, const picture_t *, picture_t * );
57 static void FilterYUYV( vout_thread_t *, const picture_t *, picture_t * );
58
59 static int  MouseEvent( vlc_object_t *, char const *,
60                         vlc_value_t, vlc_value_t, void * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 #define TYPE_TEXT N_("Transform type")
66 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
67
68 static const char *const type_list[] = { "90", "180", "270", "hflip", "vflip" };
69 static const char *const type_list_text[] = { N_("Rotate by 90 degrees"),
70   N_("Rotate by 180 degrees"), N_("Rotate by 270 degrees"),
71   N_("Flip horizontally"), N_("Flip vertically") };
72
73 #define TRANSFORM_HELP N_("Rotate or flip the video")
74 #define CFG_PREFIX "transform-"
75
76 vlc_module_begin ()
77     set_description( N_("Video transformation filter") )
78     set_shortname( N_("Transformation"))
79     set_help(TRANSFORM_HELP)
80     set_capability( "video filter", 0 )
81     set_category( CAT_VIDEO )
82     set_subcategory( SUBCAT_VIDEO_VFILTER )
83
84     add_string( CFG_PREFIX "type", "90", NULL,
85                           TYPE_TEXT, TYPE_LONGTEXT, false)
86         change_string_list( type_list, type_list_text, 0)
87
88     add_shortcut( "transform" )
89     set_callbacks( Create, Destroy )
90 vlc_module_end ()
91
92 static const char *const ppsz_filter_options[] = {
93     "type", NULL
94 };
95
96 /*****************************************************************************
97  * vout_sys_t: Transform video output method descriptor
98  *****************************************************************************
99  * This structure is part of the video output thread descriptor.
100  * It describes the Transform specific properties of an output thread.
101  *****************************************************************************/
102 struct vout_sys_t
103 {
104     int i_mode;
105     bool b_rotation;
106     vout_thread_t *p_vout;
107
108     void (*pf_filter)( vout_thread_t *, const picture_t *, picture_t * );
109 };
110
111 /*****************************************************************************
112  * Control: control facility for the vout (forwards to child vout)
113  *****************************************************************************/
114 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
115 {
116     return vout_vaControl( p_vout->p_sys->p_vout, i_query, args );
117 }
118
119 /*****************************************************************************
120  * Create: allocates Transform video thread output method
121  *****************************************************************************
122  * This function allocates and initializes a Transform vout method.
123  *****************************************************************************/
124 static int Create( vlc_object_t *p_this )
125 {
126     vout_thread_t *p_vout = (vout_thread_t *)p_this;
127     char *psz_method;
128
129     /* Allocate structure */
130     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
131     if( p_vout->p_sys == NULL )
132         return VLC_ENOMEM;
133
134     p_vout->pf_init = Init;
135     p_vout->pf_end = End;
136     p_vout->pf_manage = NULL;
137     p_vout->pf_render = Render;
138     p_vout->pf_display = NULL;
139     p_vout->pf_control = Control;
140
141     config_ChainParse( p_vout, CFG_PREFIX, ppsz_filter_options,
142                            p_vout->p_cfg );
143
144     /* Look what method was requested */
145     psz_method = var_CreateGetNonEmptyStringCommand( p_vout, "transform-type" );
146
147     switch( p_vout->fmt_in.i_chroma )
148     {
149         CASE_PLANAR_YUV_SQUARE
150         case VLC_CODEC_GREY:
151             p_vout->p_sys->pf_filter = FilterPlanar;
152             break;
153
154         case VLC_CODEC_I422:
155         case VLC_CODEC_J422:
156             p_vout->p_sys->pf_filter = FilterI422;
157             break;
158
159         CASE_PACKED_YUV_422
160             p_vout->p_sys->pf_filter = FilterYUYV;
161             break;
162
163         default:
164             msg_Err( p_vout, "Unsupported chroma" );
165             free( p_vout->p_sys );
166             return VLC_EGENERIC;
167     }
168
169     if( psz_method == NULL )
170     {
171         msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
172         msg_Err( p_vout, "no valid transform mode provided, using '90'" );
173         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
174         p_vout->p_sys->b_rotation = 1;
175     }
176     else
177     {
178         if( !strcmp( psz_method, "hflip" ) )
179         {
180             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
181             p_vout->p_sys->b_rotation = 0;
182         }
183         else if( !strcmp( psz_method, "vflip" ) )
184         {
185             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
186             p_vout->p_sys->b_rotation = 0;
187         }
188         else if( !strcmp( psz_method, "90" ) )
189         {
190             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
191             p_vout->p_sys->b_rotation = 1;
192         }
193         else if( !strcmp( psz_method, "180" ) )
194         {
195             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
196             p_vout->p_sys->b_rotation = 0;
197         }
198         else if( !strcmp( psz_method, "270" ) )
199         {
200             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
201             p_vout->p_sys->b_rotation = 1;
202         }
203         else
204         {
205             msg_Err( p_vout, "no valid transform mode provided, using '90'" );
206             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
207             p_vout->p_sys->b_rotation = 1;
208         }
209
210         free( psz_method );
211     }
212
213     return VLC_SUCCESS;
214 }
215
216 /*****************************************************************************
217  * Init: initialize Transform video thread output method
218  *****************************************************************************/
219 static int Init( vout_thread_t *p_vout )
220 {
221     video_format_t fmt;
222
223     I_OUTPUTPICTURES = 0;
224     memset( &fmt, 0, sizeof(video_format_t) );
225
226     /* Initialize the output structure */
227     p_vout->output.i_chroma = p_vout->render.i_chroma;
228     p_vout->output.i_width  = p_vout->render.i_width;
229     p_vout->output.i_height = p_vout->render.i_height;
230     p_vout->output.i_aspect = p_vout->render.i_aspect;
231     p_vout->fmt_out = p_vout->fmt_in;
232     fmt = p_vout->fmt_out;
233
234     /* Try to open the real video output */
235     msg_Dbg( p_vout, "spawning the real video output" );
236
237     if( p_vout->p_sys->b_rotation )
238     {
239         fmt.i_width = p_vout->fmt_out.i_height;
240         fmt.i_visible_width = p_vout->fmt_out.i_visible_height;
241         fmt.i_x_offset = p_vout->fmt_out.i_y_offset;
242
243         fmt.i_height = p_vout->fmt_out.i_width;
244         fmt.i_visible_height = p_vout->fmt_out.i_visible_width;
245         fmt.i_y_offset = p_vout->fmt_out.i_x_offset;
246
247         fmt.i_sar_num = p_vout->fmt_out.i_sar_den;
248         fmt.i_sar_den = p_vout->fmt_out.i_sar_num;
249     }
250
251     p_vout->p_sys->p_vout = vout_Create( p_vout, &fmt );
252
253     /* Everything failed */
254     if( p_vout->p_sys->p_vout == NULL )
255     {
256         msg_Err( p_vout, "cannot open vout, aborting" );
257         return VLC_EGENERIC;
258     }
259
260     vout_filter_AllocateDirectBuffers( p_vout, VOUT_MAX_PICTURES );
261
262     vout_filter_AddChild( p_vout, p_vout->p_sys->p_vout, MouseEvent );
263
264     return VLC_SUCCESS;
265 }
266
267 /*****************************************************************************
268  * End: terminate Transform video thread output method
269  *****************************************************************************/
270 static void End( vout_thread_t *p_vout )
271 {
272     vout_sys_t *p_sys = p_vout->p_sys;
273
274     vout_filter_DelChild( p_vout, p_sys->p_vout, MouseEvent );
275     vout_CloseAndRelease( p_sys->p_vout );
276
277     vout_filter_ReleaseDirectBuffers( p_vout );
278 }
279
280 /*****************************************************************************
281  * Destroy: destroy Transform video thread output method
282  *****************************************************************************
283  * Terminate an output method created by TransformCreateOutputMethod
284  *****************************************************************************/
285 static void Destroy( vlc_object_t *p_this )
286 {
287     vout_thread_t *p_vout = (vout_thread_t *)p_this;
288
289     free( p_vout->p_sys );
290 }
291
292 /*****************************************************************************
293  * Render: displays previously rendered output
294  *****************************************************************************
295  * This function send the currently rendered image to Transform image, waits
296  * until it is displayed and switch the two rendering buffers, preparing next
297  * frame.
298  *****************************************************************************/
299 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
300 {
301     picture_t *p_outpic;
302
303     /* This is a new frame. Get a structure from the video_output. */
304     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
305               == NULL )
306     {
307         if( !vlc_object_alive (p_vout) || p_vout->b_error )
308         {
309             return;
310         }
311         msleep( VOUT_OUTMEM_SLEEP );
312     }
313
314     p_outpic->date = p_pic->date;
315     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
316
317     p_vout->p_sys->pf_filter( p_vout, p_pic, p_outpic );
318
319     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
320
321     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
322 }
323
324 /**
325  * Forward mouse event with proper conversion.
326  */
327 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
328                        vlc_value_t oldval, vlc_value_t val, void *p_data )
329 {
330     vout_thread_t *p_vout = p_data;
331     VLC_UNUSED(p_this); VLC_UNUSED(oldval);
332
333     /* Translate the mouse coordinates
334      * FIXME missing lock */
335     if( !strcmp( psz_var, "mouse-button-down" ) )
336         return var_SetChecked( p_vout, psz_var, VLC_VAR_INTEGER, val );
337
338     int x = val.coords.x, y = val.coords.y;
339
340     switch( p_vout->p_sys->i_mode )
341     {
342         case TRANSFORM_MODE_90:
343             x = p_vout->p_sys->p_vout->output.i_height - val.coords.y;
344             y = val.coords.x;
345             break;
346
347         case TRANSFORM_MODE_180:
348             x = p_vout->p_sys->p_vout->output.i_width - val.coords.x;
349             y = p_vout->p_sys->p_vout->output.i_height - val.coords.y;
350             break;
351
352         case TRANSFORM_MODE_270:
353             x = val.coords.y;
354             y = p_vout->p_sys->p_vout->output.i_width - val.coords.x;
355             break;
356
357         case TRANSFORM_MODE_HFLIP:
358             x = p_vout->p_sys->p_vout->output.i_width - val.coords.x;
359             break;
360
361         case TRANSFORM_MODE_VFLIP:
362             y = p_vout->p_sys->p_vout->output.i_height - val.coords.y;
363             break;
364
365         default:
366             break;
367     }
368     return var_SetCoords( p_vout, psz_var, x, y );
369 }
370
371 static void FilterPlanar( vout_thread_t *p_vout,
372                           const picture_t *p_pic, picture_t *p_outpic )
373 {
374     int i_index;
375     switch( p_vout->p_sys->i_mode )
376     {
377         case TRANSFORM_MODE_90:
378             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
379             {
380                 int i_pitch = p_pic->p[i_index].i_pitch;
381
382                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
383
384                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
385                 uint8_t *p_out_end = p_out +
386                     p_outpic->p[i_index].i_visible_lines *
387                     p_outpic->p[i_index].i_pitch;
388
389                 for( ; p_out < p_out_end ; )
390                 {
391                     uint8_t *p_line_end;
392
393                     p_out_end -= p_outpic->p[i_index].i_pitch
394                                   - p_outpic->p[i_index].i_visible_pitch;
395                     p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
396                         i_pitch;
397
398                     for( ; p_in < p_line_end ; )
399                     {
400                         p_line_end -= i_pitch;
401                         *(--p_out_end) = *p_line_end;
402                     }
403
404                     p_in++;
405                 }
406             }
407             break;
408
409         case TRANSFORM_MODE_180:
410             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
411             {
412                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
413                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
414                                             * p_pic->p[i_index].i_pitch;
415
416                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
417
418                 for( ; p_in < p_in_end ; )
419                 {
420                     uint8_t *p_line_start = p_in_end
421                                              - p_pic->p[i_index].i_pitch;
422                     p_in_end -= p_pic->p[i_index].i_pitch
423                                  - p_pic->p[i_index].i_visible_pitch;
424
425                     for( ; p_line_start < p_in_end ; )
426                     {
427                         *p_out++ = *(--p_in_end);
428                     }
429
430                     p_out += p_outpic->p[i_index].i_pitch
431                               - p_outpic->p[i_index].i_visible_pitch;
432                 }
433             }
434             break;
435
436         case TRANSFORM_MODE_270:
437             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
438             {
439                 int i_pitch = p_pic->p[i_index].i_pitch;
440
441                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
442
443                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
444                 uint8_t *p_out_end = p_out +
445                     p_outpic->p[i_index].i_visible_lines *
446                     p_outpic->p[i_index].i_pitch;
447
448                 for( ; p_out < p_out_end ; )
449                 {
450                     uint8_t *p_in_end;
451
452                     p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
453                         i_pitch;
454
455                     for( ; p_in < p_in_end ; )
456                     {
457                         p_in_end -= i_pitch;
458                         *p_out++ = *p_in_end;
459                     }
460
461                     p_out += p_outpic->p[i_index].i_pitch
462                               - p_outpic->p[i_index].i_visible_pitch;
463                     p_in++;
464                 }
465             }
466             break;
467
468         case TRANSFORM_MODE_HFLIP:
469             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
470             {
471                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
472                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
473                                             * p_pic->p[i_index].i_pitch;
474
475                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
476
477                 for( ; p_in < p_in_end ; )
478                 {
479                     p_in_end -= p_pic->p[i_index].i_pitch;
480                     vlc_memcpy( p_out, p_in_end,
481                                 p_pic->p[i_index].i_visible_pitch );
482                     p_out += p_outpic->p[i_index].i_pitch;
483                 }
484             }
485             break;
486
487         case TRANSFORM_MODE_VFLIP:
488             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
489             {
490                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
491                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
492                                          * p_pic->p[i_index].i_pitch;
493
494                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
495
496                 for( ; p_in < p_in_end ; )
497                 {
498                     uint8_t *p_line_end = p_in
499                                         + p_pic->p[i_index].i_visible_pitch;
500
501                     for( ; p_in < p_line_end ; )
502                     {
503                         *p_out++ = *(--p_line_end);
504                     }
505
506                     p_in += p_pic->p[i_index].i_pitch;
507                     p_out += p_outpic->p[i_index].i_pitch
508                                 - p_outpic->p[i_index].i_visible_pitch;
509                 }
510             }
511             break;
512
513         default:
514             break;
515     }
516 }
517
518 static void FilterI422( vout_thread_t *p_vout,
519                         const picture_t *p_pic, picture_t *p_outpic )
520 {
521     int i_index;
522     switch( p_vout->p_sys->i_mode )
523     {
524         case TRANSFORM_MODE_180:
525         case TRANSFORM_MODE_HFLIP:
526         case TRANSFORM_MODE_VFLIP:
527             /* Fall back on the default implementation */
528             FilterPlanar( p_vout, p_pic, p_outpic );
529             return;
530
531         case TRANSFORM_MODE_90:
532             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
533             {
534                 int i_pitch = p_pic->p[i_index].i_pitch;
535
536                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
537
538                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
539                 uint8_t *p_out_end = p_out +
540                     p_outpic->p[i_index].i_visible_lines *
541                     p_outpic->p[i_index].i_pitch;
542
543                 if( i_index == 0 )
544                 {
545                     for( ; p_out < p_out_end ; )
546                     {
547                         uint8_t *p_line_end;
548
549                         p_out_end -= p_outpic->p[i_index].i_pitch
550                                       - p_outpic->p[i_index].i_visible_pitch;
551                         p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
552                             i_pitch;
553
554                         for( ; p_in < p_line_end ; )
555                         {
556                             p_line_end -= i_pitch;
557                             *(--p_out_end) = *p_line_end;
558                         }
559
560                         p_in++;
561                     }
562                 }
563                 else /* i_index == 1 or 2 */
564                 {
565                     for( ; p_out < p_out_end ; )
566                     {
567                         uint8_t *p_line_end, *p_out_end2;
568
569                         p_out_end -= p_outpic->p[i_index].i_pitch
570                                       - p_outpic->p[i_index].i_visible_pitch;
571                         p_out_end2 = p_out_end - p_outpic->p[i_index].i_pitch;
572                         p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
573                             i_pitch;
574
575                         for( ; p_in < p_line_end ; )
576                         {
577                             uint8_t p1, p2;
578
579                             p_line_end -= i_pitch;
580                             p1 = *p_line_end;
581                             p_line_end -= i_pitch;
582                             p2 = *p_line_end;
583
584                             /* Trick for (x+y)/2 without overflow, based on
585                              *   x + y == (x ^ y) + 2 * (x & y) */
586                             *(--p_out_end) = (p1 & p2) + ((p1 ^ p2) / 2);
587                             *(--p_out_end2) = (p1 & p2) + ((p1 ^ p2) / 2);
588                         }
589
590                         p_out_end = p_out_end2;
591                         p_in++;
592                     }
593                 }
594             }
595             break;
596
597         case TRANSFORM_MODE_270:
598             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
599             {
600                 int i_pitch = p_pic->p[i_index].i_pitch;
601
602                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
603
604                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
605                 uint8_t *p_out_end = p_out +
606                     p_outpic->p[i_index].i_visible_lines *
607                     p_outpic->p[i_index].i_pitch;
608
609                 if( i_index == 0 )
610                 {
611                     for( ; p_out < p_out_end ; )
612                     {
613                         uint8_t *p_in_end;
614
615                         p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
616                             i_pitch;
617
618                         for( ; p_in < p_in_end ; )
619                         {
620                             p_in_end -= i_pitch;
621                             *p_out++ = *p_in_end;
622                         }
623
624                         p_out += p_outpic->p[i_index].i_pitch
625                                   - p_outpic->p[i_index].i_visible_pitch;
626                         p_in++;
627                     }
628                 }
629                 else /* i_index == 1 or 2 */
630                 {
631                     for( ; p_out < p_out_end ; )
632                     {
633                         uint8_t *p_in_end, *p_out2;
634
635                         p_in_end = p_in + p_pic->p[i_index].i_visible_lines *
636                             i_pitch;
637                         p_out2 = p_out + p_outpic->p[i_index].i_pitch;
638
639                         for( ; p_in < p_in_end ; )
640                         {
641                             uint8_t p1, p2;
642
643                             p_in_end -= i_pitch;
644                             p1 = *p_in_end;
645                             p_in_end -= i_pitch;
646                             p2 = *p_in_end;
647
648                             /* Trick for (x+y)/2 without overflow, based on
649                              *   x + y == (x ^ y) + 2 * (x & y) */
650                             *p_out++ = (p1 & p2) + ((p1 ^ p2) / 2);
651                             *p_out2++ = (p1 & p2) + ((p1 ^ p2) / 2);
652                         }
653
654                         p_out2 += p_outpic->p[i_index].i_pitch
655                                    - p_outpic->p[i_index].i_visible_pitch;
656                         p_out = p_out2;
657                         p_in++;
658                     }
659                 }
660             }
661             break;
662
663         default:
664             break;
665     }
666 }
667
668 static void FilterYUYV( vout_thread_t *p_vout,
669                         const picture_t *p_pic, picture_t *p_outpic )
670 {
671     int i_index;
672     int i_y_offset, i_u_offset, i_v_offset;
673     if( GetPackedYuvOffsets( p_pic->format.i_chroma, &i_y_offset,
674                              &i_u_offset, &i_v_offset ) != VLC_SUCCESS )
675         return;
676
677     switch( p_vout->p_sys->i_mode )
678     {
679         case TRANSFORM_MODE_HFLIP:
680             /* Fall back on the default implementation */
681             FilterPlanar( p_vout, p_pic, p_outpic );
682             return;
683
684         case TRANSFORM_MODE_90:
685             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
686             {
687                 int i_pitch = p_pic->p[i_index].i_pitch;
688
689                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
690
691                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
692                 uint8_t *p_out_end = p_out +
693                     p_outpic->p[i_index].i_visible_lines *
694                     p_outpic->p[i_index].i_pitch;
695
696                 int i_offset  = i_u_offset;
697                 int i_offset2 = i_v_offset;
698                 for( ; p_out < p_out_end ; )
699                 {
700                     uint8_t *p_line_end;
701
702                     p_out_end -= p_outpic->p[i_index].i_pitch
703                                   - p_outpic->p[i_index].i_visible_pitch;
704                     p_line_end = p_in + p_pic->p[i_index].i_visible_lines *
705                         i_pitch;
706
707                     for( ; p_in < p_line_end ; )
708                     {
709                         p_line_end -= i_pitch;
710                         p_out_end -= 4;
711                         p_out_end[i_y_offset+2] = p_line_end[i_y_offset];
712                         p_out_end[i_u_offset] = p_line_end[i_offset];
713                         p_line_end -= i_pitch;
714                         p_out_end[i_y_offset] = p_line_end[i_y_offset];
715                         p_out_end[i_v_offset] = p_line_end[i_offset2];
716                     }
717
718                     p_in += 2;
719
720                     {
721                         int a = i_offset;
722                         i_offset = i_offset2;
723                         i_offset2 = a;
724                     }
725                 }
726             }
727             break;
728
729         case TRANSFORM_MODE_180:
730             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
731             {
732                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
733                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
734                                             * p_pic->p[i_index].i_pitch;
735
736                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
737
738                 for( ; p_in < p_in_end ; )
739                 {
740                     uint8_t *p_line_start = p_in_end
741                                              - p_pic->p[i_index].i_pitch;
742                     p_in_end -= p_pic->p[i_index].i_pitch
743                                  - p_pic->p[i_index].i_visible_pitch;
744
745                     for( ; p_line_start < p_in_end ; )
746                     {
747                         p_in_end -= 4;
748                         p_out[i_y_offset] = p_in_end[i_y_offset+2];
749                         p_out[i_u_offset] = p_in_end[i_u_offset];
750                         p_out[i_y_offset+2] = p_in_end[i_y_offset];
751                         p_out[i_v_offset] = p_in_end[i_v_offset];
752                         p_out += 4;
753                     }
754
755                     p_out += p_outpic->p[i_index].i_pitch
756                               - p_outpic->p[i_index].i_visible_pitch;
757                 }
758             }
759             break;
760
761         case TRANSFORM_MODE_270:
762             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
763             {
764                 int i_pitch = p_pic->p[i_index].i_pitch;
765
766                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
767
768                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
769                 uint8_t *p_out_end = p_out +
770                     p_outpic->p[i_index].i_visible_lines *
771                     p_outpic->p[i_index].i_pitch;
772
773                 int i_offset  = i_u_offset;
774                 int i_offset2 = i_v_offset;
775                 for( ; p_out < p_out_end ; )
776                 {
777                     uint8_t *p_in_end;
778
779                     p_in_end = p_in
780                              + p_pic->p[i_index].i_visible_lines * i_pitch;
781
782                     for( ; p_in < p_in_end ; )
783                     {
784                         p_in_end -= i_pitch;
785                         p_out[i_y_offset] = p_in_end[i_y_offset];
786                         p_out[i_u_offset] = p_in_end[i_offset];
787                         p_in_end -= i_pitch;
788                         p_out[i_y_offset+2] = p_in_end[i_y_offset];
789                         p_out[i_v_offset] = p_in_end[i_offset2];
790                         p_out += 4;
791                     }
792
793                     p_out += p_outpic->p[i_index].i_pitch
794                            - p_outpic->p[i_index].i_visible_pitch;
795                     p_in += 2;
796
797                     {
798                         int a = i_offset;
799                         i_offset = i_offset2;
800                         i_offset2 = a;
801                     }
802                 }
803             }
804             break;
805
806         case TRANSFORM_MODE_VFLIP:
807             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
808             {
809                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
810                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_visible_lines
811                                          * p_pic->p[i_index].i_pitch;
812
813                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
814
815                 for( ; p_in < p_in_end ; )
816                 {
817                     uint8_t *p_line_end = p_in
818                                         + p_pic->p[i_index].i_visible_pitch;
819
820                     for( ; p_in < p_line_end ; )
821                     {
822                         p_line_end -= 4;
823                         p_out[i_y_offset] = p_line_end[i_y_offset+2];
824                         p_out[i_u_offset] = p_line_end[i_u_offset];
825                         p_out[i_y_offset+2] = p_line_end[i_y_offset];
826                         p_out[i_v_offset] = p_line_end[i_v_offset];
827                         p_out += 4;
828                     }
829
830                     p_in += p_pic->p[i_index].i_pitch;
831                     p_out += p_outpic->p[i_index].i_pitch
832                                 - p_outpic->p[i_index].i_visible_pitch;
833                 }
834             }
835             break;
836
837         default:
838             break;
839     }
840 }