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