]> git.sesse.net Git - vlc/blob - modules/video_filter/transform.c
* ./src/video_output/video_output.c, modules/*: factorized video output
[vlc] / modules / video_filter / transform.c
1 /*****************************************************************************
2  * transform.c : transform image plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id: transform.c,v 1.4 2002/11/28 17:35:00 sam 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 /*****************************************************************************
52  * Module descriptor
53  *****************************************************************************/
54 #define TYPE_TEXT N_("transform type")
55 #define TYPE_LONGTEXT N_("One of '90', '180', '270', 'hflip' and 'vflip'")
56
57 static char *type_list[] = { "90", "180", "270", "hflip", "vflip", NULL };
58
59 vlc_module_begin();
60     add_category_hint( N_("Miscellaneous"), NULL );
61     add_string_from_list( "transform-type", "90", type_list, NULL,
62                           TYPE_TEXT, TYPE_LONGTEXT);
63     set_description( _("image transformation module") );
64     set_capability( "video filter", 0 );
65     add_shortcut( "transform" );
66     set_callbacks( Create, Destroy );
67 vlc_module_end();
68
69 /*****************************************************************************
70  * vout_sys_t: Transform video output method descriptor
71  *****************************************************************************
72  * This structure is part of the video output thread descriptor.
73  * It describes the Transform specific properties of an output thread.
74  *****************************************************************************/
75 struct vout_sys_t
76 {
77     int i_mode;
78     vlc_bool_t b_rotation;
79     vout_thread_t *p_vout;
80 };
81
82 /*****************************************************************************
83  * Create: allocates Transform video thread output method
84  *****************************************************************************
85  * This function allocates and initializes a Transform vout method.
86  *****************************************************************************/
87 static int Create( vlc_object_t *p_this )
88 {
89     vout_thread_t *p_vout = (vout_thread_t *)p_this;
90     char *psz_method;
91
92     /* Allocate structure */
93     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94     if( p_vout->p_sys == NULL )
95     {
96         msg_Err( p_vout, "out of memory" );
97         return( 1 );
98     }
99
100     p_vout->pf_init = Init;
101     p_vout->pf_end = End;
102     p_vout->pf_manage = NULL;
103     p_vout->pf_render = Render;
104     p_vout->pf_display = NULL;
105
106     /* Look what method was requested */
107     psz_method = config_GetPsz( p_vout, "transform-type" );
108
109     if( psz_method == NULL )
110     {
111         msg_Err( p_vout, "configuration variable %s empty", "transform-type" );
112         msg_Err( p_vout, "no valid transform mode provided, using '90'" );
113         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
114         p_vout->p_sys->b_rotation = 1;
115     }
116     else
117     {
118         if( !strcmp( psz_method, "hflip" ) )
119         {
120             p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
121             p_vout->p_sys->b_rotation = 0;
122         }
123         else if( !strcmp( psz_method, "vflip" ) )
124         {
125             p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
126             p_vout->p_sys->b_rotation = 0;
127         }
128         else if( !strcmp( psz_method, "90" ) )
129         {
130             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
131             p_vout->p_sys->b_rotation = 1;
132         }
133         else if( !strcmp( psz_method, "180" ) )
134         {
135             p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
136             p_vout->p_sys->b_rotation = 0;
137         }
138         else if( !strcmp( psz_method, "270" ) )
139         {
140             p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
141             p_vout->p_sys->b_rotation = 1;
142         }
143         else
144         {
145             msg_Err( p_vout, "no valid transform mode provided, using '90'" );
146             p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
147             p_vout->p_sys->b_rotation = 1;
148         }
149
150         free( psz_method );
151     }
152     
153     return( 0 );
154 }
155
156 /*****************************************************************************
157  * Init: initialize Transform video thread output method
158  *****************************************************************************/
159 static int Init( vout_thread_t *p_vout )
160 {
161     int i_index;
162     picture_t *p_pic;
163     
164     I_OUTPUTPICTURES = 0;
165
166     /* Initialize the output structure */
167     p_vout->output.i_chroma = p_vout->render.i_chroma;
168     p_vout->output.i_width  = p_vout->render.i_width;
169     p_vout->output.i_height = p_vout->render.i_height;
170     p_vout->output.i_aspect = p_vout->render.i_aspect;
171
172     /* Try to open the real video output */
173     msg_Dbg( p_vout, "spawning the real video output" );
174
175     if( p_vout->p_sys->b_rotation )
176     {
177         p_vout->p_sys->p_vout = vout_Create( p_vout,
178                            p_vout->render.i_height, p_vout->render.i_width,
179                            p_vout->render.i_chroma,
180                            (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
181                                / (u64)p_vout->render.i_aspect );
182     }
183     else
184     {
185         p_vout->p_sys->p_vout = vout_Create( p_vout,
186                            p_vout->render.i_width, p_vout->render.i_height,
187                            p_vout->render.i_chroma, p_vout->render.i_aspect );
188     }
189
190     /* Everything failed */
191     if( p_vout->p_sys->p_vout == NULL )
192     {
193         msg_Err( p_vout, "cannot open vout, aborting" );
194         return( 0 );
195     }
196  
197     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
198
199     return( 0 );
200 }
201
202 /*****************************************************************************
203  * End: terminate Transform video thread output method
204  *****************************************************************************/
205 static void End( vout_thread_t *p_vout )
206 {
207     int i_index;
208
209     /* Free the fake output buffers we allocated */
210     for( i_index = I_OUTPUTPICTURES ; i_index ; )
211     {
212         i_index--;
213         free( PP_OUTPUTPICTURE[ i_index ]->p_data_orig );
214     }
215 }
216
217 /*****************************************************************************
218  * Destroy: destroy Transform video thread output method
219  *****************************************************************************
220  * Terminate an output method created by TransformCreateOutputMethod
221  *****************************************************************************/
222 static void Destroy( vlc_object_t *p_this )
223 {
224     vout_thread_t *p_vout = (vout_thread_t *)p_this;
225
226     vout_Destroy( p_vout->p_sys->p_vout );
227
228     free( p_vout->p_sys );
229 }
230
231 /*****************************************************************************
232  * Render: displays previously rendered output
233  *****************************************************************************
234  * This function send the currently rendered image to Transform image, waits
235  * until it is displayed and switch the two rendering buffers, preparing next
236  * frame.
237  *****************************************************************************/
238 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
239 {
240     picture_t *p_outpic;
241     int i_index;
242
243     /* This is a new frame. Get a structure from the video_output. */
244     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
245               == NULL )
246     {
247         if( p_vout->b_die || p_vout->b_error )
248         {
249             return;
250         }
251         msleep( VOUT_OUTMEM_SLEEP );
252     }   
253
254     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, p_pic->date );
255     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
256
257     switch( p_vout->p_sys->i_mode )
258     {
259         case TRANSFORM_MODE_90:
260             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
261             {
262                 int i_pitch = p_pic->p[i_index].i_pitch;
263
264                 u8 *p_in = p_pic->p[i_index].p_pixels;
265
266                 u8 *p_out = p_outpic->p[i_index].p_pixels;
267                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
268                                          * p_outpic->p[i_index].i_pitch;
269
270                 for( ; p_out < p_out_end ; )
271                 {
272                     u8 *p_line_end;
273
274                     p_line_end = p_in + p_pic->p[i_index].i_lines
275                                          * p_pic->p[i_index].i_pitch;
276
277                     for( ; p_in < p_line_end ; )
278                     {
279                         p_line_end -= i_pitch;
280                         *(--p_out_end) = *p_line_end;
281                     }
282
283                     p_in++;
284                 }
285             }
286             break;
287
288         case TRANSFORM_MODE_180:
289             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
290             {
291                 u8 *p_in = p_pic->p[i_index].p_pixels;
292                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
293                                        * p_pic->p[i_index].i_pitch;
294
295                 u8 *p_out = p_outpic->p[i_index].p_pixels;
296
297                 for( ; p_in < p_in_end ; )
298                 {
299                     *p_out++ = *(--p_in_end);
300                 }
301             }
302             break;
303
304         case TRANSFORM_MODE_270:
305             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
306             {
307                 int i_pitch = p_pic->p[i_index].i_pitch;
308
309                 u8 *p_in = p_pic->p[i_index].p_pixels;
310
311                 u8 *p_out = p_outpic->p[i_index].p_pixels;
312                 u8 *p_out_end = p_out + p_outpic->p[i_index].i_lines
313                                          * p_outpic->p[i_index].i_pitch;
314
315                 for( ; p_out < p_out_end ; )
316                 {
317                     u8 *p_in_end;
318
319                     p_in_end = p_in + p_pic->p[i_index].i_lines
320                                        * p_pic->p[i_index].i_pitch;
321
322                     for( ; p_in < p_in_end ; )
323                     {
324                         p_in_end -= i_pitch;
325                         *p_out++ = *p_in_end;
326                     }
327
328                     p_in++;
329                 }
330             }
331             break;
332
333         case TRANSFORM_MODE_VFLIP:
334             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
335             {
336                 u8 *p_in = p_pic->p[i_index].p_pixels;
337                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
338                                        * p_pic->p[i_index].i_pitch;
339
340                 u8 *p_out = p_outpic->p[i_index].p_pixels;
341
342                 for( ; p_in < p_in_end ; )
343                 {
344                     p_in_end -= p_pic->p[i_index].i_pitch;
345                     p_vout->p_vlc->pf_memcpy( p_out, p_in_end,
346                                               p_pic->p[i_index].i_pitch );
347                     p_out += p_pic->p[i_index].i_pitch;
348                 }
349             }
350             break;
351
352         case TRANSFORM_MODE_HFLIP:
353             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
354             {
355                 u8 *p_in = p_pic->p[i_index].p_pixels;
356                 u8 *p_in_end = p_in + p_pic->p[i_index].i_lines
357                                        * p_pic->p[i_index].i_pitch;
358
359                 u8 *p_out = p_outpic->p[i_index].p_pixels;
360
361                 for( ; p_in < p_in_end ; )
362                 {
363                     u8 *p_line_end = p_in + p_pic->p[i_index].i_pitch;
364
365                     for( ; p_in < p_line_end ; )
366                     {
367                         *p_out++ = *(--p_line_end);
368                     }
369
370                     p_in += p_pic->p[i_index].i_pitch;
371                 }
372             }
373             break;
374
375         default:
376             break;
377     }
378
379     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
380
381     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
382 }
383