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-aasink
21 * @see_also: #GstCACASink
23 * Displays video as b/w ascii art.
26 * <title>Example launch line</title>
28 * gst-launch-1.0 filesrc location=test.avi ! decodebin ! videoconvert ! aasink
29 * ]| This pipeline renders a video to ascii art into a separate window.
31 * gst-launch-1.0 filesrc location=test.avi ! decodebin ! videoconvert ! aasink driver=curses
32 * ]| This pipeline renders a video to ascii art into the current terminal.
43 #include <gst/video/gstvideometa.h>
44 #include "gstaasink.h"
46 /* aasink signals and args */
65 PROP_FRAMES_DISPLAYED,
69 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
72 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
75 static GstCaps *gst_aasink_fixate (GstBaseSink * bsink, GstCaps * caps);
76 static gboolean gst_aasink_setcaps (GstBaseSink * bsink, GstCaps * caps);
77 static void gst_aasink_get_times (GstBaseSink * bsink, GstBuffer * buffer,
78 GstClockTime * start, GstClockTime * end);
79 static gboolean gst_aasink_propose_allocation (GstBaseSink * bsink,
81 static GstFlowReturn gst_aasink_show_frame (GstVideoSink * videosink,
84 static void gst_aasink_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_aasink_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
89 static GstStateChangeReturn gst_aasink_change_state (GstElement * element,
90 GstStateChange transition);
92 #define gst_aasink_parent_class parent_class
93 G_DEFINE_TYPE (GstAASink, gst_aasink, GST_TYPE_VIDEO_SINK);
95 #define GST_TYPE_AADRIVERS (gst_aasink_drivers_get_type())
97 gst_aasink_drivers_get_type (void)
99 static GType driver_type = 0;
103 const struct aa_driver *driver;
107 for (n_drivers = 0; aa_drivers[n_drivers]; n_drivers++) {
108 /* count number of drivers */
111 drivers = g_new0 (GEnumValue, n_drivers + 1);
113 for (i = 0; i < n_drivers; i++) {
114 driver = aa_drivers[i];
115 drivers[i].value = i;
116 drivers[i].value_name = g_strdup (driver->name);
117 drivers[i].value_nick = g_utf8_strdown (driver->shortname, -1);
119 drivers[i].value = 0;
120 drivers[i].value_name = NULL;
121 drivers[i].value_nick = NULL;
123 driver_type = g_enum_register_static ("GstAASinkDrivers", drivers);
128 #define GST_TYPE_AADITHER (gst_aasink_dither_get_type())
130 gst_aasink_dither_get_type (void)
132 static GType dither_type = 0;
135 GEnumValue *ditherers;
139 for (n_ditherers = 0; aa_dithernames[n_ditherers]; n_ditherers++) {
140 /* count number of ditherers */
143 ditherers = g_new0 (GEnumValue, n_ditherers + 1);
145 for (i = 0; i < n_ditherers; i++) {
146 ditherers[i].value = i;
147 ditherers[i].value_name = g_strdup (aa_dithernames[i]);
148 ditherers[i].value_nick =
149 g_strdelimit (g_strdup (aa_dithernames[i]), " _", '-');
151 ditherers[i].value = 0;
152 ditherers[i].value_name = NULL;
153 ditherers[i].value_nick = NULL;
155 dither_type = g_enum_register_static ("GstAASinkDitherers", ditherers);
161 gst_aasink_class_init (GstAASinkClass * klass)
163 GObjectClass *gobject_class;
164 GstElementClass *gstelement_class;
165 GstBaseSinkClass *gstbasesink_class;
166 GstVideoSinkClass *gstvideosink_class;
168 gobject_class = (GObjectClass *) klass;
169 gstelement_class = (GstElementClass *) klass;
170 gstbasesink_class = (GstBaseSinkClass *) klass;
171 gstvideosink_class = (GstVideoSinkClass *) klass;
173 gobject_class->set_property = gst_aasink_set_property;
174 gobject_class->get_property = gst_aasink_get_property;
176 /* FIXME: add long property descriptions */
177 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_WIDTH,
178 g_param_spec_int ("width", "width", "width", G_MININT, G_MAXINT, 0,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HEIGHT,
181 g_param_spec_int ("height", "height", "height", G_MININT, G_MAXINT, 0,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DRIVER,
184 g_param_spec_enum ("driver", "driver", "driver", GST_TYPE_AADRIVERS, 0,
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DITHER,
187 g_param_spec_enum ("dither", "dither", "dither", GST_TYPE_AADITHER, 0,
188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BRIGHTNESS,
190 g_param_spec_int ("brightness", "brightness", "brightness", G_MININT,
191 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONTRAST,
193 g_param_spec_int ("contrast", "contrast", "contrast", G_MININT, G_MAXINT,
194 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GAMMA,
196 g_param_spec_float ("gamma", "gamma", "gamma", 0.0, 5.0, 1.0,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INVERSION,
199 g_param_spec_boolean ("inversion", "inversion", "inversion", TRUE,
200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RANDOMVAL,
202 g_param_spec_int ("randomval", "randomval", "randomval", G_MININT,
203 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 g_object_class_install_property (G_OBJECT_CLASS (klass),
205 PROP_FRAMES_DISPLAYED, g_param_spec_int ("frames-displayed",
206 "frames displayed", "frames displayed", G_MININT, G_MAXINT, 0,
207 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
208 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FRAME_TIME,
209 g_param_spec_int ("frame-time", "frame time", "frame time", G_MININT,
210 G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
212 gst_element_class_add_static_pad_template (gstelement_class, &sink_template);
214 gst_element_class_set_static_metadata (gstelement_class,
215 "ASCII art video sink", "Sink/Video", "An ASCII art videosink",
216 "Wim Taymans <wim.taymans@chello.be>");
218 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_aasink_change_state);
220 gstbasesink_class->fixate = GST_DEBUG_FUNCPTR (gst_aasink_fixate);
221 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_aasink_setcaps);
222 gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_aasink_get_times);
223 gstbasesink_class->propose_allocation =
224 GST_DEBUG_FUNCPTR (gst_aasink_propose_allocation);
226 gstvideosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_aasink_show_frame);
230 gst_aasink_fixate (GstBaseSink * bsink, GstCaps * caps)
232 GstStructure *structure;
234 caps = gst_caps_make_writable (caps);
236 structure = gst_caps_get_structure (caps, 0);
238 gst_structure_fixate_field_nearest_int (structure, "width", 320);
239 gst_structure_fixate_field_nearest_int (structure, "height", 240);
240 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
242 caps = GST_BASE_SINK_CLASS (parent_class)->fixate (bsink, caps);
248 gst_aasink_setcaps (GstBaseSink * basesink, GstCaps * caps)
253 aasink = GST_AASINK (basesink);
255 if (!gst_video_info_from_caps (&info, caps))
265 GST_DEBUG_OBJECT (aasink, "invalid caps");
271 gst_aasink_init (GstAASink * aasink)
273 memcpy (&aasink->ascii_surf, &aa_defparams,
274 sizeof (struct aa_hardware_params));
275 aasink->ascii_parms.bright = 0;
276 aasink->ascii_parms.contrast = 16;
277 aasink->ascii_parms.gamma = 1.0;
278 aasink->ascii_parms.dither = 0;
279 aasink->ascii_parms.inversion = 0;
280 aasink->ascii_parms.randomval = 0;
281 aasink->aa_driver = 0;
285 gst_aasink_scale (GstAASink * aasink, guchar * src, guchar * dest,
286 gint sw, gint sh, gint ss, gint dw, gint dh)
291 g_return_if_fail ((dw != 0) && (dh != 0));
294 yinc = (sh << 16) / dh;
295 xinc = (sw << 16) / dw;
297 for (y = dh; y; y--) {
298 while (ypos > 0x10000) {
304 guchar *destp = dest;
307 for (x = dw; x; x--) {
308 while (xpos >= 0x10000L) {
322 gst_aasink_get_times (GstBaseSink * sink, GstBuffer * buffer,
323 GstClockTime * start, GstClockTime * end)
325 *start = GST_BUFFER_TIMESTAMP (buffer);
326 if (GST_BUFFER_DURATION_IS_VALID (buffer))
327 *end = *start + GST_BUFFER_DURATION (buffer);
331 gst_aasink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
337 gst_query_parse_allocation (query, &caps, NULL);
342 if (!gst_video_info_from_caps (&info, caps))
345 size = GST_VIDEO_INFO_SIZE (&info);
347 /* we need at least 2 buffer because we hold on to the last one */
348 gst_query_add_allocation_pool (query, NULL, size, 2, 0);
350 /* we support various metadata */
351 gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
358 GST_DEBUG_OBJECT (bsink, "no caps specified");
363 GST_DEBUG_OBJECT (bsink, "invalid caps specified");
369 gst_aasink_show_frame (GstVideoSink * videosink, GstBuffer * buffer)
374 aasink = GST_AASINK (videosink);
376 GST_DEBUG ("show frame");
378 if (!gst_video_frame_map (&frame, &aasink->info, buffer, GST_MAP_READ))
381 gst_aasink_scale (aasink, GST_VIDEO_FRAME_PLANE_DATA (&frame, 0), /* src */
382 aa_image (aasink->context), /* dest */
383 GST_VIDEO_INFO_WIDTH (&aasink->info), /* sw */
384 GST_VIDEO_INFO_HEIGHT (&aasink->info), /* sh */
385 GST_VIDEO_FRAME_PLANE_STRIDE (&frame, 0), /* ss */
386 aa_imgwidth (aasink->context), /* dw */
387 aa_imgheight (aasink->context)); /* dh */
389 aa_render (aasink->context, &aasink->ascii_parms,
390 0, 0, aa_imgwidth (aasink->context), aa_imgheight (aasink->context));
391 aa_flush (aasink->context);
392 aa_getevent (aasink->context, FALSE);
393 gst_video_frame_unmap (&frame);
400 GST_DEBUG_OBJECT (aasink, "invalid frame");
401 return GST_FLOW_ERROR;
407 gst_aasink_set_property (GObject * object, guint prop_id, const GValue * value,
412 aasink = GST_AASINK (object);
416 aasink->ascii_surf.width = g_value_get_int (value);
419 aasink->ascii_surf.height = g_value_get_int (value);
422 aasink->aa_driver = g_value_get_enum (value);
426 aasink->ascii_parms.dither = g_value_get_enum (value);
429 case PROP_BRIGHTNESS:{
430 aasink->ascii_parms.bright = g_value_get_int (value);
434 aasink->ascii_parms.contrast = g_value_get_int (value);
438 aasink->ascii_parms.gamma = g_value_get_float (value);
441 case PROP_INVERSION:{
442 aasink->ascii_parms.inversion = g_value_get_boolean (value);
445 case PROP_RANDOMVAL:{
446 aasink->ascii_parms.randomval = g_value_get_int (value);
455 gst_aasink_get_property (GObject * object, guint prop_id, GValue * value,
460 aasink = GST_AASINK (object);
464 g_value_set_int (value, aasink->ascii_surf.width);
468 g_value_set_int (value, aasink->ascii_surf.height);
472 g_value_set_enum (value, aasink->aa_driver);
476 g_value_set_enum (value, aasink->ascii_parms.dither);
479 case PROP_BRIGHTNESS:{
480 g_value_set_int (value, aasink->ascii_parms.bright);
484 g_value_set_int (value, aasink->ascii_parms.contrast);
488 g_value_set_float (value, aasink->ascii_parms.gamma);
491 case PROP_INVERSION:{
492 g_value_set_boolean (value, aasink->ascii_parms.inversion);
495 case PROP_RANDOMVAL:{
496 g_value_set_int (value, aasink->ascii_parms.randomval);
499 case PROP_FRAMES_DISPLAYED:{
500 g_value_set_int (value, aasink->frames_displayed);
503 case PROP_FRAME_TIME:{
504 g_value_set_int (value, aasink->frame_time / 1000000);
508 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 gst_aasink_open (GstAASink * aasink)
517 if (!aasink->context) {
518 aa_recommendhidisplay (aa_drivers[aasink->aa_driver]->shortname);
520 aasink->context = aa_autoinit (&aasink->ascii_surf);
521 if (aasink->context == NULL) {
522 GST_ELEMENT_ERROR (GST_ELEMENT (aasink), LIBRARY, TOO_LAZY, (NULL),
523 ("error opening aalib context"));
526 aa_autoinitkbd (aasink->context, 0);
527 aa_resizehandler (aasink->context, (void *) aa_resize);
533 gst_aasink_close (GstAASink * aasink)
535 aa_close (aasink->context);
536 aasink->context = NULL;
541 static GstStateChangeReturn
542 gst_aasink_change_state (GstElement * element, GstStateChange transition)
544 GstStateChangeReturn ret;
547 switch (transition) {
548 case GST_STATE_CHANGE_NULL_TO_READY:
550 case GST_STATE_CHANGE_READY_TO_PAUSED:
551 if (!gst_aasink_open (GST_AASINK (element)))
554 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
560 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
562 switch (transition) {
563 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
565 case GST_STATE_CHANGE_PAUSED_TO_READY:
567 case GST_STATE_CHANGE_READY_TO_NULL:
568 gst_aasink_close (GST_AASINK (element));
578 return GST_STATE_CHANGE_FAILURE;
583 plugin_init (GstPlugin * plugin)
585 if (!gst_element_register (plugin, "aasink", GST_RANK_NONE, GST_TYPE_AASINK))
591 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
594 "ASCII Art video sink",
595 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);