2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * SECTION:element-cacasink
21 * @see_also: #GstAASink
23 * Displays video as color ascii art.
26 * <title>Example launch line</title>
28 * CACA_GEOMETRY=160x60 CACA_FONT=5x7 gst-launch-1.0 filesrc location=test.avi ! decodebin ! videoconvert ! cacasink
29 * ]| This pipeline renders a video to ascii art into a separate window using a
30 * small font and specifying the ascii resolution.
32 * CACA_DRIVER=ncurses gst-launch-1.0 filesrc location=test.avi ! decodebin ! videoconvert ! cacasink
33 * ]| This pipeline renders a video to ascii art into the current terminal.
44 #include "gstcacasink.h"
47 //#define GST_CACA_DEFAULT_RED_MASK R_MASK_32_REVERSE_INT
48 //#define GST_CACA_DEFAULT_GREEN_MASK G_MASK_32_REVERSE_INT
49 //#define GST_CACA_DEFAULT_BLUE_MASK B_MASK_32_REVERSE_INT
51 /* cacasink signals and args */
57 #define GST_CACA_DEFAULT_SCREEN_WIDTH 80
58 #define GST_CACA_DEFAULT_SCREEN_HEIGHT 25
59 #define GST_CACA_DEFAULT_DITHER CACA_DITHERING_NONE
60 #define GST_CACA_DEFAULT_ANTIALIASING TRUE
71 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
74 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE
75 ("{ RGB, BGR, RGBx, xRGB, BGRx, xBGR, RGB16, RGB15 }"))
78 static gboolean gst_cacasink_setcaps (GstBaseSink * pad, GstCaps * caps);
79 static void gst_cacasink_get_times (GstBaseSink * sink, GstBuffer * buffer,
80 GstClockTime * start, GstClockTime * end);
81 static GstFlowReturn gst_cacasink_render (GstBaseSink * basesink,
84 static void gst_cacasink_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_cacasink_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static GstStateChangeReturn gst_cacasink_change_state (GstElement * element,
90 GstStateChange transition);
92 #define gst_cacasink_parent_class parent_class
93 G_DEFINE_TYPE (GstCACASink, gst_cacasink, GST_TYPE_BASE_SINK);
95 #define GST_TYPE_CACADITHER (gst_cacasink_dither_get_type())
97 gst_cacasink_dither_get_type (void)
99 static GType dither_type = 0;
101 static const GEnumValue dither_types[] = {
102 {CACA_DITHERING_NONE, "No dithering", "none"},
103 {CACA_DITHERING_ORDERED2, "Ordered 2x2 Bayer dithering", "2x2"},
104 {CACA_DITHERING_ORDERED4, "Ordered 4x4 Bayer dithering", "4x4"},
105 {CACA_DITHERING_ORDERED8, "Ordered 8x8 Bayer dithering", "8x8"},
106 {CACA_DITHERING_RANDOM, "Random dithering", "random"},
111 dither_type = g_enum_register_static ("GstCACASinkDithering", dither_types);
117 gst_cacasink_class_init (GstCACASinkClass * klass)
119 GObjectClass *gobject_class;
120 GstElementClass *gstelement_class;
121 GstBaseSinkClass *gstbasesink_class;
123 gobject_class = (GObjectClass *) klass;
124 gstelement_class = (GstElementClass *) klass;
125 gstbasesink_class = (GstBaseSinkClass *) klass;
127 parent_class = g_type_class_peek_parent (klass);
129 gobject_class->set_property = gst_cacasink_set_property;
130 gobject_class->get_property = gst_cacasink_get_property;
132 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCREEN_WIDTH,
133 g_param_spec_int ("screen-width", "Screen Width",
134 "The width of the screen", 0, G_MAXINT, GST_CACA_DEFAULT_SCREEN_WIDTH,
135 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
136 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_SCREEN_HEIGHT,
137 g_param_spec_int ("screen-height", "Screen Height",
138 "The height of the screen", 0, G_MAXINT,
139 GST_CACA_DEFAULT_SCREEN_HEIGHT,
140 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
141 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DITHER,
142 g_param_spec_enum ("dither", "Dither Type", "Set type of Dither",
143 GST_TYPE_CACADITHER, GST_CACA_DEFAULT_DITHER,
144 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_ANTIALIASING,
146 g_param_spec_boolean ("anti-aliasing", "Anti Aliasing",
147 "Enables Anti-Aliasing", GST_CACA_DEFAULT_ANTIALIASING,
148 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
150 gstelement_class->change_state = gst_cacasink_change_state;
152 gst_element_class_set_static_metadata (gstelement_class,
153 "A colored ASCII art video sink", "Sink/Video",
154 "A colored ASCII art videosink", "Zeeshan Ali <zak147@yahoo.com>");
155 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
157 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_cacasink_setcaps);
158 gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_cacasink_get_times);
159 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_cacasink_render);
160 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_cacasink_render);
164 gst_cacasink_get_times (GstBaseSink * sink, GstBuffer * buffer,
165 GstClockTime * start, GstClockTime * end)
167 *start = GST_BUFFER_TIMESTAMP (buffer);
168 *end = *start + GST_BUFFER_DURATION (buffer);
172 gst_cacasink_setcaps (GstBaseSink * basesink, GstCaps * caps)
174 GstCACASink *cacasink;
176 guint bpp, red_mask, green_mask, blue_mask;
178 cacasink = GST_CACASINK (basesink);
180 if (!gst_video_info_from_caps (&info, caps))
183 switch (GST_VIDEO_INFO_FORMAT (&info)) {
184 case GST_VIDEO_FORMAT_RGB:
185 case GST_VIDEO_FORMAT_BGR:
186 case GST_VIDEO_FORMAT_RGBx:
187 case GST_VIDEO_FORMAT_xRGB:
188 case GST_VIDEO_FORMAT_BGRx:
189 case GST_VIDEO_FORMAT_xBGR:
190 bpp = 8 * info.finfo->pixel_stride[0];
191 red_mask = 0xff << (8 * info.finfo->poffset[GST_VIDEO_COMP_R]);
192 green_mask = 0xff << (8 * info.finfo->poffset[GST_VIDEO_COMP_G]);
193 blue_mask = 0xff << (8 * info.finfo->poffset[GST_VIDEO_COMP_B]);
195 case GST_VIDEO_FORMAT_RGB16:
201 case GST_VIDEO_FORMAT_RGB15:
211 if (cacasink->bitmap) {
212 caca_free_bitmap (cacasink->bitmap);
214 cacasink->bitmap = caca_create_bitmap (bpp,
215 GST_VIDEO_INFO_WIDTH (&info),
216 GST_VIDEO_INFO_HEIGHT (&info),
217 GST_ROUND_UP_4 (GST_VIDEO_INFO_WIDTH (&info) * bpp / 8),
218 red_mask, green_mask, blue_mask, 0);
219 if (!cacasink->bitmap)
222 cacasink->info = info;
229 GST_ERROR_OBJECT (cacasink, "error parsing caps");
234 GST_ERROR_OBJECT (cacasink, "invalid format");
239 GST_ERROR_OBJECT (cacasink, "could not create bitmap");
245 gst_cacasink_init (GstCACASink * cacasink)
247 cacasink->screen_width = GST_CACA_DEFAULT_SCREEN_WIDTH;
248 cacasink->screen_height = GST_CACA_DEFAULT_SCREEN_HEIGHT;
250 cacasink->dither = GST_CACA_DEFAULT_DITHER;
251 cacasink->antialiasing = GST_CACA_DEFAULT_ANTIALIASING;
255 gst_cacasink_render (GstBaseSink * basesink, GstBuffer * buffer)
257 GstCACASink *cacasink = GST_CACASINK (basesink);
260 GST_DEBUG ("render");
262 if (!gst_video_frame_map (&frame, &cacasink->info, buffer, GST_MAP_READ))
266 caca_draw_bitmap (0, 0, cacasink->screen_width - 1,
267 cacasink->screen_height - 1, cacasink->bitmap,
268 GST_VIDEO_FRAME_PLANE_DATA (&frame, 0));
271 gst_video_frame_unmap (&frame);
278 GST_ERROR_OBJECT (cacasink, "invalid frame received");
279 return GST_FLOW_ERROR;
284 gst_cacasink_set_property (GObject * object, guint prop_id,
285 const GValue * value, GParamSpec * pspec)
287 GstCACASink *cacasink;
289 g_return_if_fail (GST_IS_CACASINK (object));
291 cacasink = GST_CACASINK (object);
295 cacasink->dither = g_value_get_enum (value);
296 caca_set_dithering (cacasink->dither + CACA_DITHERING_NONE);
299 case PROP_ANTIALIASING:{
300 cacasink->antialiasing = g_value_get_boolean (value);
301 if (cacasink->antialiasing) {
302 caca_set_feature (CACA_ANTIALIASING_MAX);
304 caca_set_feature (CACA_ANTIALIASING_MIN);
314 gst_cacasink_get_property (GObject * object, guint prop_id, GValue * value,
317 GstCACASink *cacasink;
319 cacasink = GST_CACASINK (object);
322 case PROP_SCREEN_WIDTH:{
323 g_value_set_int (value, cacasink->screen_width);
326 case PROP_SCREEN_HEIGHT:{
327 g_value_set_int (value, cacasink->screen_height);
331 g_value_set_enum (value, cacasink->dither);
334 case PROP_ANTIALIASING:{
335 g_value_set_boolean (value, cacasink->antialiasing);
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
346 gst_cacasink_open (GstCACASink * cacasink)
348 cacasink->bitmap = NULL;
350 if (caca_init () < 0)
353 cacasink->screen_width = caca_get_width ();
354 cacasink->screen_height = caca_get_height ();
355 cacasink->antialiasing = TRUE;
356 caca_set_feature (CACA_ANTIALIASING_MAX);
357 cacasink->dither = 0;
358 caca_set_dithering (CACA_DITHERING_NONE);
365 GST_ELEMENT_ERROR (cacasink, RESOURCE, OPEN_WRITE, (NULL),
366 ("caca_init() failed"));
372 gst_cacasink_close (GstCACASink * cacasink)
374 if (cacasink->bitmap) {
375 caca_free_bitmap (cacasink->bitmap);
376 cacasink->bitmap = NULL;
381 static GstStateChangeReturn
382 gst_cacasink_change_state (GstElement * element, GstStateChange transition)
384 GstStateChangeReturn ret;
386 switch (transition) {
387 case GST_STATE_CHANGE_READY_TO_PAUSED:
388 if (!gst_cacasink_open (GST_CACASINK (element)))
389 return GST_STATE_CHANGE_FAILURE;
395 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
397 switch (transition) {
398 case GST_STATE_CHANGE_PAUSED_TO_READY:
399 gst_cacasink_close (GST_CACASINK (element));
408 plugin_init (GstPlugin * plugin)
410 if (!gst_element_register (plugin, "cacasink", GST_RANK_NONE,
417 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
420 "Colored ASCII Art video sink",
421 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)