]> git.sesse.net Git - vlc/blob - plugins/filter/transform.c
* ./ChangeLog: imported the 0.2.92 changes, unrolled current CVS changes.
[vlc] / plugins / 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.3 2002/01/02 14:37:42 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 <errno.h>
28 #include <stdlib.h>                                      /* malloc(), free() */
29 #include <string.h>
30
31 #include <videolan/vlc.h>
32
33 #include "video.h"
34 #include "video_output.h"
35
36 #include "filter_common.h"
37
38 #define TRANSFORM_MODE_HFLIP   1
39 #define TRANSFORM_MODE_VFLIP   2
40 #define TRANSFORM_MODE_90      3
41 #define TRANSFORM_MODE_180     4
42 #define TRANSFORM_MODE_270     5
43
44 /*****************************************************************************
45  * Capabilities defined in the other files.
46  *****************************************************************************/
47 static void vout_getfunctions( function_list_t * p_function_list );
48
49 /*****************************************************************************
50  * Build configuration tree.
51  *****************************************************************************/
52 MODULE_CONFIG_START
53 MODULE_CONFIG_STOP
54
55 MODULE_INIT_START
56     SET_DESCRIPTION( "image transformation module" )
57     /* Capability score set to 0 because we don't want to be spawned
58      * as a video output unless explicitly requested to */
59     ADD_CAPABILITY( VOUT, 0 )
60     ADD_SHORTCUT( "transform" )
61 MODULE_INIT_STOP
62
63 MODULE_ACTIVATE_START
64     vout_getfunctions( &p_module->p_functions->vout );
65 MODULE_ACTIVATE_STOP
66
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
69
70 /*****************************************************************************
71  * vout_sys_t: Transform video output method descriptor
72  *****************************************************************************
73  * This structure is part of the video output thread descriptor.
74  * It describes the Transform specific properties of an output thread.
75  *****************************************************************************/
76 typedef struct vout_sys_s
77 {
78     int i_mode;
79     boolean_t b_rotation;
80     struct vout_thread_s *p_vout;
81
82 } vout_sys_t;
83
84 /*****************************************************************************
85  * Local prototypes
86  *****************************************************************************/
87 static int  vout_Probe     ( probedata_t *p_data );
88 static int  vout_Create    ( struct vout_thread_s * );
89 static int  vout_Init      ( struct vout_thread_s * );
90 static void vout_End       ( struct vout_thread_s * );
91 static void vout_Destroy   ( struct vout_thread_s * );
92 static int  vout_Manage    ( struct vout_thread_s * );
93 static void vout_Display   ( struct vout_thread_s *, struct picture_s * );
94
95 /*****************************************************************************
96  * Functions exported as capabilities. They are declared as static so that
97  * we don't pollute the namespace too much.
98  *****************************************************************************/
99 static void vout_getfunctions( function_list_t * p_function_list )
100 {
101     p_function_list->pf_probe = vout_Probe;
102     p_function_list->functions.vout.pf_create     = vout_Create;
103     p_function_list->functions.vout.pf_init       = vout_Init;
104     p_function_list->functions.vout.pf_end        = vout_End;
105     p_function_list->functions.vout.pf_destroy    = vout_Destroy;
106     p_function_list->functions.vout.pf_manage     = vout_Manage;
107     p_function_list->functions.vout.pf_display    = vout_Display;
108     p_function_list->functions.vout.pf_setpalette = NULL;
109 }
110
111 /*****************************************************************************
112  * intf_Probe: return a score
113  *****************************************************************************/
114 static int vout_Probe( probedata_t *p_data )
115 {
116     return( 0 );
117 }
118
119 /*****************************************************************************
120  * vout_Create: allocates Transform video thread output method
121  *****************************************************************************
122  * This function allocates and initializes a Transform vout method.
123  *****************************************************************************/
124 static int vout_Create( vout_thread_t *p_vout )
125 {
126     char *psz_method;
127
128     /* Allocate structure */
129     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
130     if( p_vout->p_sys == NULL )
131     {
132         intf_ErrMsg( "error: %s", strerror(ENOMEM) );
133         return( 1 );
134     }
135
136     /* Look what method was requested */
137     psz_method = main_GetPszVariable( VOUT_FILTER_VAR, "" );
138
139     while( *psz_method && *psz_method != ':' )
140     {
141         psz_method++;
142     }
143
144     if( !strcmp( psz_method, ":hflip" ) )
145     {
146         p_vout->p_sys->i_mode = TRANSFORM_MODE_HFLIP;
147         p_vout->p_sys->b_rotation = 0;
148     }
149     else if( !strcmp( psz_method, ":vflip" ) )
150     {
151         p_vout->p_sys->i_mode = TRANSFORM_MODE_VFLIP;
152         p_vout->p_sys->b_rotation = 0;
153     }
154     else if( !strcmp( psz_method, ":90" ) )
155     {
156         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
157         p_vout->p_sys->b_rotation = 1;
158     }
159     else if( !strcmp( psz_method, ":180" ) )
160     {
161         p_vout->p_sys->i_mode = TRANSFORM_MODE_180;
162         p_vout->p_sys->b_rotation = 0;
163     }
164     else if( !strcmp( psz_method, ":270" ) )
165     {
166         p_vout->p_sys->i_mode = TRANSFORM_MODE_270;
167         p_vout->p_sys->b_rotation = 1;
168     }
169     else
170     {
171         intf_ErrMsg( "filter error: no valid transform mode provided, "
172                      "using transform:90" );
173         p_vout->p_sys->i_mode = TRANSFORM_MODE_90;
174         p_vout->p_sys->b_rotation = 1;
175     }
176
177     return( 0 );
178 }
179
180 /*****************************************************************************
181  * vout_Init: initialize Transform video thread output method
182  *****************************************************************************/
183 static int vout_Init( vout_thread_t *p_vout )
184 {
185     int i_index;
186     char *psz_filter;
187     picture_t *p_pic;
188     
189     I_OUTPUTPICTURES = 0;
190
191     /* Initialize the output structure */
192     switch( p_vout->render.i_chroma )
193     {
194         case FOURCC_I420:
195         case FOURCC_IYUV:
196         case FOURCC_YV12:
197         case FOURCC_I422:
198         case FOURCC_I444:
199             p_vout->output.i_chroma = p_vout->render.i_chroma;
200             p_vout->output.i_width  = p_vout->render.i_width;
201             p_vout->output.i_height = p_vout->render.i_height;
202             p_vout->output.i_aspect = p_vout->render.i_aspect;
203             break;
204
205         default:
206             return( 0 ); /* unknown chroma */
207             break;
208     }
209
210     /* Try to open the real video output */
211     psz_filter = main_GetPszVariable( VOUT_FILTER_VAR, "" );
212     main_PutPszVariable( VOUT_FILTER_VAR, "" );
213
214     intf_WarnMsg( 1, "filter: spawning the real video output" );
215
216     if( p_vout->p_sys->b_rotation )
217     {
218         p_vout->p_sys->p_vout =
219             vout_CreateThread( NULL,
220                            p_vout->render.i_height, p_vout->render.i_width,
221                            p_vout->render.i_chroma,
222                            (u64)VOUT_ASPECT_FACTOR * (u64)VOUT_ASPECT_FACTOR
223                                / (u64)p_vout->render.i_aspect );
224     }
225     else
226     {
227         p_vout->p_sys->p_vout =
228             vout_CreateThread( NULL,
229                            p_vout->render.i_width, p_vout->render.i_height,
230                            p_vout->render.i_chroma, p_vout->render.i_aspect );
231     }
232
233     /* Everything failed */
234     if( p_vout->p_sys->p_vout == NULL )
235     {
236         intf_ErrMsg( "filter error: can't open vout, aborting" );
237         return( 0 );
238     }
239  
240     main_PutPszVariable( VOUT_FILTER_VAR, psz_filter );
241
242     ALLOCATE_DIRECTBUFFERS( VOUT_MAX_PICTURES );
243
244     return( 0 );
245 }
246
247 /*****************************************************************************
248  * vout_End: terminate Transform video thread output method
249  *****************************************************************************/
250 static void vout_End( vout_thread_t *p_vout )
251 {
252     int i_index;
253
254     /* Free the fake output buffers we allocated */
255     for( i_index = I_OUTPUTPICTURES ; i_index ; )
256     {
257         i_index--;
258         free( PP_OUTPUTPICTURE[ i_index ]->planes[ 0 ].p_data );
259     }
260 }
261
262 /*****************************************************************************
263  * vout_Destroy: destroy Transform video thread output method
264  *****************************************************************************
265  * Terminate an output method created by TransformCreateOutputMethod
266  *****************************************************************************/
267 static void vout_Destroy( vout_thread_t *p_vout )
268 {
269     vout_DestroyThread( p_vout->p_sys->p_vout, NULL );
270
271     free( p_vout->p_sys );
272 }
273
274 /*****************************************************************************
275  * vout_Manage: handle Transform events
276  *****************************************************************************
277  * This function should be called regularly by video output thread. It manages
278  * console events. It returns a non null value on error.
279  *****************************************************************************/
280 static int vout_Manage( vout_thread_t *p_vout )
281 {
282     return( 0 );
283 }
284
285 /*****************************************************************************
286  * vout_Display: displays previously rendered output
287  *****************************************************************************
288  * This function send the currently rendered image to Transform image, waits
289  * until it is displayed and switch the two rendering buffers, preparing next
290  * frame.
291  *****************************************************************************/
292 static void vout_Display( vout_thread_t *p_vout, picture_t *p_pic )
293 {
294     picture_t *p_outpic;
295     int i_index;
296
297     /* This is a new frame. Get a structure from the video_output. */
298     while( ( p_outpic = vout_CreatePicture( p_vout->p_sys->p_vout, 0, 0, 0 ) )
299               == NULL )
300     {
301         if( p_vout->b_die || p_vout->b_error )
302         {
303             return;
304         }
305         msleep( VOUT_OUTMEM_SLEEP );
306     }   
307
308     vout_DatePicture( p_vout->p_sys->p_vout, p_outpic, mdate() + 50000 );
309     vout_LinkPicture( p_vout->p_sys->p_vout, p_outpic );
310
311     switch( p_vout->p_sys->i_mode )
312     {
313         case TRANSFORM_MODE_90:
314             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
315             {
316                 int i_line_bytes = p_pic->planes[ i_index ].i_line_bytes;
317
318                 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
319
320                 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
321                 pixel_data_t *p_out_end = p_out
322                                        + p_outpic->planes[ i_index ].i_bytes;
323
324                 for( ; p_out < p_out_end ; )
325                 {
326                     pixel_data_t *p_line_end;
327
328                     p_line_end = p_in + p_pic->planes[ i_index ].i_bytes;
329
330                     for( ; p_in < p_line_end ; )
331                     {
332                         p_line_end -= i_line_bytes;
333                         *(--p_out_end) = *p_line_end;
334                     }
335
336                     p_in++;
337                 }
338             }
339             break;
340
341         case TRANSFORM_MODE_180:
342             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
343             {
344                 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
345                 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
346
347                 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
348
349                 for( ; p_in < p_in_end ; )
350                 {
351                     *p_out++ = *(--p_in_end);
352                 }
353             }
354             break;
355
356         case TRANSFORM_MODE_270:
357             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
358             {
359                 int i_line_bytes = p_pic->planes[ i_index ].i_line_bytes;
360
361                 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
362
363                 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
364                 pixel_data_t *p_out_end = p_out
365                                        + p_outpic->planes[ i_index ].i_bytes;
366
367                 for( ; p_out < p_out_end ; )
368                 {
369                     pixel_data_t *p_in_end;
370
371                     p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
372
373                     for( ; p_in < p_in_end ; )
374                     {
375                         p_in_end -= i_line_bytes;
376                         *p_out++ = *p_in_end;
377                     }
378
379                     p_in++;
380                 }
381             }
382             break;
383
384         case TRANSFORM_MODE_VFLIP:
385             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
386             {
387                 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
388                 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
389
390                 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
391
392                 for( ; p_in < p_in_end ; )
393                 {
394                     p_in_end -= p_pic->planes[ i_index ].i_line_bytes;
395                     FAST_MEMCPY( p_out, p_in_end,
396                                  p_pic->planes[ i_index ].i_line_bytes );
397                     p_out += p_pic->planes[ i_index ].i_line_bytes;
398                 }
399             }
400             break;
401
402         case TRANSFORM_MODE_HFLIP:
403             for( i_index = 0 ; i_index < p_pic->i_planes ; i_index++ )
404             {
405                 pixel_data_t *p_in = p_pic->planes[ i_index ].p_data;
406                 pixel_data_t *p_in_end = p_in + p_pic->planes[ i_index ].i_bytes;
407
408                 pixel_data_t *p_out = p_outpic->planes[ i_index ].p_data;
409
410                 for( ; p_in < p_in_end ; )
411                 {
412                     pixel_data_t *p_line_end = p_in
413                             + p_pic->planes[ i_index ].i_line_bytes;
414
415                     for( ; p_in < p_line_end ; )
416                     {
417                         *p_out++ = *(--p_line_end);
418                     }
419
420                     p_in += p_pic->planes[ i_index ].i_line_bytes;
421                 }
422             }
423             break;
424
425         default:
426             break;
427     }
428
429     vout_UnlinkPicture( p_vout->p_sys->p_vout, p_outpic );
430
431     vout_DisplayPicture( p_vout->p_sys->p_vout, p_outpic );
432 }
433