1 /*****************************************************************************
2 * caca.c: Color ASCII Art video output plugin using libcaca
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: caca.c,v 1.3 2003/11/22 16:18:05 sam Exp $
7 * Authors: Sam Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <stdlib.h> /* free() */
29 #include <string.h> /* strerror() */
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Create ( vlc_object_t * );
42 static void Destroy ( vlc_object_t * );
44 static int Init ( vout_thread_t * );
45 static void End ( vout_thread_t * );
46 static int Manage ( vout_thread_t * );
47 static void Render ( vout_thread_t *, picture_t * );
48 static void Display ( vout_thread_t *, picture_t * );
50 /*****************************************************************************
52 *****************************************************************************/
53 #define MODE_TEXT N_("dithering mode")
54 #define MODE_LONGTEXT N_("Choose the libcaca dithering mode")
56 static char *mode_list[] = { "none", "ordered", "random" };
57 static char *mode_list_text[] = { N_("No dithering"), N_("Ordered dithering"),
58 N_("Random dithering") };
61 add_category_hint( N_("Dithering"), NULL, VLC_FALSE );
62 add_string( "caca-dithering", "ordered", NULL, MODE_TEXT,
63 MODE_LONGTEXT, VLC_FALSE );
64 change_string_list( mode_list, mode_list_text, 0 );
65 set_description( _("colour ASCII art video output") );
66 set_capability( "video output", 12 );
67 set_callbacks( Create, Destroy );
70 /*****************************************************************************
71 * vout_sys_t: libcaca video output method descriptor
72 *****************************************************************************
73 * This structure is part of the video output thread descriptor.
74 * It describes the libcaca specific properties of an output thread.
75 *****************************************************************************/
78 struct caca_bitmap *p_bitmap;
81 /*****************************************************************************
82 * Create: allocates libcaca video output thread
83 *****************************************************************************
84 * This function initializes libcaca vout method.
85 *****************************************************************************/
86 static int Create( vlc_object_t *p_this )
88 vout_thread_t *p_vout = (vout_thread_t *)p_this;
89 enum caca_dithering dither = CACA_DITHER_ORDERED;
92 /* Allocate structure */
93 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94 if( p_vout->p_sys == NULL )
96 msg_Err( p_vout, "out of memory" );
102 msg_Err( p_vout, "cannot initialize libcaca" );
103 free( p_vout->p_sys );
107 var_Create( p_vout, "caca-dithering", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
108 var_Get( p_vout, "caca-dithering", &val );
111 if( !strcmp( val.psz_string, "none" ) )
113 dither = CACA_DITHER_NONE;
115 else if( !strcmp( val.psz_string, "random" ) )
117 dither = CACA_DITHER_RANDOM;
119 free( val.psz_string );
121 caca_set_dithering( dither );
123 p_vout->pf_init = Init;
124 p_vout->pf_end = End;
125 p_vout->pf_manage = Manage;
126 p_vout->pf_render = Render;
127 p_vout->pf_display = Display;
132 /*****************************************************************************
133 * Init: initialize libcaca video output thread
134 *****************************************************************************/
135 static int Init( vout_thread_t *p_vout )
138 picture_t *p_pic = NULL;
140 I_OUTPUTPICTURES = 0;
142 p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
143 p_vout->output.i_width = p_vout->render.i_width;
144 p_vout->output.i_height = p_vout->render.i_height;
145 p_vout->output.i_aspect = p_vout->render.i_aspect;
147 p_vout->output.i_rmask = 0x00ff0000;
148 p_vout->output.i_gmask = 0x0000ff00;
149 p_vout->output.i_bmask = 0x000000ff;
151 /* Create the libcaca bitmap */
152 p_vout->p_sys->p_bitmap =
153 caca_create_bitmap( 32,
154 p_vout->output.i_width,
155 p_vout->output.i_height,
156 4 * ((p_vout->output.i_width + 15) & ~15),
157 p_vout->output.i_rmask,
158 p_vout->output.i_gmask,
159 p_vout->output.i_bmask );
160 if( !p_vout->p_sys->p_bitmap )
162 msg_Err( p_vout, "could not create libcaca bitmap" );
166 /* Find an empty picture slot */
167 for( i_index = 0 ; i_index < VOUT_MAX_PICTURES ; i_index++ )
169 if( p_vout->p_picture[ i_index ].i_status == FREE_PICTURE )
171 p_pic = p_vout->p_picture + i_index;
181 /* Allocate the picture */
182 p_pic->p->i_lines = p_vout->output.i_height;
183 p_pic->p->i_pitch = 4 * ((p_vout->output.i_width + 15) & ~15);
184 p_pic->p->i_pixel_pitch = 4;
185 p_pic->p->i_visible_pitch = 4 * p_vout->output.i_width;
187 p_pic->p->p_pixels = malloc( p_pic->p->i_pitch * p_pic->p->i_lines );
189 p_pic->i_status = DESTROYED_PICTURE;
190 p_pic->i_type = DIRECT_PICTURE;
192 PP_OUTPUTPICTURE[ I_OUTPUTPICTURES ] = p_pic;
198 /*****************************************************************************
199 * End: terminate libcaca video output thread
200 *****************************************************************************/
201 static void End( vout_thread_t *p_vout )
203 caca_free_bitmap( p_vout->p_sys->p_bitmap );
206 /*****************************************************************************
207 * Destroy: destroy libcaca video output thread
208 *****************************************************************************
209 * Terminate an output method created by AaCreateOutputMethod
210 *****************************************************************************/
211 static void Destroy( vlc_object_t *p_this )
213 vout_thread_t *p_vout = (vout_thread_t *)p_this;
216 free( p_vout->p_sys );
219 /*****************************************************************************
220 * Manage: handle libcaca events
221 *****************************************************************************
222 * This function should be called regularly by video output thread. It manages
223 * console events. It returns a non null value on error.
224 *****************************************************************************/
225 static int Manage( vout_thread_t *p_vout )
230 while(( event = caca_get_event() ))
232 if( event & CACA_EVENT_KEY_PRESS )
234 switch( event & 0xffff )
237 val.i_int = KEY_MODIFIER_CTRL | 'q';
240 val.i_int = KEY_SPACE;
247 var_Set( p_vout->p_vlc, "key-pressed", val );
253 /*****************************************************************************
254 * Render: render previously calculated output
255 *****************************************************************************/
256 static void Render( vout_thread_t *p_vout, picture_t *p_pic )
259 caca_draw_bitmap( 0, 0, caca_get_width() - 1, caca_get_height() - 1,
260 p_vout->p_sys->p_bitmap, p_pic->p->p_pixels );
263 /*****************************************************************************
264 * Display: displays previously rendered output
265 *****************************************************************************/
266 static void Display( vout_thread_t *p_vout, picture_t *p_pic )