]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
Include vlc_plugin.h as needed
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * vout_aa.c: Aa video output display method for testing purposes
3  *****************************************************************************
4  * Copyright (C) 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Sigmund Augdal Helberg <dnumgis@videolan.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28
29 #include <aalib.h>
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <vlc_plugin.h>
37 #include <vlc_vout.h>
38 #include <vlc_interface.h>
39
40 /*****************************************************************************
41  * Local prototypes
42  *****************************************************************************/
43 static int  Create    ( vlc_object_t * );
44 static void Destroy   ( vlc_object_t * );
45
46 static int  Init      ( vout_thread_t * );
47 static void End       ( vout_thread_t * );
48 static int  Manage    ( vout_thread_t * );
49 static void Render    ( vout_thread_t *, picture_t * );
50 static void Display   ( vout_thread_t *, picture_t * );
51
52 static void SetPalette     ( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
53
54 /*****************************************************************************
55  * Module descriptor
56  *****************************************************************************/
57 vlc_module_begin();
58     set_shortname( _("ASCII Art"));
59     set_category( CAT_VIDEO );
60     set_subcategory( SUBCAT_VIDEO_VOUT );
61     set_description( _("ASCII-art video output") );
62     set_capability( "video output", 10 );
63     add_shortcut( "aalib" );
64     set_callbacks( Create, Destroy );
65 vlc_module_end();
66
67 /*****************************************************************************
68  * vout_sys_t: aa video output method descriptor
69  *****************************************************************************
70  * This structure is part of the video output thread descriptor.
71  * It describes the aa specific properties of an output thread.
72  *****************************************************************************/
73 struct vout_sys_t
74 {
75     struct aa_context*  aa_context;
76     aa_palette          palette;
77     int                 i_width;                     /* width of main window */
78     int                 i_height;                   /* height of main window */
79 };
80
81 /*****************************************************************************
82  * Create: allocates aa video thread output method
83  *****************************************************************************
84  * This function allocates and initializes a aa vout method.
85  *****************************************************************************/
86 static int Create( vlc_object_t *p_this )
87 {
88     vout_thread_t *p_vout = (vout_thread_t *)p_this;
89
90     /* Allocate structure */
91     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
92     if( p_vout->p_sys == NULL )
93     {
94         msg_Err( p_vout, "out of memory" );
95         return( 1 );
96     }
97
98     /* Don't parse any options, but take $AAOPTS into account */
99     aa_parseoptions( NULL, NULL, NULL, NULL );
100
101     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
102     {
103         msg_Err( p_vout, "cannot initialize aalib" );
104         return( 1 );
105     }
106
107     p_vout->pf_init = Init;
108     p_vout->pf_end = End;
109     p_vout->pf_manage = Manage;
110     p_vout->pf_render = Render;
111     p_vout->pf_display = Display;
112
113     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
114     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
115     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
116     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
117     aa_hidemouse( p_vout->p_sys->aa_context );
118     return( 0 );
119 }
120
121 /*****************************************************************************
122  * Init: initialize aa video thread output method
123  *****************************************************************************/
124 static int Init( vout_thread_t *p_vout )
125 {
126     int i_index;
127     picture_t *p_pic = NULL;
128
129     I_OUTPUTPICTURES = 0;
130
131     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
132     p_vout->output.i_width = p_vout->p_sys->i_width;
133     p_vout->output.i_height = p_vout->p_sys->i_height;
134     p_vout->output.i_aspect = p_vout->p_sys->i_width
135                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
136     p_vout->output.pf_setpalette = SetPalette;
137
138     /* Find an empty picture slot */
139     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
140     {
141         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
142         {
143             p_pic = p_vout->p_picture + i_index;
144             break;
145         }
146     }
147
148     if( p_pic == NULL )
149     {
150         return -1;
151     }
152
153     /* Allocate the picture */
154     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
155     p_pic->p->i_lines = p_vout->p_sys->i_height;
156     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
157     p_pic->p->i_pitch = p_vout->p_sys->i_width;
158     p_pic->p->i_pixel_pitch = 1;
159     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
160     p_pic->i_planes = 1;
161
162     p_pic->i_status = DESTROYED_PICTURE;
163     p_pic->i_type   = DIRECT_PICTURE;
164
165     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
166     I_OUTPUTPICTURES++;
167
168     return 0;
169 }
170
171 /*****************************************************************************
172  * End: terminate aa video thread output method
173  *****************************************************************************/
174 static void End( vout_thread_t *p_vout )
175 {
176     ;
177 }
178
179 /*****************************************************************************
180  * Destroy: destroy aa video thread output method
181  *****************************************************************************
182  * Terminate an output method created by AaCreateOutputMethod
183  *****************************************************************************/
184 static void Destroy( vlc_object_t *p_this )
185 {
186     vout_thread_t *p_vout = (vout_thread_t *)p_this;
187
188     aa_close( p_vout->p_sys->aa_context );
189     free( p_vout->p_sys );
190 }
191
192 /*****************************************************************************
193  * Manage: handle aa events
194  *****************************************************************************
195  * This function should be called regularly by video output thread. It manages
196  * console events. It returns a non null value on error.
197  *****************************************************************************/
198 static int Manage( vout_thread_t *p_vout )
199 {
200     int event, x, y, b;
201     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
202     switch ( event )
203     {
204     case AA_MOUSE:
205         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
206         if ( b & AA_BUTTON3 )
207         {
208             intf_thread_t *p_intf;
209             p_intf = vlc_object_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
210             if( p_intf )
211             {
212                 p_intf->b_menu_change = 1;
213                 vlc_object_release( p_intf );
214             }
215         }
216         break;
217     case AA_RESIZE:
218         p_vout->i_changes |= VOUT_SIZE_CHANGE;
219         aa_resize( p_vout->p_sys->aa_context );
220         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
221         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
222         break;
223     default:
224         break;
225     }
226     return( 0 );
227 }
228
229 /*****************************************************************************
230  * Render: render previously calculated output
231  *****************************************************************************/
232 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
233 {
234   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
235                  aa_imgwidth( p_vout->p_sys->aa_context ),
236                  aa_imgheight( p_vout->p_sys->aa_context ) );
237 }
238
239 /*****************************************************************************
240  * Display: displays previously rendered output
241  *****************************************************************************/
242 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
243 {
244     /* No need to do anything, the fake direct buffers stay as they are */
245     int i_width, i_height, i_x, i_y;
246
247     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
248                        &i_x, &i_y, &i_width, &i_height );
249
250     aa_flush(p_vout->p_sys->aa_context);
251 }
252
253 /*****************************************************************************
254  * SetPalette: set the 8bpp palette
255  *****************************************************************************/
256 static void SetPalette( vout_thread_t *p_vout,
257                         uint16_t *red, uint16_t *green, uint16_t *blue )
258 {
259     int i;
260
261     /* Fill colors with color information */
262     for( i = 0; i < 256; i++ )
263     {
264         aa_setpalette( p_vout->p_sys->palette, 256 -i,
265                        red[ i ], green[ i ], blue[ i ] );
266     }
267 }
268