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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, 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 filesrc location=test.avi ! decodebin ! ffmpegcolorspace ! aasink
29 * ]| This pipeline renders a video to ascii art into a separate window.
31 * gst-launch filesrc location=test.avi ! decodebin ! ffmpegcolorspace ! aasink driver=curses
32 * ]| This pipeline renders a video to ascii art into the current terminal.
43 #include "gstaasink.h"
45 /* aasink signals and args */
64 PROP_FRAMES_DISPLAYED,
68 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
71 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("I420"))
74 static gboolean gst_aasink_setcaps (GstBaseSink * pad, GstCaps * caps);
75 static void gst_aasink_get_times (GstBaseSink * sink, GstBuffer * buffer,
76 GstClockTime * start, GstClockTime * end);
77 static GstFlowReturn gst_aasink_render (GstBaseSink * basesink,
80 static void gst_aasink_set_property (GObject * object, guint prop_id,
81 const GValue * value, GParamSpec * pspec);
82 static void gst_aasink_get_property (GObject * object, guint prop_id,
83 GValue * value, GParamSpec * pspec);
85 static GstStateChangeReturn gst_aasink_change_state (GstElement * element,
86 GstStateChange transition);
88 #define gst_aasink_parent_class parent_class
89 G_DEFINE_TYPE (GstAASink, gst_aasink, GST_TYPE_BASE_SINK);
91 #define GST_TYPE_AADRIVERS (gst_aasink_drivers_get_type())
93 gst_aasink_drivers_get_type (void)
95 static GType driver_type = 0;
99 const struct aa_driver *driver;
103 for (n_drivers = 0; aa_drivers[n_drivers]; n_drivers++) {
104 /* count number of drivers */
107 drivers = g_new0 (GEnumValue, n_drivers + 1);
109 for (i = 0; i < n_drivers; i++) {
110 driver = aa_drivers[i];
111 drivers[i].value = i;
112 drivers[i].value_name = g_strdup (driver->name);
113 drivers[i].value_nick = g_utf8_strdown (driver->shortname, -1);
115 drivers[i].value = 0;
116 drivers[i].value_name = NULL;
117 drivers[i].value_nick = NULL;
119 driver_type = g_enum_register_static ("GstAASinkDrivers", drivers);
124 #define GST_TYPE_AADITHER (gst_aasink_dither_get_type())
126 gst_aasink_dither_get_type (void)
128 static GType dither_type = 0;
131 GEnumValue *ditherers;
135 for (n_ditherers = 0; aa_dithernames[n_ditherers]; n_ditherers++) {
136 /* count number of ditherers */
139 ditherers = g_new0 (GEnumValue, n_ditherers + 1);
141 for (i = 0; i < n_ditherers; i++) {
142 ditherers[i].value = i;
143 ditherers[i].value_name = g_strdup (aa_dithernames[i]);
144 ditherers[i].value_nick =
145 g_strdelimit (g_strdup (aa_dithernames[i]), " _", '-');
147 ditherers[i].value = 0;
148 ditherers[i].value_name = NULL;
149 ditherers[i].value_nick = NULL;
151 dither_type = g_enum_register_static ("GstAASinkDitherers", ditherers);
157 gst_aasink_class_init (GstAASinkClass * klass)
159 GObjectClass *gobject_class;
160 GstElementClass *gstelement_class;
161 GstBaseSinkClass *gstbasesink_class;
163 gobject_class = (GObjectClass *) klass;
164 gstelement_class = (GstElementClass *) klass;
165 gstbasesink_class = (GstBaseSinkClass *) klass;
167 gobject_class->set_property = gst_aasink_set_property;
168 gobject_class->get_property = gst_aasink_get_property;
170 /* FIXME: add long property descriptions */
171 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_WIDTH,
172 g_param_spec_int ("width", "width", "width", G_MININT, G_MAXINT, 0,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_HEIGHT,
175 g_param_spec_int ("height", "height", "height", G_MININT, G_MAXINT, 0,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DRIVER,
178 g_param_spec_enum ("driver", "driver", "driver", GST_TYPE_AADRIVERS, 0,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_DITHER,
181 g_param_spec_enum ("dither", "dither", "dither", GST_TYPE_AADITHER, 0,
182 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_BRIGHTNESS,
184 g_param_spec_int ("brightness", "brightness", "brightness", G_MININT,
185 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_CONTRAST,
187 g_param_spec_int ("contrast", "contrast", "contrast", G_MININT, G_MAXINT,
188 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
189 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_GAMMA,
190 g_param_spec_float ("gamma", "gamma", "gamma", 0.0, 5.0, 1.0,
191 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
192 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_INVERSION,
193 g_param_spec_boolean ("inversion", "inversion", "inversion", TRUE,
194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
195 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_RANDOMVAL,
196 g_param_spec_int ("randomval", "randomval", "randomval", G_MININT,
197 G_MAXINT, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (G_OBJECT_CLASS (klass),
199 PROP_FRAMES_DISPLAYED, g_param_spec_int ("frames-displayed",
200 "frames displayed", "frames displayed", G_MININT, G_MAXINT, 0,
201 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
202 g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_FRAME_TIME,
203 g_param_spec_int ("frame-time", "frame time", "frame time", G_MININT,
204 G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
206 gst_element_class_add_pad_template (gstelement_class,
207 gst_static_pad_template_get (&sink_template));
209 gst_element_class_set_details_simple (gstelement_class,
210 "ASCII art video sink", "Sink/Video", "An ASCII art videosink",
211 "Wim Taymans <wim.taymans@chello.be>");
213 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_aasink_change_state);
215 gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_aasink_setcaps);
216 gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_aasink_get_times);
217 gstbasesink_class->preroll = GST_DEBUG_FUNCPTR (gst_aasink_render);
218 gstbasesink_class->render = GST_DEBUG_FUNCPTR (gst_aasink_render);
222 gst_aasink_fixate (GstPad * pad, GstCaps * caps)
224 GstStructure *structure;
226 structure = gst_caps_get_structure (caps, 0);
228 gst_structure_fixate_field_nearest_int (structure, "width", 320);
229 gst_structure_fixate_field_nearest_int (structure, "height", 240);
230 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
234 gst_aasink_setcaps (GstBaseSink * basesink, GstCaps * caps)
239 aasink = GST_AASINK (basesink);
241 if (!gst_video_info_from_caps (&info, caps))
251 GST_DEBUG_OBJECT (aasink, "invalid caps");
257 gst_aasink_init (GstAASink * aasink)
261 pad = GST_BASE_SINK_PAD (aasink);
262 gst_pad_set_fixatecaps_function (pad, gst_aasink_fixate);
264 memcpy (&aasink->ascii_surf, &aa_defparams,
265 sizeof (struct aa_hardware_params));
266 aasink->ascii_parms.bright = 0;
267 aasink->ascii_parms.contrast = 16;
268 aasink->ascii_parms.gamma = 1.0;
269 aasink->ascii_parms.dither = 0;
270 aasink->ascii_parms.inversion = 0;
271 aasink->ascii_parms.randomval = 0;
272 aasink->aa_driver = 0;
276 gst_aasink_scale (GstAASink * aasink, guchar * src, guchar * dest,
277 gint sw, gint sh, gint dw, gint dh)
282 g_return_if_fail ((dw != 0) && (dh != 0));
285 yinc = (sh << 16) / dh;
286 xinc = (sw << 16) / dw;
288 for (y = dh; y; y--) {
289 while (ypos > 0x10000) {
295 guchar *destp = dest;
298 for (x = dw; x; x--) {
299 while (xpos >= 0x10000L) {
313 gst_aasink_get_times (GstBaseSink * sink, GstBuffer * buffer,
314 GstClockTime * start, GstClockTime * end)
316 *start = GST_BUFFER_TIMESTAMP (buffer);
317 if (GST_BUFFER_DURATION_IS_VALID (buffer))
318 *end = *start + GST_BUFFER_DURATION (buffer);
322 gst_aasink_render (GstBaseSink * basesink, GstBuffer * buffer)
327 aasink = GST_AASINK (basesink);
329 GST_DEBUG ("render");
331 if (!gst_video_frame_map (&frame, &aasink->info, buffer, GST_MAP_READ))
334 gst_aasink_scale (aasink, GST_VIDEO_FRAME_PLANE_DATA (&frame, 0), /* src */
335 aa_image (aasink->context), /* dest */
336 GST_VIDEO_INFO_WIDTH (&aasink->info), /* sw */
337 GST_VIDEO_INFO_HEIGHT (&aasink->info), /* sh */
338 aa_imgwidth (aasink->context), /* dw */
339 aa_imgheight (aasink->context)); /* dh */
341 aa_render (aasink->context, &aasink->ascii_parms,
342 0, 0, aa_imgwidth (aasink->context), aa_imgheight (aasink->context));
343 aa_flush (aasink->context);
344 aa_getevent (aasink->context, FALSE);
345 gst_video_frame_unmap (&frame);
352 GST_DEBUG_OBJECT (aasink, "invalid frame");
353 return GST_FLOW_ERROR;
359 gst_aasink_set_property (GObject * object, guint prop_id, const GValue * value,
364 aasink = GST_AASINK (object);
368 aasink->ascii_surf.width = g_value_get_int (value);
371 aasink->ascii_surf.height = g_value_get_int (value);
374 aasink->aa_driver = g_value_get_enum (value);
378 aasink->ascii_parms.dither = g_value_get_enum (value);
381 case PROP_BRIGHTNESS:{
382 aasink->ascii_parms.bright = g_value_get_int (value);
386 aasink->ascii_parms.contrast = g_value_get_int (value);
390 aasink->ascii_parms.gamma = g_value_get_float (value);
393 case PROP_INVERSION:{
394 aasink->ascii_parms.inversion = g_value_get_boolean (value);
397 case PROP_RANDOMVAL:{
398 aasink->ascii_parms.randomval = g_value_get_int (value);
407 gst_aasink_get_property (GObject * object, guint prop_id, GValue * value,
412 aasink = GST_AASINK (object);
416 g_value_set_int (value, aasink->ascii_surf.width);
420 g_value_set_int (value, aasink->ascii_surf.height);
424 g_value_set_enum (value, aasink->aa_driver);
428 g_value_set_enum (value, aasink->ascii_parms.dither);
431 case PROP_BRIGHTNESS:{
432 g_value_set_int (value, aasink->ascii_parms.bright);
436 g_value_set_int (value, aasink->ascii_parms.contrast);
440 g_value_set_float (value, aasink->ascii_parms.gamma);
443 case PROP_INVERSION:{
444 g_value_set_boolean (value, aasink->ascii_parms.inversion);
447 case PROP_RANDOMVAL:{
448 g_value_set_int (value, aasink->ascii_parms.randomval);
451 case PROP_FRAMES_DISPLAYED:{
452 g_value_set_int (value, aasink->frames_displayed);
455 case PROP_FRAME_TIME:{
456 g_value_set_int (value, aasink->frame_time / 1000000);
460 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
467 gst_aasink_open (GstAASink * aasink)
469 if (!aasink->context) {
470 aa_recommendhidisplay (aa_drivers[aasink->aa_driver]->shortname);
472 aasink->context = aa_autoinit (&aasink->ascii_surf);
473 if (aasink->context == NULL) {
474 GST_ELEMENT_ERROR (GST_ELEMENT (aasink), LIBRARY, TOO_LAZY, (NULL),
475 ("error opening aalib context"));
478 aa_autoinitkbd (aasink->context, 0);
479 aa_resizehandler (aasink->context, (void *) aa_resize);
485 gst_aasink_close (GstAASink * aasink)
487 aa_close (aasink->context);
488 aasink->context = NULL;
493 static GstStateChangeReturn
494 gst_aasink_change_state (GstElement * element, GstStateChange transition)
496 GstStateChangeReturn ret;
499 switch (transition) {
500 case GST_STATE_CHANGE_NULL_TO_READY:
502 case GST_STATE_CHANGE_READY_TO_PAUSED:
503 if (!gst_aasink_open (GST_AASINK (element)))
506 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
512 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
514 switch (transition) {
515 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
517 case GST_STATE_CHANGE_PAUSED_TO_READY:
519 case GST_STATE_CHANGE_READY_TO_NULL:
520 gst_aasink_close (GST_AASINK (element));
530 return GST_STATE_CHANGE_FAILURE;
535 plugin_init (GstPlugin * plugin)
537 if (!gst_element_register (plugin, "aasink", GST_RANK_NONE, GST_TYPE_AASINK))
543 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
546 "ASCII Art video sink",
547 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);