]> git.sesse.net Git - vlc/blob - modules/video_output/aa.c
Merge branch '1.0'
[vlc] / modules / video_output / aa.c
1 /*****************************************************************************
2  * vout_aa.c: Aa video output display method for testing purposes
3  *****************************************************************************
4  * Copyright (C) 2002-2009 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_common.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( N_("ASCII Art"))
59     set_category( CAT_VIDEO )
60     set_subcategory( SUBCAT_VIDEO_VOUT )
61     set_description( N_("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         return VLC_ENOMEM;
94
95     /* Don't parse any options, but take $AAOPTS into account */
96     aa_parseoptions( NULL, NULL, NULL, NULL );
97
98     if (!(p_vout->p_sys->aa_context = aa_autoinit(&aa_defparams)))
99     {
100         msg_Err( p_vout, "cannot initialize aalib" );
101         free( p_vout->p_sys );
102         return VLC_EGENERIC;
103     }
104
105     p_vout->pf_init = Init;
106     p_vout->pf_end = End;
107     p_vout->pf_manage = Manage;
108     p_vout->pf_render = Render;
109     p_vout->pf_display = Display;
110
111     p_vout->p_sys->i_width = aa_imgwidth(p_vout->p_sys->aa_context);
112     p_vout->p_sys->i_height = aa_imgheight(p_vout->p_sys->aa_context);
113     aa_autoinitkbd( p_vout->p_sys->aa_context, 0 );
114     aa_autoinitmouse( p_vout->p_sys->aa_context, AA_MOUSEPRESSMASK );
115     aa_hidemouse( p_vout->p_sys->aa_context );
116
117     return VLC_SUCCESS;
118 }
119
120 /*****************************************************************************
121  * Init: initialize aa video thread output method
122  *****************************************************************************/
123 static int Init( vout_thread_t *p_vout )
124 {
125     int i_index;
126     picture_t *p_pic = NULL;
127
128     I_OUTPUTPICTURES = 0;
129
130     p_vout->output.i_chroma = VLC_CODEC_RGB8;
131     p_vout->output.i_width = p_vout->p_sys->i_width;
132     p_vout->output.i_height = p_vout->p_sys->i_height;
133     p_vout->output.i_aspect = p_vout->p_sys->i_width
134                                * VOUT_ASPECT_FACTOR / p_vout->p_sys->i_height;
135     p_vout->output.pf_setpalette = SetPalette;
136
137     /* Find an empty picture slot */
138     for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
139     {
140         if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
141         {
142             p_pic = p_vout->p_picture + i_index;
143             break;
144         }
145     }
146
147     if( p_pic == NULL )
148         return VLC_EGENERIC;
149
150     /* Allocate the picture */
151     p_pic->p->p_pixels = aa_image( p_vout->p_sys->aa_context );
152     p_pic->p->i_lines = p_vout->p_sys->i_height;
153     p_pic->p->i_visible_lines = p_vout->p_sys->i_height;
154     p_pic->p->i_pitch = p_vout->p_sys->i_width;
155     p_pic->p->i_pixel_pitch = 1;
156     p_pic->p->i_visible_pitch = p_vout->p_sys->i_width;
157     p_pic->i_planes = 1;
158
159     p_pic->i_status = DESTROYED_PICTURE;
160     p_pic->i_type   = DIRECT_PICTURE;
161
162     PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
163     I_OUTPUTPICTURES++;
164
165     return VLC_SUCCESS;
166 }
167
168 /*****************************************************************************
169  * End: terminate aa video thread output method
170  *****************************************************************************/
171 static void End( vout_thread_t *p_vout )
172 {
173     ;
174 }
175
176 /*****************************************************************************
177  * Destroy: destroy aa video thread output method
178  *****************************************************************************
179  * Terminate an output method created by AaCreateOutputMethod
180  *****************************************************************************/
181 static void Destroy( vlc_object_t *p_this )
182 {
183     vout_thread_t *p_vout = (vout_thread_t *)p_this;
184
185     aa_close( p_vout->p_sys->aa_context );
186     free( p_vout->p_sys );
187 }
188
189 /*****************************************************************************
190  * Manage: handle aa events
191  *****************************************************************************
192  * This function should be called regularly by video output thread. It manages
193  * console events. It returns a non null value on error.
194  *****************************************************************************/
195 static int Manage( vout_thread_t *p_vout )
196 {
197     int event, x, y, b;
198     event = aa_getevent( p_vout->p_sys->aa_context, 0 );
199     switch ( event )
200     {
201     case AA_MOUSE:
202         aa_getmouse( p_vout->p_sys->aa_context, &x, &y, &b );
203         if ( b & AA_BUTTON3 )
204             var_SetBool( p_vout->p_libvlc, "intf-popupmenu", true );
205         break;
206     case AA_RESIZE:
207         p_vout->i_changes |= VOUT_SIZE_CHANGE;
208         aa_resize( p_vout->p_sys->aa_context );
209         p_vout->p_sys->i_width = aa_imgwidth( p_vout->p_sys->aa_context );
210         p_vout->p_sys->i_height = aa_imgheight( p_vout->p_sys->aa_context );
211         break;
212     default:
213         break;
214     }
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219  * Render: render previously calculated output
220  *****************************************************************************/
221 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
222 {
223   aa_fastrender( p_vout->p_sys->aa_context, 0, 0,
224                  aa_imgwidth( p_vout->p_sys->aa_context ),
225                  aa_imgheight( p_vout->p_sys->aa_context ) );
226 }
227
228 /*****************************************************************************
229  * Display: displays previously rendered output
230  *****************************************************************************/
231 static void Display( vout_thread_t *p_vout, picture_t *p_pic )
232 {
233     /* No need to do anything, the fake direct buffers stay as they are */
234     unsigned int i_width, i_height, i_x, i_y;
235
236     vout_PlacePicture( p_vout, p_vout->p_sys->i_width, p_vout->p_sys->i_height,
237                        &i_x, &i_y, &i_width, &i_height );
238
239     aa_flush(p_vout->p_sys->aa_context);
240 }
241
242 /*****************************************************************************
243  * SetPalette: set the 8bpp palette
244  *****************************************************************************/
245 static void SetPalette( vout_thread_t *p_vout,
246                         uint16_t *red, uint16_t *green, uint16_t *blue )
247 {
248     int i;
249
250     /* Fill colors with color information */
251     for( i = 0; i < 256; i++ )
252     {
253         aa_setpalette( p_vout->p_sys->palette, 256 -i,
254                        red[ i ], green[ i ], blue[ i ] );
255     }
256 }
257