]> git.sesse.net Git - vlc/blob - modules/video_filter/transform.c
acdc7aee2ae21893819a734496cfa93e303f5405
[vlc] / modules / video_filter / transform.c
1 /*****************************************************************************
2  * transform.c : transform image module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id: transform.c,v 1.17 2004/01/25 03:28:41 hartman Exp $
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "filter_common.h"
34
35 #define TRANSFORM_MODE_HFLIP   1
36 #define TRANSFORM_MODE_VFLIP   2
37 #define TRANSFORM_MODE_90      3
38 #define TRANSFORM_MODE_180     4
39 #define TRANSFORM_MODE_270     5
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Create    ( vlc_object_t * );
45 static void Destroy   ( vlc_object_t * );
46
47 static int  Init      ( vout_thread_t * );
48 static void End       ( vout_thread_t * );
49 static void Render    ( vout_thread_t *, picture_t * );
50
51 static int  SendEvents( vlc_object_t *, char const *,
52                         vlc_value_t, vlc_value_t, void * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 #define TYPE_TEXT N_("Transform type")
58 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
59
60 static char *type_list[] = { "90", "180", "270", "hflip", "vflip" };
61 static char *type_list_text[] = { N_("Rotate by 90 degrees"),
62   N_("Rotate by 180 degrees"), N_("Rotate by 270 degrees"),
63   N_("Flip horizontally"), N_("Flip vertically") };
64
65 vlc_module_begin();
66     add_category_hint( N_("Miscellaneous"), NULL, VLC_FALSE );
67     add_string( "transform-type", "90", NULL,
68                           TYPE_TEXT, TYPE_LONGTEXT, VLC_FALSE);
69         change_string_list( type_list, type_list_text, 0);
70     set_description( _("Video transformation filter") );
71     set_capability( "video filter", 0 );
72     add_shortcut( "transform" );
73     set_callbacks( Create, Destroy );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * vout_sys_t: Transform video output method descriptor
78  *****************************************************************************
79  * This structure is part of the video output thread descriptor.
80  * It describes the Transform specific properties of an output thread.
81  *****************************************************************************/
82 struct vout_sys_t
83 {
84     int i_mode;
85     vlc_bool_t b_rotation;
86     vout_thread_t *p_vout;
87 };
88
89 /*****************************************************************************
90  * Create: allocates Transform video thread output method
91  *****************************************************************************
92  * This function allocates and initializes a Transform vout method.
93  *****************************************************************************/
94 static int Create( vlc_object_t *p_this )
95 {
96     vout_thread_t *p_vout = (vout_thread_t *)p_this;
97     char *psz_method;
98
99     /* Allocate structure */
100     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
101     if( p_vout->p_sys == NULL )
102     {
103         msg_Err( p_vout, "out of memory" );
104         return VLC_ENOMEM;
105     }
106
107     p_vout->pf_init = Init;
108     p_vout->pf_end = End;
109     p_vout->pf_manage = NULL;
110     p_vout->pf_render = Render;
111     p_vout->pf_display = NULL;
112
113     /* Look what method was requested */
114     psz_method = config_GetPsz( p_vout, "transform-type" );
115
116     if( psz_method == NULL )
117     {
118         msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
119         msg_Err( p_vout, "no valid transform mode provided, using '90'" );
120         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
121         p_vout->p_sys->b_rotation = 1;
122     }
123     else
124     {
125         if( !strcmp( psz_method, "hflip" ) )
126         {
127             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
128             p_vout->p_sys->b_rotation = 0;
129         }
130         else if( !strcmp( psz_method, "vflip" ) )
131         {
132             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
133             p_vout->p_sys->b_rotation = 0;
134         }
135         else if( !strcmp( psz_method, "90" ) )
136         {
137             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
138             p_vout->p_sys->b_rotation = 1;
139         }
140         else if( !strcmp( psz_method, "180" ) )
141         {
142             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
143             p_vout->p_sys->b_rotation = 0;
144         }
145         else if( !strcmp( psz_method, "270" ) )
146         {
147             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
148             p_vout->p_sys->b_rotation = 1;
149         }
150         else
151         {
152             msg_Err( p_vout, "no valid transform mode provided, using '90'" );
153             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
154             p_vout->p_sys->b_rotation = 1;
155         }
156
157         free( psz_method );
158     }
159
160     return VLC_SUCCESS;
161 }
162
163 /*****************************************************************************
164  * Init: initialize Transform video thread output method
165  *****************************************************************************/
166 static int Init( vout_thread_t *p_vout )
167 {
168     int i_index;
169     picture_t *p_pic;
170
171     I_OUTPUTPICTURES = 0;
172
173     /* Initialize the output structure */
174     p_vout->output.i_chroma = p_vout->render.i_chroma;
175     p_vout->output.i_width  = p_vout->render.i_width;
176     p_vout->output.i_height = p_vout->render.i_height;
177     p_vout->output.i_aspect = p_vout->render.i_aspect;
178
179     /* Try to open the real video output */
180     msg_Dbg( p_vout, "spawning the real video output" );
181
182     if( p_vout->p_sys->b_rotation )
183     {
184         p_vout->p_sys->p_vout = vout_Create( p_vout,
185                            p_vout->render.i_height, p_vout->render.i_width,
186                            p_vout->render.i_chroma,
187                            (uint64_t)VOUT_ASPECT_FACTOR
188                             * (uint64_t)VOUT_ASPECT_FACTOR
189                             / (uint64_t)p_vout->render.i_aspect );
190     }
191     else
192     {
193         p_vout->p_sys->p_vout = vout_Create( p_vout,
194                            p_vout->render.i_width, p_vout->render.i_height,
195                            p_vout->render.i_chroma, p_vout->render.i_aspect );
196     }
197
198     /* Everything failed */
199     if( p_vout->p_sys->p_vout == NULL )
200     {
201         msg_Err( p_vout, "cannot open vout, aborting" );
202         return VLC_EGENERIC;
203     }
204
205     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
206
207     ADD_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
208
209     ADD_PARENT_CALLBACKS( SendEventsToChild );
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * End: terminate Transform video thread output method
216  *****************************************************************************/
217 static void End( vout_thread_t *p_vout )
218 {
219     int i_index;
220
221     /* Free the fake output buffers we allocated */
222     for( i_index = I_OUTPUTPICTURES ; i_index ; )
223     {
224         i_index--;
225         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
226     }
227 }
228
229 /*****************************************************************************
230  * Destroy: destroy Transform video thread output method
231  *****************************************************************************
232  * Terminate an output method created by TransformCreateOutputMethod
233  *****************************************************************************/
234 static void Destroy( vlc_object_t *p_this )
235 {
236     vout_thread_t *p_vout = (vout_thread_t *)p_this;
237
238     DEL_CALLBACKS( p_vout->p_sys->p_vout, SendEvents );
239     vlc_object_detach( p_vout->p_sys->p_vout );
240     vout_Destroy( p_vout->p_sys->p_vout );
241
242     DEL_PARENT_CALLBACKS( SendEventsToChild );
243
244     free( p_vout->p_sys );
245 }
246
247 /*****************************************************************************
248  * Render: displays previously rendered output
249  *****************************************************************************
250  * This function send the currently rendered image to Transform image, waits
251  * until it is displayed and switch the two rendering buffers, preparing next
252  * frame.
253  *****************************************************************************/
254 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
255 {
256     picture_t *p_outpic;
257     int i_index;
258
259     /* This is a new frame. Get a structure from the video_output. */
260     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
261               == NULL )
262     {
263         if( p_vout->b_die || p_vout->b_error )
264         {
265             return;
266         }
267         msleep( VOUT_OUTMEM_SLEEP );
268     }
269
270     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
271     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
272
273     switch( p_vout->p_sys->i_mode )
274     {
275         case TRANSFORM_MODE_90:
276             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
277             {
278                 int i_pitch = p_pic->p[i_index].i_pitch;
279
280                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
281
282                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
283                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
284                                               * p_outpic->p[i_index].i_pitch;
285
286                 for( ; p_out < p_out_end ; )
287                 {
288                     uint8_t *p_line_end;
289
290                     p_out_end -= p_outpic->p[i_index].i_pitch
291                                   - p_outpic->p[i_index].i_visible_pitch;
292                     p_line_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
293
294                     for( ; p_in < p_line_end ; )
295                     {
296                         p_line_end -= i_pitch;
297                         *(--p_out_end) = *p_line_end;
298                     }
299
300                     p_in++;
301                 }
302             }
303             break;
304
305         case TRANSFORM_MODE_180:
306             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
307             {
308                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
309                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
310                                             * p_pic->p[i_index].i_pitch;
311
312                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
313
314                 for( ; p_in < p_in_end ; )
315                 {
316                     uint8_t *p_line_start = p_in_end
317                                              - p_pic->p[i_index].i_pitch;
318                     p_in_end -= p_pic->p[i_index].i_pitch
319                                  - p_pic->p[i_index].i_visible_pitch;
320
321                     for( ; p_line_start < p_in_end ; )
322                     {
323                         *p_out++ = *(--p_in_end);
324                     }
325
326                     p_out += p_outpic->p[i_index].i_pitch
327                               - p_outpic->p[i_index].i_visible_pitch;
328                 }
329             }
330             break;
331
332         case TRANSFORM_MODE_270:
333             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
334             {
335                 int i_pitch = p_pic->p[i_index].i_pitch;
336
337                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
338
339                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
340                 uint8_t *p_out_end = p_out + p_outpic->p[i_index].i_lines
341                                               * p_outpic->p[i_index].i_pitch;
342
343                 for( ; p_out < p_out_end ; )
344                 {
345                     uint8_t *p_in_end;
346
347                     p_in_end = p_in + p_pic->p[i_index].i_lines * i_pitch;
348
349                     for( ; p_in < p_in_end ; )
350                     {
351                         p_in_end -= i_pitch;
352                         *p_out++ = *p_in_end;
353                     }
354
355                     p_out += p_outpic->p[i_index].i_pitch
356                               - p_outpic->p[i_index].i_visible_pitch;
357                     p_in++;
358                 }
359             }
360             break;
361
362         case TRANSFORM_MODE_HFLIP:
363             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
364             {
365                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
366                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
367                                             * p_pic->p[i_index].i_pitch;
368
369                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
370
371                 for( ; p_in < p_in_end ; )
372                 {
373                     p_in_end -= p_pic->p[i_index].i_pitch;
374                     p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
375                                            p_pic->p[i_index].i_visible_pitch );
376                     p_out += p_pic->p[i_index].i_pitch;
377                 }
378             }
379             break;
380
381         case TRANSFORM_MODE_VFLIP:
382             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
383             {
384                 uint8_t *p_in = p_pic->p[i_index].p_pixels;
385                 uint8_t *p_in_end = p_in + p_pic->p[i_index].i_lines
386                                             * p_pic->p[i_index].i_pitch;
387
388                 uint8_t *p_out = p_outpic->p[i_index].p_pixels;
389
390                 for( ; p_in < p_in_end ; )
391                 {
392                     uint8_t *p_line_end = p_in
393                                            + p_pic->p[i_index].i_visible_pitch;
394
395                     for( ; p_in < p_line_end ; )
396                     {
397                         *p_out++ = *(--p_line_end);
398                     }
399
400                     p_in += p_pic->p[i_index].i_pitch;
401                 }
402             }
403             break;
404
405         default:
406             break;
407     }
408
409     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
410
411     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
412 }
413
414 /*****************************************************************************
415  * SendEvents: forward mouse and keyboard events to the parent p_vout
416  *****************************************************************************/
417 static int SendEvents( vlc_object_t *p_this, char const *psz_var,
418                        vlc_value_t oldval, vlc_value_t newval, void *_p_vout )
419 {
420     vout_thread_t *p_vout = (vout_thread_t *)_p_vout;
421     vlc_value_t sentval = newval;
422
423     /* Translate the mouse coordinates */
424     if( !strcmp( psz_var, "mouse-x" ) )
425     {
426         switch( p_vout->p_sys->i_mode )
427         {
428         case TRANSFORM_MODE_270:
429             sentval.i_int = p_vout->p_sys->p_vout->output.i_width
430                              - sentval.i_int;
431         case TRANSFORM_MODE_90:
432             var_Set( p_vout, "mouse-y", sentval );
433             return VLC_SUCCESS;
434
435         case TRANSFORM_MODE_180:
436         case TRANSFORM_MODE_HFLIP:
437             sentval.i_int = p_vout->p_sys->p_vout->output.i_width
438                              - sentval.i_int;
439             break;
440
441         case TRANSFORM_MODE_VFLIP:
442         default:
443             break;
444         }
445     }
446     else if( !strcmp( psz_var, "mouse-y" ) )
447     {
448         switch( p_vout->p_sys->i_mode )
449         {
450         case TRANSFORM_MODE_90:
451             sentval.i_int = p_vout->p_sys->p_vout->output.i_height
452                              - sentval.i_int;
453         case TRANSFORM_MODE_270:
454             var_Set( p_vout, "mouse-x", sentval );
455             return VLC_SUCCESS;
456
457         case TRANSFORM_MODE_180:
458         case TRANSFORM_MODE_VFLIP:
459             sentval.i_int = p_vout->p_sys->p_vout->output.i_height
460                              - sentval.i_int;
461             break;
462
463         case TRANSFORM_MODE_HFLIP:
464         default:
465             break;
466         }
467     }
468
469     var_Set( p_vout, psz_var, sentval );
470
471     return VLC_SUCCESS;
472 }
473
474 /*****************************************************************************
475  * SendEventsToChild: forward events to the child/children vout
476  *****************************************************************************/
477 static int SendEventsToChild( vlc_object_t *p_this, char const *psz_var,
478                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
479 {
480     vout_thread_t *p_vout = (vout_thread_t *)p_this;
481     var_Set( p_vout->p_sys->p_vout, psz_var, newval );
482     return VLC_SUCCESS;
483 }