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